zayid

June 12th, 2012, 08:27 PM

Hello guys, pls im supposed to create a c++ program to calculate two input numbers. I'm also expected to add error handling but it keeps giving me errors. i have commented on it so you can have an idea what i'm trying to do. If i run the program straight it works, but if i'm to use an alphabet for either numerator or denominator it fails. thanks

//division calculator with error handling

#include <iostream>

using namespace std;

int main(int argc, char* argv[])

{

int returnvalue;

float numerator = 0;

float denominator = 0;

float divresult = 0;

cout <<"Please enter the numerator:"; //the numerator is put in

cin >>numerator;

if(cin.fail()) //if the value put in is not a number (0-9), it fails

{

cerr<<"Invalid Input\nNumbers required!"<<endl;//information regarding the error and advising for input change

cin.clear();

char discardit[3];

cin>>discardit;

char exitchar;

cout <<"please enter the numerator:";

cin >>numerator;

}

else// but if the numerator is correct (0-9), it moves here

{

cout<<"Please enter the denominator:";

cin>>denominator;

}

if(cin.fail()) //if the denominator is not a number(0-9), it fails

{

cerr<<"Invalid Input\nNumbers required!"<<endl;//information regarding the error and advising for input change

cin.clear();

char discardit[3];

cin>>discardit;

char exitchar;

cout<<"Please enter the denominator:";

cin>>denominator;

}

else// but if the denominator is correct then...

if(denominator==0)//...it tests to make sure the input is not zero

{

cout<<"Cannot divide by zero"<<endl;

cout<<"please enter the denominator:";

cin>>denominator;

}

else // but if it isn't zero, it moves here and does the division

divresult = (numerator/denominator);

cout<<divresult<<endl;

return returnvalue;// returns the result.

}//end of program

//division calculator with error handling

#include <iostream>

using namespace std;

int main(int argc, char* argv[])

{

int returnvalue;

float numerator = 0;

float denominator = 0;

float divresult = 0;

cout <<"Please enter the numerator:"; //the numerator is put in

cin >>numerator;

if(cin.fail()) //if the value put in is not a number (0-9), it fails

{

cerr<<"Invalid Input\nNumbers required!"<<endl;//information regarding the error and advising for input change

cin.clear();

char discardit[3];

cin>>discardit;

char exitchar;

cout <<"please enter the numerator:";

cin >>numerator;

}

else// but if the numerator is correct (0-9), it moves here

{

cout<<"Please enter the denominator:";

cin>>denominator;

}

if(cin.fail()) //if the denominator is not a number(0-9), it fails

{

cerr<<"Invalid Input\nNumbers required!"<<endl;//information regarding the error and advising for input change

cin.clear();

char discardit[3];

cin>>discardit;

char exitchar;

cout<<"Please enter the denominator:";

cin>>denominator;

}

else// but if the denominator is correct then...

if(denominator==0)//...it tests to make sure the input is not zero

{

cout<<"Cannot divide by zero"<<endl;

cout<<"please enter the denominator:";

cin>>denominator;

}

else // but if it isn't zero, it moves here and does the division

divresult = (numerator/denominator);

cout<<divresult<<endl;

return returnvalue;// returns the result.

}//end of program