C++ 如何在代码中正确使用char和void原型?
我在这里的目标是让用户在程序错误检查输入是否正确之前选择一个操作数符号,并传递一条关于将询问什么类型的算术问题的消息。我真的不明白如何使用char和void原型。这是为了一个任务,在这个任务中,我必须使用这些特定的函数并从中调用这些特定的操作 以下是我需要使用的原型:C++ 如何在代码中正确使用char和void原型?,c++,C++,我在这里的目标是让用户在程序错误检查输入是否正确之前选择一个操作数符号,并传递一条关于将询问什么类型的算术问题的消息。我真的不明白如何使用char和void原型。这是为了一个任务,在这个任务中,我必须使用这些特定的函数并从中调用这些特定的操作 以下是我需要使用的原型: void PrintMenu (); char GetMenuChoice (); void TellUserAboutOp (char i); 这是我在int main()中的内容: 到目前为止,函数是这样的: void Pr
void PrintMenu ();
char GetMenuChoice ();
void TellUserAboutOp (char i);
这是我在int main()中的内容:
到目前为止,函数是这样的:
void PrintMenu ()
{
cout << "What operation would you like to practice?" << endl
<< "Please type the character giving your choice after the small arrow: "
<< endl << "\t + for addition" << endl << "\t - for subtraction" << endl
<< "\t * for multiplication" << endl << "\t / for division -> ";
}
char GetMenuChoice ()
{
char chosenOperand;
cin >> chosenOperand;
while ((chosenOperand != '+') || (chosenOperand != '-') ||
(chosenOperand != '*') || (chosenOperand != '/'));
{
cout << "That's not a valid character for your choice. Please try again!"
<< endl << "Type in the character of your choice -> ";
cin >> chosenOperand;
}
return (chosenOperand);
}
void TellUserAboutOp (char i)
{
if
(i == '+')
cout << "OK, let's try 7 different questions to practice addition.";
else if
(i == '-')
cout << "OK, let's try 7 different questions to practice subtraction.";
else if
(i == '*')
cout << "OK, let's try 7 different questions to practice multiplication.";
else if
(i == '/')
cout << "OK, le's try 7 different questions to practice division.";
return;
}
调用
GetMenuChoice
时,它返回一个char,您希望将其存储在操作数选项中。返回非void类型的函数可以像该类型的变量一样使用。因此:
char operandChoice = GetMenuChoice();
将编译并清除未初始化变量警告,同时执行所需操作。我确信您的赋值是为了理解函数如何返回您可以在赋值和其他类似表达式中使用的值。您没有显示
main
的全部代码,但您似乎定义了一个局部变量操作数选择
,在使用它之前没有给出任何有用的值。你可能想要一条像这样的线
operandChoice = GetMenuChoice();
在你主要的地方。根据其原型,GetMenuChoice
返回一个char
,这意味着您可以将函数的结果分配给char
变量。您还可以在通过写入声明操作数选项的位置初始化操作数选项
char operandChoice = GetMenuChoice();
函数的原型告诉您它的签名。第一件事(在函数名之前)是返回类型void
是一种特殊的返回类型,表示函数实际上不返回任何内容。因此,您对其他函数的使用是正确的。执行FunctionName(一些参数)
只执行函数。您可以对返回char
(或其他内容)的函数执行相同的操作,但它会丢弃函数返回的结果
附加提示:我认为您在GetMenuChoice
中的条件下有问题。chosenOperand
变量只能等于其中一个操作数字符。因此,至少有三个比较结果始终为真,因此您永远不会退出while
循环。您可能希望以不同于| |
(表示“或”)的方式组合每个运算符的检查
还有一个代码>在while循环之后。这只会创建一个无休止的while
循环,因为由{
,}
包围的以下块将不被视为循环体。因为代码>,while循环的主体为空。修复代码,完成任务
更改了GetMenuChoice();在int main()中,执行以下操作:
在循环运行时修复:
void PrintMenu ()
{
cout << "What operation would you like to practice?" << endl
<< "Please type the character giving your choice after the small arrow: "
<< endl << "\t + for addition" << endl << "\t - for subtraction" << endl
<< "\t * for multiplication" << endl << "\t / for division -> ";
}
char GetMenuChoice ()
{
char chosenOperand;
cin >> chosenOperand;
while
((chosenOperand != '+') && (chosenOperand != '-')
&& (chosenOperand != '*') && (chosenOperand != '/'))
{
cout << "That's not a valid character for your choice. Please try again!" << endl
<< "Type in the character of your choice -> ";
cin >> chosenOperand;
}
return (chosenOperand);
}
void TellUserAboutOp (char i)
{
if
(i == '+')
cout << "OK, let's try 7 different questions to practice addition." << endl;
else if
(i == '-')
cout << "OK, let's try 7 different questions to practice subtraction." << endl;
else if
(i == '*')
cout << "OK, let's try 7 different questions to practice multiplication." << endl;
else if
(i == '/')
cout << "OK, le's try 7 different questions to practice division." << endl;
return;
}
void打印菜单()
{
你难道不能“更新”掉我们正在更正答案的那一行吗?改变问题的关键部分从而使答案无效是没有意义的(除非你在复制粘贴过程中出错)。如果答案给您留下了另一个问题,您可以对答案进行评论,在原始问题的末尾添加一些内容,或者发布一个新问题。现在您的问题中甚至没有问题(“没有编译错误或警告”,那么您为什么要问!)。错误是什么?回滚更改,因为它们使发布的答案无效。我不理解您的编辑。所以一切都正常?为什么您将while
更改为if
语句?while
循环的目的是(我猜)一次又一次地从用户那里获取一个字符,直到它与其中一个操作符匹配。if
语句只会检查一次,然后不管发生什么情况都会继续。很抱歉一直打扰你,但从你的声誉来看,我看到你是新来的,所以我想这里的一些信息可能会有所帮助:你发布的正是这两个我在回答中描述的事情。你为什么发布你自己的答案?请注意,虽然通常鼓励你在上面发布你自己的答案,但如果你自己发现了问题,复制另一个答案,说“这很有效”这不是解决问题的方法。相反,你接受解决问题的答案。我曾看到其他人这样做,我认为这是协议。我真诚地尝试做正确的事情。我希望我所要求的内容准确无误,并希望有一个易于阅读的答案,供未来的任何人使用。感谢大家的提醒。我不会这样做再做一次。
char operandChoice = GetMenuChoice();
char operandChoice = GetMenuChoice();
void PrintMenu ()
{
cout << "What operation would you like to practice?" << endl
<< "Please type the character giving your choice after the small arrow: "
<< endl << "\t + for addition" << endl << "\t - for subtraction" << endl
<< "\t * for multiplication" << endl << "\t / for division -> ";
}
char GetMenuChoice ()
{
char chosenOperand;
cin >> chosenOperand;
while
((chosenOperand != '+') && (chosenOperand != '-')
&& (chosenOperand != '*') && (chosenOperand != '/'))
{
cout << "That's not a valid character for your choice. Please try again!" << endl
<< "Type in the character of your choice -> ";
cin >> chosenOperand;
}
return (chosenOperand);
}
void TellUserAboutOp (char i)
{
if
(i == '+')
cout << "OK, let's try 7 different questions to practice addition." << endl;
else if
(i == '-')
cout << "OK, let's try 7 different questions to practice subtraction." << endl;
else if
(i == '*')
cout << "OK, let's try 7 different questions to practice multiplication." << endl;
else if
(i == '/')
cout << "OK, le's try 7 different questions to practice division." << endl;
return;
}