运行时的以下代码无法激活switch语句中的命令,也无法在}结束时阻止GoAgain,同时无法正常工作,询问用户是否要在退出之前退出。 GoAgain()函数本身可以正常工作,即使在其他函数中调用也是如此。 我也尝试让它在案例之外运行添加(即使我显然需要修复数学运算)并运行。 但是,只要它尝试运行switch语句,它就会将GoAgain设置为false并终止。 帮帮我!
// Fractions Project.cpp : Defines the entry point for the 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() { do { 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 for calculation: "; cin >> choice; switch (choice) // Switch function allows user to choose the calculation to 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 a = 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 a value for the numerator please ==> "; cin >> fraction.num; do { cout << "Enter a value for the denominator please ==> "; cin >> fraction.den; if (fraction.den == 0){ cout << "The denominator cannot be zero. " << endl; cout << "Enter a nonzero denominator." << endl; } // then } while (fraction.den == 0); }// Function SetFraction() // ========================== // ======================================== Fraction Multiply(Fraction f1, Fraction f2) { int a = 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 you like to 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 a = 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 a = 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 following code when run fails to activate the commands in the switch statement as well as prevents the GoAgain at the end of the } while to not work correctly in asking the user if they want to exit before it does so. The GoAgain() Function works correctly by itself and even when called in others. I also tried having it run the addition outside of the case(even though I clearly need to fix the math) and it runs. However as soon as it tries to run the switch statement it sets the GoAgain as false and terminates. Help!
// Fractions Project.cpp : Defines the entry point for the 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() { do { 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 for calculation: "; cin >> choice; switch (choice) // Switch function allows user to choose the calculation to 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 a = 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 a value for the numerator please ==> "; cin >> fraction.num; do { cout << "Enter a value for the denominator please ==> "; cin >> fraction.den; if (fraction.den == 0){ cout << "The denominator cannot be zero. " << endl; cout << "Enter a nonzero denominator." << endl; } // then } while (fraction.den == 0); }// Function SetFraction() // ========================== // ======================================== Fraction Multiply(Fraction f1, Fraction f2) { int a = 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 you like to 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 a = 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 a = 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() // =====================最满意答案
变量choice是一个整数,您将其视为一个字符。
这意味着当你这样做
cin >> choice然后输入函数需要一个整数,当它看到一个不是整数的东西时它会失败,并将choice设置为零。
如果你想读一个字符,那么choice必须是一个char 。 请注意,这将导致其他问题,因为cin >> choice将只读取一个字符,并将换行符保留在输入缓冲区中,并且您需要忽略该行的其余部分。
您还应该在switch语句中添加一个default大小写,以检测用户何时输入您不期望的内容。
你也不应该在案件中调用GoAgain 。
The variable choice is an integer, you treat it as a character.
That mean when you do
cin >> choicethen the input function expects an integer, and when it sees something which is not an integer then it will fail, and set choice to zero.
If you want to read a character, then choice have to be a char. Do note that this will lead to other problems, as then cin >> choice will read only a single character, and leave the newline in the input buffer, and you need to ignore the rest of the line.
You should also add a default case to your switch statement, to detect when the user inputs something which you did not expect.
You should also not call GoAgain in the cases.
更多推荐
发布评论