Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › What's wrong with this code?
New Posts  All Forums:Forum Nav:

What's wrong with this code?

post #1 of 5
Thread Starter 
Ignore the demented formatting it just wont copy paste normally from visual studio... I cannot seem to figure out what is wrong with this C++ code... Basically it loops when I input the correct value (F or N). It comes up with the proper error/retry if I input an invalid character but when I input what is meant to be in there it just re-displays: "Enter book type (F=Fiction, N=Non-Fiction): "... This is pretty simple really and should work... am I writing the while properly? Even when I change rBook to an 'A' it seems to think it is an 'R' and loops it...


Edit: I am using conio.h for the cin/cout etc and I am not allowed to use other more complex stuff to do this I am limited to these commands...


while (rBook == 'R')
{
cout << "Enter book type (F=Fiction, N=Non-Fiction): ";
cin >> rBook;
BookType = rBook;
BookType = toupper(BookType);

if (BookType == 'F')
{
BookTotalF = BookTotalF + 1;
BookTypeF = 1;
rBook = 'A';
}
else if (BookType == 'N')
{
BookTotalN = BookTotalN + 1;
BookTypeN = 1;
rBook = 'A';
}
else
cout << "\
Error, you may only enter F or N \
";
rBook = 'R';
}
6625WD
(13 items)
 
  
CPUMotherboardGraphicsRAM
T7500 2.2GHz Core 2 Duo Intel 965 (Santa Rosa) 8600M GT 512MB GDDR3 2GB DDR2 800MHz Kingston 
Hard Drive
120GB SATA Hitachi 
  hide details  
Reply
6625WD
(13 items)
 
  
CPUMotherboardGraphicsRAM
T7500 2.2GHz Core 2 Duo Intel 965 (Santa Rosa) 8600M GT 512MB GDDR3 2GB DDR2 800MHz Kingston 
Hard Drive
120GB SATA Hitachi 
  hide details  
Reply
post #2 of 5
I'm no expert, and know zero C++ but theres a } missing. Here's the changed code.

while (rBook == 'R')
{
cout << "Enter book type (F=Fiction, N=Non-Fiction): ";
cin >> rBook;
BookType = rBook;
BookType = toupper(BookType);
}
if (BookType == 'F')
{
BookTotalF = BookTotalF + 1;
BookTypeF = 1;
rBook = 'A';
}
else if (BookType == 'N')
{
BookTotalN = BookTotalN + 1;
BookTypeN = 1;
rBook = 'A';
}
else
cout << "\
Error, you may only enter F or N \
";
rBook = 'R';
}
Studio Machine
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Q6600 @ 2.6 Ghz I forget. nVidia 8400 2GB DDR2 
Hard DriveOptical DriveOSMonitor
1x320GB, 1x750GB CD/DVD+-RW Windows Vista Ultimate SP1 & Arch Linux 64bit 19inch Acer X193W 
KeyboardPowerCaseMouse
Logitech UltraSlim Pathetic 350w Cube 2 Quid Bargain Bin Thing 
  hide details  
Reply
Studio Machine
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Q6600 @ 2.6 Ghz I forget. nVidia 8400 2GB DDR2 
Hard DriveOptical DriveOSMonitor
1x320GB, 1x750GB CD/DVD+-RW Windows Vista Ultimate SP1 & Arch Linux 64bit 19inch Acer X193W 
KeyboardPowerCaseMouse
Logitech UltraSlim Pathetic 350w Cube 2 Quid Bargain Bin Thing 
  hide details  
Reply
post #3 of 5
AFAIK You need stdio.h too.
#well
(18 items)
 
Lenovo L530
(8 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 4770k Gigabyte Z87X-UD4H Sapphire RX 580 Corsair CMX8GX3M2A2000C9 
Hard DriveOptical DriveCoolingOS
Samsung 840 EVO Generic DVD±RW Burner Noctua NH-D14 Windows 10 
MonitorMonitorKeyboardPower
Dell U2711 Samsung SyncMaster 2233 Ducky DK9008 Overclock.net Edition, Cherry MX B... Corsair TX850 
CaseMouseMouse PadAudio
Fractal Design Core 2500 Logitech G303 QPAD HeatoN L M-Audio Fast Track USB 
AudioAudio
Beyerdynamic DT-770 80 Ohm AntLion ModMic v3 
CPUGraphicsRAMRAM
Intel Ivy Bridge 3210M Intel HD 4000 Graphics Soldered Corsair Vengeance  
Hard DriveOptical DriveOSMonitor
500GB DVD-+RW Windows 7 Professional 1600x900 
  hide details  
Reply
#well
(18 items)
 
Lenovo L530
(8 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 4770k Gigabyte Z87X-UD4H Sapphire RX 580 Corsair CMX8GX3M2A2000C9 
Hard DriveOptical DriveCoolingOS
Samsung 840 EVO Generic DVD±RW Burner Noctua NH-D14 Windows 10 
MonitorMonitorKeyboardPower
Dell U2711 Samsung SyncMaster 2233 Ducky DK9008 Overclock.net Edition, Cherry MX B... Corsair TX850 
CaseMouseMouse PadAudio
Fractal Design Core 2500 Logitech G303 QPAD HeatoN L M-Audio Fast Track USB 
AudioAudio
Beyerdynamic DT-770 80 Ohm AntLion ModMic v3 
CPUGraphicsRAMRAM
Intel Ivy Bridge 3210M Intel HD 4000 Graphics Soldered Corsair Vengeance  
Hard DriveOptical DriveOSMonitor
500GB DVD-+RW Windows 7 Professional 1600x900 
  hide details  
Reply
post #4 of 5
Thread Starter 
Ok I will try adding that header file in... Also the {} are ok.. If I were to add that one in it would be closing the while statement early as the if is part of the while thus the closing } at the very end, also would make the last one be closing nothing xD

The problem here is if I were to do this with in integer instead of a char it would work but for some reason as a char it wont recognise the change to 'A' and continues to loop as if it were still 'R'? Must be done as a char though as you can see it requires F and N

Edit: I tried doing it with a GOTO like this but still got the same odd loop problem... It is almost as if it is listening to the else even though it has used if or else if...

rLoop:
{
cout << "Enter book type (F=Fiction, N=Non-Fiction): ";
cin >> BookType;
BookType = toupper(BookType);
if (BookType == 'F')
{
BookTotalF = BookTotalF + 1;
BookTypeF = 1;
}
else if (BookType == 'N')
{
BookTotalN = BookTotalN + 1;
BookTypeN = 1;
}
else
cout << "\
Error, you may only enter F or N \
";
goto rLoop;
}
6625WD
(13 items)
 
  
CPUMotherboardGraphicsRAM
T7500 2.2GHz Core 2 Duo Intel 965 (Santa Rosa) 8600M GT 512MB GDDR3 2GB DDR2 800MHz Kingston 
Hard Drive
120GB SATA Hitachi 
  hide details  
Reply
6625WD
(13 items)
 
  
CPUMotherboardGraphicsRAM
T7500 2.2GHz Core 2 Duo Intel 965 (Santa Rosa) 8600M GT 512MB GDDR3 2GB DDR2 800MHz Kingston 
Hard Drive
120GB SATA Hitachi 
  hide details  
Reply
post #5 of 5
Thread Starter 
Heh nevermind.. Figured it out.. I forgot that if an else is longer then one line it requires its own set of {} -_- Thanks anyway though xD
6625WD
(13 items)
 
  
CPUMotherboardGraphicsRAM
T7500 2.2GHz Core 2 Duo Intel 965 (Santa Rosa) 8600M GT 512MB GDDR3 2GB DDR2 800MHz Kingston 
Hard Drive
120GB SATA Hitachi 
  hide details  
Reply
6625WD
(13 items)
 
  
CPUMotherboardGraphicsRAM
T7500 2.2GHz Core 2 Duo Intel 965 (Santa Rosa) 8600M GT 512MB GDDR3 2GB DDR2 800MHz Kingston 
Hard Drive
120GB SATA Hitachi 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › What's wrong with this code?