Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 06-27-05   #1 (permalink)
PC Gamer
 
kris_cs1's Avatar
 
amd ati

Join Date: Apr 2005
Location: Chester, Cheshire
Posts: 218

Rep: 27 kris_cs1 is acknowledged by some
Unique Rep: 24
FAQs Submitted: 1
Trader Rating: 0
Default C++ Noob question

Right, I have decided that seeing as though it is the summers hols for me that i will go through my SAMs teach yourself book. I will probably to asking for a lot of help

And here is my first question. Why doesn't this work?

Code:

#include<iostream>
 
usingnamespace std;
 
int main(int argc, char* argv[])
 
{
 
int ReturnCode = 0;
 
float Dividend = 1;
 
cout << "Dividend: ";
 
cin >> Dividend;
 
if (!cin.fail())
 
{
 
float Divisor;
 
cout << "Divisor: ";
 
cin >> Divisor;
 
if (!cin.fail())
 
{
 
float Result = (Dividend/Divisor);
 
cout << Result << endl;
 
}
 
else
 
{
 
cerr << "Input error, not a number?" << endl;
 
cin.clear();
 
char BadInput[5];
 
ReturnCode = 1;
 
};
 
}
 
else
 
{
 
cerr << "Input error, not a number?" << endl;
 
cin.clear();
 
char BadInput[5];
 
ReturnCode = 1;
 
};
 
char StopCharacter;
 
cout << endl << "Press a key and \"Enter\": ";
 
cin >> StopCharacter;
 
return ReturnCode;
 
}
 
It is copied exactly from the book but the error catching routine doesn't work. It just closes on both Visual C++.net and Dev C++.
Thanks very much
Kris
__________________
Kris



System: My System
CPU
AMD Sempron 2800+
Motherboard
ASUS A7N8X-VM
Sound Card
5.1 SURROUND SOUND
Power Supply
350W
Case
Black
OS
Windows XP Home SP2
Monitor
19"
kris_cs1 is offline   Reply With Quote
Old 06-27-05   #2 (permalink)
Overclocker
 
Join Date: Dec 2004
Posts: 401

Rep: 65 Melraidin is acknowledged by some
Unique Rep: 48
FAQs Submitted: 4
Folding Team Rank: 217
Trader Rating: 1
Default

Quote:
Originally Posted by kris_cs1
Right, I have decided that seeing as though it is the summers hols for me that i will go through my SAMs teach yourself book. I will probably to asking for a lot of help

And here is my first question. Why doesn't this work?

[snip]

It is copied exactly from the book but the error catching routine doesn't work. It just closes on both Visual C++.net and Dev C++.
Thanks very much
Kris

As far as I can tell the error handler is running fine, except you don't have any form of pause to allow the user to view the error. I imagine either you missed a couple lines copying from the book, or the book needs to be updated (a common occurence, sadly).

Try adding a line such as "cin >> BadInput;" after the BadInput definition in each of the error handlers, this will work just like the StopCharacter lines towards the end; it will allow the user to view the error message and force the user to enter a character before closing the application.
Melraidin is offline I fold for Overclock.net   Reply With Quote
Old 06-27-05   #3 (permalink)
Overclocker
 
Jonrezz's Avatar
 
intel nvidia

Join Date: Dec 2004
Location: croton on hudson NY
Posts: 646

Rep: 40 Jonrezz is acknowledged by some
Unique Rep: 36
FAQs Submitted: 1
Folding Team Rank: 2238
Hardware Reviews: 1
Trader Rating: 0
Default

try adding this bit of code at the end of your main function:

system("PAUSE");
return EXIT_SUCCESS;

System: laptop
CPU
c2d t7200
Motherboard
sony laptop
Memory
2 gb ddr2
Graphics Card
7400 (best i could find for 13")
Hard Drive
160 gb 5400 rpm
Sound Card
integrated
Power Supply
sony laptop
Case
SZ model
OS
vista buisness
Monitor
13.3" glossy kind
Jonrezz is offline I fold for Overclock.net   Reply With Quote
Old 06-27-05   #4 (permalink)
PC Gamer
 
kris_cs1's Avatar
 
amd ati

Join Date: Apr 2005
Location: Chester, Cheshire
Posts: 218

Rep: 27 kris_cs1 is acknowledged by some
Unique Rep: 24
FAQs Submitted: 1
Trader Rating: 0
Default

Ah yes of course. I always hated this book lol.
Thanks very much both of you!
__________________
Kris



System: My System
CPU
AMD Sempron 2800+
Motherboard
ASUS A7N8X-VM
Sound Card
5.1 SURROUND SOUND
Power Supply
350W
Case
Black
OS
Windows XP Home SP2
Monitor
19"
kris_cs1 is offline   Reply With Quote
Old 06-27-05   #5 (permalink)
PC Gamer
 
kris_cs1's Avatar
 
amd ati

Join Date: Apr 2005
Location: Chester, Cheshire
Posts: 218

Rep: 27 kris_cs1 is acknowledged by some
Unique Rep: 24
FAQs Submitted: 1
Trader Rating: 0
Default

Well, I may as well ask so that I understand it.
Why is it that it doesn't stop anyway when running these lines:
Code:
 
cout << endl << "Press a key and \"Enter\": ";
cin >> StopCharacter;
If you put a 'cin >> BadInput' into the else blocks, the "Press a key and \"Enter\": " displays and waits for user input after it. Without the 'cin >> BadInput' however, it does not display. Why is it that it does not stop at 'cin >> StopCharacter;'? without the 'cin >> BadInput' in the else block? Also, why is it that with the 'cin >> BadInput' it seems to skip that and wait for user input in the StopCharacter variable. Surely it should stop and wait for the user to input something into the BadInput variable and then go on to prompting for the user to input a character into the StopCharacter variable, not just skip the first one.... am i making any sense?
__________________
Kris



System: My System
CPU
AMD Sempron 2800+
Motherboard
ASUS A7N8X-VM
Sound Card
5.1 SURROUND SOUND
Power Supply
350W
Case
Black
OS
Windows XP Home SP2
Monitor
19"

Last edited by kris_cs1 : 06-27-05 at 06:07 PM.
kris_cs1 is offline   Reply With Quote
Old 06-27-05   #6 (permalink)
Overclocker
 
Join Date: Dec 2004
Posts: 401

Rep: 65 Melraidin is acknowledged by some
Unique Rep: 48
FAQs Submitted: 4
Folding Team Rank: 217
Trader Rating: 1
Default

Quote:
Originally Posted by kris_cs1
Well, I may as well ask so that I understand it.
Why is it that it doesn't stop anyway when running these lines:
Code:
 
cout << endl << "Press a key and \"Enter\": ";
cin >> StopCharacter;
If you put a 'cin >> BadInput' into the else blocks, the "Press a key and \"Enter\": " displays and waits for user input after it. Without the 'cin >> BadInput' however, it does not display. Why is it that it does not stop at 'cin >> StopCharacter;'? without the 'cin >> BadInput' in the else block? Surely it shoud stop and wait for the user to input something into the BadInput variable and then go on to prompting for the user to input a character into the StopCharacter variable, not just skip the first one.... am i making any sense?
Haven't got time to test the code, but possibly the input buffer still has the invalid input inside? Try flushing the buffer (I believe it's cin.clear(), but not positive) before each input.
Melraidin is offline I fold for Overclock.net   Reply With Quote
Old 06-27-05   #7 (permalink)
PC Gamer
 
kris_cs1's Avatar
 
amd ati

Join Date: Apr 2005
Location: Chester, Cheshire
Posts: 218

Rep: 27 kris_cs1 is acknowledged by some
Unique Rep: 24
FAQs Submitted: 1
Trader Rating: 0
Default

Quote:
Originally Posted by Melraidin
Haven't got time to test the code, but possibly the input buffer still has the invalid input inside? Try flushing the buffer (I believe it's cin.clear(), but not positive) before each input.
thanks but cin.clear() is used before the BadInput prompt but it just seems to skip past it and go the the StopCharacter prompt still.
__________________
Kris



System: My System
CPU
AMD Sempron 2800+
Motherboard
ASUS A7N8X-VM
Sound Card
5.1 SURROUND SOUND
Power Supply
350W
Case
Black
OS
Windows XP Home SP2
Monitor
19"
kris_cs1 is offline   Reply With Quote
Old 06-28-05   #8 (permalink)
Programmer
 
MrSmiley's Avatar
 
amd nvidia

Join Date: Oct 2004
Location: Manhattan, Kansas
Posts: 1,060

Rep: 72 MrSmiley is acknowledged by some
Unique Rep: 57
FAQs Submitted: 1
Folding Team Rank: 747
Hardware Reviews: 1
Trader Rating: 0
Default

Quote:
Originally Posted by kris_cs1
Well, I may as well ask so that I understand it.
Why is it that it doesn't stop anyway when running these lines:
Code:
 
  cout << endl << "Press a key and \"Enter\": ";
  cin >> StopCharacter;
Ignore previous made point... dude, endl clears the current outstream buffer. To do something like that you would have to do
Code:
 cout << endl;
 cout << "Press any key\n";
 cin >> StopChar;
__________________
System: My System
CPU
Intel 2.8GHz
Motherboard
ESG smth
Power Supply
Antec TruePower 350Watt
OS
FreeBSD

Last edited by MrSmiley : 06-28-05 at 01:34 AM.
MrSmiley is offline I fold for Overclock.net   Reply With Quote
Old 06-28-05   #9 (permalink)
Overclocker
 
Join Date: Dec 2004
Posts: 401

Rep: 65 Melraidin is acknowledged by some
Unique Rep: 48
FAQs Submitted: 4
Folding Team Rank: 217
Trader Rating: 1
Default

Quote:
Originally Posted by kris_cs1
thanks but cin.clear() is used before the BadInput prompt but it just seems to skip past it and go the the StopCharacter prompt still.
I'll have to try loading this up in VS tomorrow, but have you actually tried adding the cin.clear () statements before each input?
Melraidin is offline I fold for Overclock.net   Reply With Quote
Old 06-28-05   #10 (permalink)
PC Gamer
 
kris_cs1's Avatar
 
amd ati

Join Date: Apr 2005
Location: Chester, Cheshire
Posts: 218

Rep: 27 kris_cs1 is acknowledged by some
Unique Rep: 24
FAQs Submitted: 1
Trader Rating: 0
Default

Quote:
Originally Posted by MrSmiley
Ignore previous made point... dude, endl clears the current outstream buffer. To do something like that you would have to do
Code:
cout << endl;
cout << "Press any key\n";
cin >> StopChar;
Oh, well thats what it says in the book... i just thought it moved onto the next line....

Quote:
Originally Posted by Melraidin
I'll have to try loading this up in VS tomorrow, but have you actually tried adding the cin.clear () statements before each input?
Yep... didn't seem to make a difference sorry
__________________
Kris



System: My System
CPU
AMD Sempron 2800+
Motherboard
ASUS A7N8X-VM
Sound Card
5.1 SURROUND SOUND
Power Supply
350W
Case
Black
OS
Windows XP Home SP2
Monitor
19"
kris_cs1 is offline   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 08:06 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.32394 seconds with 8 queries