In regards to the last program, trying to get it to quit going intoan inevitable
ID: 3619165 • Letter: I
Question
In regards to the last program, trying to get it to quit going intoan inevitable loop when something other then a number is entered.For instance, I entered the letter A, and it sent it for atailspin.Explanation / Answer
x.P5lor="red">please rate - thanks why are you allowing a negative numerator and not a negative denominator? it's the same difference if you were entering just 1 character it could be done with a cin.clear() cin.ignore(100,' '); but because your not, you must read the data as characters (I used a string) and then convert it to a number get back to me if any problems #include #include #include #include using namespace std; //prototype of the functions bool getnum(int&); char menu(); void addFractions( int a1, int b1, int a2, int b2, int & num, int & den ); void subtractFractions( int a1, int b1, int a2, int b2, int & num, int & den ); void multiplyFractions( int a1, int b1, int a2, int b2, int & num, int & den ); void divideFractions( int a1, int b1, int a2, int b2, int & num, int & den ); int main() // function main begins program execution { int a1, b1, a2, b2, num, den, exit; bool good1=false,good2=false,num1found=false,num2found=false,inputerror; char ch = menu(); while (ch!='X') { //Try block checks for input validation //throws if exception occured do { try {if(!good1) {if(!num1found) {// prompt and read the input from the user cout > a1; inputerror=getnum(a1); if(inputerror) { string s="valid number not entered"; throw s; } num1found=true; } // prompt and read the input from the user cout > b1; inputerror=getnum(b1); if(inputerror) { string s="valid number not entered"; throw s; } // validate the denominator if ( b1 == 0 ) throw b1; else if (b1 a2; inputerror=getnum(a2); if(inputerror) { string s="valid number not entered"; throw s; } num2found=true; } // prompt and read input from the user cout > b2; inputerror=getnum(b2); if(inputerror) { string s="valid number not entered"; throw s; } // validate the denominator if ( b2 == 0 ) throw b2; else if (b2Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.