Curt
apostate
- Joined
- Feb 2, 2004
- Posts
- 7,710
I've written a simple 4 function calculator for my Computer Science course, and it's pretty ok. But one of the requirements is that it shouldn't crash if a user inputs a character or word instead of an integer.
here's a piece of the code for division prototype: ( I figured out how to not crash if it divides by 0...pretty easy)
I know I need to do either a if statement, a while loop, or even a throw/catch, right after the cin>> of a character... but how do I tell it that?
Any help would be greatly appreciated.
Here's the .cpp file
http://userwww.sfsu.edu/~curtwrx/csc205/STAYONTARGET.cpp
and the compiled exe
http://userwww.sfsu.edu/~curtwrx/csc205/STAYONTARGET.exe
here's a piece of the code for division prototype: ( I figured out how to not crash if it divides by 0...pretty easy)
Code:
void Division (void)
{
float divint1,divint2,divans;
cout<<"What is the dividend";
cin >>divint1;
cout<<"What is the divisor?";
cin >>divint2;
if (divint2==0)
{
cout<<"Do not divide by zero you dolt!";
}
divans=divint1/divint2;
if (divint1 < divint2)
{
cout<<"Your first number must be larger than your second.";
}
cout<<"\n"<<endl;
cout<<divint1<<"/"<<divint2<<"="<<divans<<"."<<endl;
cout<<"\n"<<endl;
}
I know I need to do either a if statement, a while loop, or even a throw/catch, right after the cin>> of a character... but how do I tell it that?
Any help would be greatly appreciated.
Here's the .cpp file
http://userwww.sfsu.edu/~curtwrx/csc205/STAYONTARGET.cpp
and the compiled exe
http://userwww.sfsu.edu/~curtwrx/csc205/STAYONTARGET.exe
Last edited:

