c++ - switch case being used in a do{}while statement causes the GoAgain to not work properly -
the following code when run fails activate commands in switch statement prevents goagain @ end of } while not work correctly in asking user if want exit before so. goagain() function works correctly , when called in others. tried having run addition outside of case(even though need fix math) , runs. tries run switch statement sets goagain false , terminates. help!
// fractions project.cpp : defines entry point console application. // #include "stdafx.h" #include <iostream> #include <string> using namespace std; // ================= struct fraction { int num; int den; }; // structure fraction // ===================== // ================== // funtion prototypes // ================================= fraction add(fraction, fraction); fraction multiply(fraction, fraction); fraction subtract(fraction, fraction); fraction divide(fraction, fraction); void printfraction(string, fraction); void setfraction(fraction&); bool goagain(); // ================================ // ============ int main() { { int choice; fraction fa, fb, fc; setfraction(fa); setfraction(fb); printfraction("fa", fa); printfraction("fb", fb); add(fa, fb); printfraction("fa", fa); printfraction("fb", fb); fc = add(fa, fb); printfraction("fa + fb ", fc); cout << endl << endl << "table of operations" << endl; cout << "a -- addition" << endl; cout << "b -- subtraction" << endl; cout << "c -- multiplication" << endl; cout << "d -- division" << endl << endl; cout << "enter choice calculation: "; cin >> choice; switch (choice) // switch function allows user choose calculation execute { case 'a': case 'a': { add(fa, fb); printfraction("fa", fa); printfraction("fb", fb); fc = add(fa, fb); printfraction("fa + fb ", fc); } break; case 'b': case 'b': subtract(fa, fb); printfraction("fa", fa); printfraction("fb", fb); fc = subtract(fa, fb); printfraction("fa - fb ", fc); goagain(); break; case 'c': case 'c': multiply(fa, fb); printfraction("fa", fa); printfraction("fb", fb); fc = multiply(fa, fb); printfraction("fa x fb ", fc); goagain(); break; case 'd': case 'd': divide(fa, fb); printfraction("fa", fa); printfraction("fb", fb); fc = divide(fa, fb); printfraction("fa / fb ", fc); goagain(); break; } } while (goagain()); return 0; }// function main() // =================== // ======================================== fraction add(fraction f1, fraction f2) { int = f1.num, b = f1.den; int c = f2.num, d = f2.den; fraction fsum; fsum.num = a*d + b*c; fsum.den = b*d; return fsum; }// function add() // ================== // =========================================================== void printfraction(string fractionname, fraction fraction) { cout << fractionname << " = "; cout << fraction.num << "/" << fraction.den << endl; }// function printfraction() // ============================ // ====================================== void setfraction(fraction& fraction) { cout << "enter value numerator please ==> "; cin >> fraction.num; { cout << "enter value denominator please ==> "; cin >> fraction.den; if (fraction.den == 0){ cout << "the denominator cannot zero. " << endl; cout << "enter nonzero denominator." << endl; } // } while (fraction.den == 0); }// function setfraction() // ========================== // ======================================== fraction multiply(fraction f1, fraction f2) { int = f1.num, b = f1.den; int c = f2.num, d = f2.den; fraction fsum; fsum.num = a*d; fsum.den = b*c; return fsum; }// function multiply() // ======================= // =============== bool goagain(){ // =============== char answer; cout << endl << "would try again? (y/n)"; cin >> answer; cout << endl; if (answer == 'y' | answer == 'y') return true; else return false; }// function goagain() // ====================== // ============================================= fraction subtract(fraction f1, fraction f2) { int = f1.num, b = f1.den; int c = f2.num, d = f2.den; fraction fsum; fsum.num = a*d - b*c; fsum.den = b*d; return fsum; }// function subtract() // ======================= // =========================================== fraction divide(fraction f1, fraction f2) { int = f1.num, b = f1.den; int c = f2.num, d = f2.den; fraction fsum; fsum.num = (a*d) / (b*c); fsum.den = b*d; return fsum; }// function divide() // =====================
the variable choice
integer, treat character.
that mean when do
cin >> choice
then input function expects integer, , when sees not integer fail, , set choice
zero.
if want read character, choice
have char
. note lead other problems, cin >> choice
read single character, , leave newline in input buffer, , need ignore rest of line.
you should add default
case switch
statement, detect when user inputs did not expect.
you should not call goagain
in cases.
Comments
Post a Comment