C++ 如何在不退出c+;中的after else语句的情况下继续函数+;
您好,我的问题是,在else语句之后,如何继续使用新输入?在我的程序中,我写了一条else语句,如果输入既不是1也不是2,用户必须输入一个新值才能得到他/她想要的结果。但是在我的else语句之后,我的程序关闭了。我不知道怎么解决这个问题。我对C++非常陌生,所以请对自己严加评论… 这是我的密码:C++ 如何在不退出c+;中的after else语句的情况下继续函数+;,c++,C++,您好,我的问题是,在else语句之后,如何继续使用新输入?在我的程序中,我写了一条else语句,如果输入既不是1也不是2,用户必须输入一个新值才能得到他/她想要的结果。但是在我的else语句之后,我的程序关闭了。我不知道怎么解决这个问题。我对C++非常陌生,所以请对自己严加评论… 这是我的密码: // This program will allow the user to input from the keyboard // whether the last word to the follo
// This program will allow the user to input from the keyboard
// whether the last word to the following proverb should be party or country:
// "Now is the time for all good men to come to the aid of their _______"
// Inputting a 1 will use the word party. A 2 will use the word country.
#include <iostream>
#include <string>
using namespace std;
void writeProverb(int);
int main ()
{
int wordCode;
cout << "Given the phrase:" << endl;
cout << "Now is the time for all good men to come to the aid of their ___" << endl;
cout << "Input a 1 if you want the sentence to be finished with party" << endl;
cout << "Input a 2 if you want the sentence to be finished with country." << endl;
cout << "Please input your choice now" << endl;
cin >> wordCode;
cout << endl;
writeProverb(wordCode);
return 0;
}
void writeProverb (int number)
{
if (number == 1)
cout << "Now is the time for all good men to come to the aid of their party." << endl;
else if (number == 2)
cout << "Now is the time for all good men to come to the aid of their country." << endl;
else
{
cout << "I'm sorry but that is an incorrect choice: Please input a 1 or 2" << endl;
}
}
//此程序允许用户从键盘输入
//以下谚语的最后一句话是“党”还是“国家”:
//“现在是所有好人帮助他们的时候了”
//输入1将使用party这个词。A 2将使用“国家”一词。
#包括
#包括
使用名称空间std;
无效冲销(整数);
int main()
{
int字码;
cout这是基本逻辑,您应该了解循环以及如何使用循环。当wordCode不在1和2之间(包括1和2)时,循环将继续,换句话说,它将继续,直到您得到1或2为止
int main ()
{
int wordCode;
cout << "Given the phrase:" << endl;
cout << "Now is the time for all good men to come to the aid of their ___" << endl;
cout << "Input a 1 if you want the sentence to be finished with party" << endl;
cout << "Input a 2 if you want the sentence to be finished with country." << endl;
do
{
cout << "Please input your choice now 1 or 2: " << endl;
cin >> wordCode;
} while(!(1 <= wordCode && wordCode <=2));
cout <<endl;
writeProverb(wordCode);
return 0;
}
int main()
{
int字码;
cout这是基本逻辑,您应该了解循环以及如何使用循环。当wordCode不在1和2之间(包括1和2)时,循环将继续,换句话说,它将继续,直到您得到1或2为止
int main ()
{
int wordCode;
cout << "Given the phrase:" << endl;
cout << "Now is the time for all good men to come to the aid of their ___" << endl;
cout << "Input a 1 if you want the sentence to be finished with party" << endl;
cout << "Input a 2 if you want the sentence to be finished with country." << endl;
do
{
cout << "Please input your choice now 1 or 2: " << endl;
cin >> wordCode;
} while(!(1 <= wordCode && wordCode <=2));
cout <<endl;
writeProverb(wordCode);
return 0;
}
int main()
{
int字码;
不能do{
密码;
不能do{
密码;
正如Sakthi Kumar已经指出的那样,在获得法律价值之前,您是否希望有一个do while
构造
但是,你不想转移对法律价值的认识,因此,如果价值合法与否,《<代码> >写作谚语< /C> >方法返回。这将使事物保持在适当的抽象层次。你还应该考虑通过使用一个对象来更新“菜单”打印,从而将所有东西捆绑在一起。
// ... in main()
do {
cout << "Please input your choice now" << endl;
cin >> wordCode;
cout << endl;
writeProverb(wordCode);
} while( !writeProverbIfLegalNumber(wordCode) );
}
bool writeProverbIfLegalNumber (int number) {
if (number == 1) {
cout << "Now is the time for all good men to come to the aid of their party." << endl;
return true;
}
else if (number == 2) {
cout << "Now is the time for all good men to come to the aid of their country." << endl;
return true;
}
else
{
cout << "I'm sorry but that is an incorrect choice: Please input a 1 or 2" << endl;
}
return false;
}
/…在main()中
做{
密码;
正如Sakthi Kumar已经指出的那样,在获得法律价值之前,您是否希望有一个do while
构造
但是,你不想转移对法律价值的认识,因此,如果价值合法与否,《<代码> >写作谚语< /C> >方法返回。这将使事物保持在适当的抽象层次。你还应该考虑通过使用一个对象来更新“菜单”打印,从而将所有东西捆绑在一起。
// ... in main()
do {
cout << "Please input your choice now" << endl;
cin >> wordCode;
cout << endl;
writeProverb(wordCode);
} while( !writeProverbIfLegalNumber(wordCode) );
}
bool writeProverbIfLegalNumber (int number) {
if (number == 1) {
cout << "Now is the time for all good men to come to the aid of their party." << endl;
return true;
}
else if (number == 2) {
cout << "Now is the time for all good men to come to the aid of their country." << endl;
return true;
}
else
{
cout << "I'm sorry but that is an incorrect choice: Please input a 1 or 2" << endl;
}
return false;
}
/…在main()中
做{
密码;
cout在这种情况下,您可以将判断语句放在“main”函数中。您可以使用“while”语句来控制逻辑
boolean flag = true;
while(flag){
cout<<"input";
cin>>wordCode;
if(wordCode==1 || wordCode==2){
flag=false;
writeProverb(wordCode);
}
}
boolean标志=true;
while(旗帜){
密码;
if(wordCode==1 | | wordCode==2){
flag=false;
writeProverb(wordCode);
}
}
在这种情况下,您可以将判断语句放在“main”函数中。您可以使用“while”语句来控制逻辑
boolean flag = true;
while(flag){
cout<<"input";
cin>>wordCode;
if(wordCode==1 || wordCode==2){
flag=false;
writeProverb(wordCode);
}
}
boolean标志=true;
while(旗帜){
密码;
if(wordCode==1 | | wordCode==2){
flag=false;
writeProverb(wordCode);
}
}
发布前格式化您的代码。下次会更加小心。谢谢您的建议。发布前格式化您的代码。下次会更加小心。谢谢您的建议。我不确定他们是否真的希望程序无限期地继续。从技术上讲,他们可以关闭命令行,但通常会有一个选项来bre完成循环并退出,就像上面Sakthi Kumar的回答一样。我知道这一点,但我认为有人只是想让他/她的作业免费完成。你不能通过给他/她解决方案来教他/她思考。谢谢mcl,但我不是想让这个作业免费完成。我已经试着自己做了两个小时了,就像我一样aid,我对编程基本上是新手。虽然我读过关于循环的书,但我仍然不熟悉何时应用它。显然,我需要更多的练习。尽管如此,我感谢您的帮助。您可能还希望在main()中的返回0;之前加一个getch()函数。该函数将在程序完成时阻止控制台窗口自动关闭。#包含用于getch();@weathergirlgetch
是getcharacter,iirc的缩写。它从标准输入中读取一个字符。因此,如果你在程序结束时调用getch
,它将等待输入的一个字符,而你的程序不会立即结束。我不确定他们是否真的希望程序无限期地继续。从技术上讲他们可以关闭命令行,但通常有一个打破循环并退出的选项,就像上面Sakthi Kumar的回答一样。我知道这一点,但我认为有人只是想让他/她的作业免费完成。你不能通过给他/她解决方案来教他/她思考。谢谢mcl,但我不是想让他/她完成这个作业r免费。我已经试着自己解决了2个小时了,就像我说的,我对编程基本上是新手。虽然我读过关于循环的书,但我仍然不熟悉何时应用它。显然我需要更多的练习。不过我感谢你的帮助。你可能还想在返回0;之前放一个getch();在main()中函数。该函数将在程序完成时阻止控制台窗口自动关闭。#包含用于getch();@weathergirlgetch
是getcharacter,iirc的缩写。它从标准输入中读取一个字符。因此,如果您在程序结束时调用getch
,它将等待输入的一个字符,并且您的程序不会立即结束。我不确定一个对象是否是一个好主意,因为这似乎是基本的入门级内容.走得太远太快可能会引起混乱。我喜欢在WriteOverb
中保留“这是否合法?”的决定,但是,+1。Computerfreaker是对的,这是一个非常基本的入门级内容…还没有真正了解布尔值,但仍然感谢您的帮助