Originally Posted by
manasij7479
Use code tags !
Sorry
pls you should first understand that i started learning programming about 2 months ago so i'm very much a newbie lol, also, i don't get what you're saying(R41D3N) - but it is insisted i follow this format. i have refined it a bit but i'm having problem if i use an alphabet as numerator, then it'll be blank but the compiler is waiting for the denominator, how do i get that info to show? also if at the first instance i use an alphabet in the denominator, even if it does the division, the answer will be zero(that means it has reset my numerator to 0) thanks
Code:
//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\nUse only Numbers"<<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\nUse only Numbers"<<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;
}
// but if the denominator is correct then...
else 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;
returnvalue = 0;
return returnvalue;// returns the result.
}//end of program