Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Alternative to GETS - C++
New Posts  All Forums:Forum Nav:

Alternative to GETS - C++

post #1 of 5
Thread Starter 
Hey,

Well I am looking for an alternative to gets as from what I am told I should never use it. Any help would be great.

The overall program is a lot more code but this should be enough to give me an idea of what to replace.
Code:
int main()
{
while(true)
{
char exp[MAX];
cout << "Enter Postfix Expression : ";
gets(exp);
error = false;
float evaluation = evaluate(&exp[0]);
if(!error)
{
cout << "\t" << evaluation;
}
cout << endl << endl;
}
}
Chidori
(13 items)
 
  
CPUMotherboardGraphicsRAM
intel i7 870 Asus P7P55D-E PRO Sapphire 7950 OC 8GB - G.Skill DDR3 1600 (Ripjaws) 
Hard DriveOptical DriveOSMonitor
C300 128GB SSD,2 x WD Cavier Black 1TB,500GB Green Asus DVD-RW Windows 8 64-bit 3 x Asus VE248H (24') 
KeyboardPowerCaseMouse
Das Mechnical Corsair Professional HX850W Lian Li Lancool PC-K62 Logitech G9x 
Mouse Pad
Qck+ steel 
  hide details  
Reply
Chidori
(13 items)
 
  
CPUMotherboardGraphicsRAM
intel i7 870 Asus P7P55D-E PRO Sapphire 7950 OC 8GB - G.Skill DDR3 1600 (Ripjaws) 
Hard DriveOptical DriveOSMonitor
C300 128GB SSD,2 x WD Cavier Black 1TB,500GB Green Asus DVD-RW Windows 8 64-bit 3 x Asus VE248H (24') 
KeyboardPowerCaseMouse
Das Mechnical Corsair Professional HX850W Lian Li Lancool PC-K62 Logitech G9x 
Mouse Pad
Qck+ steel 
  hide details  
Reply
post #2 of 5
i remember using fgets() way back.. i've heard the 'never use gets, use fgets' thing from somewhere.

i think gets() just does whatever the hell you tell it to without even looking, so it will gladly write bits into outerspace (or read from outerspace maybe). Whereas fgets() does, um some checks or something, and its a little more effort to use and therefore safer. something like that.

you can tell i don't remember much c++. I'm totally ok with that.smile.gif
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
post #3 of 5
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #4 of 5
std::cin / Scanf.
Sandy Bridge
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k P8P67 EVO MSI GTX 770 Gaming Edition 8GB Corsair Dominator GT 
Hard DriveHard DriveCoolingOS
500GB F3 500GB F3 CoolerMaster Hyper 212+ archlinux 64bit / Windows 7 Ultimate 64bit 
MonitorPowerCaseMouse
Dell U2312HM Seasonic M12ll 520W Corsair 650D Logitech G500 
Mouse PadAudio
Razer Goliaath Microlab M-820 
  hide details  
Reply
Sandy Bridge
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k P8P67 EVO MSI GTX 770 Gaming Edition 8GB Corsair Dominator GT 
Hard DriveHard DriveCoolingOS
500GB F3 500GB F3 CoolerMaster Hyper 212+ archlinux 64bit / Windows 7 Ultimate 64bit 
MonitorPowerCaseMouse
Dell U2312HM Seasonic M12ll 520W Corsair 650D Logitech G500 
Mouse PadAudio
Razer Goliaath Microlab M-820 
  hide details  
Reply
post #5 of 5
gets is actually C, if you're going purely C++ please use cin as stated above, if you are on a Microsoft Compiler and have Microsoft Header files and want to use a C function DO NOT use scanf, use scanf_s, it is the safe version of scanf_s as scanf is deprecated. scanf_s is not ANSI though so portability would be an issue, strictly from a C standpoint.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Alternative to GETS - C++