简体   繁体   English

我如何继续该功能而不退出c ++中的else语句

[英]how do I continue on the function without exiting after else statement in c++

Hello so my question is how do I continue on with new inputs after an else statement? 您好,所以我的问题是,在else语句之后如何继续输入新内容? In my program, I wrote an else statement that if the input is neither 1 nor 2, the user has to put a new value to get the result he/she wants. 在我的程序中,我编写了else语句,如果输入既不是1也不是2,则用户必须输入新值才能获得他/她想要的结果。 But after my else statement, my program shuts down. 但是在我的else语句之后,我的程序关闭了。 I don't know how to fix this. 我不知道该如何解决。 I'm very new to c++ so please keep harsh comments to yourself... Here's my code: 我是C ++的新手,所以请给自己一些苛刻的评论……这是我的代码:

// 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;
 } 


 }

So basically, after the else statement, I want my program to wait for the user to enter 1 or 2 instead of just shutting down. 所以基本上,在else语句之后,我希望我的程序等待用户输入1或2,而不仅仅是关闭。

do {
    cout << "Please input your choice now" << endl;
    cin  >> wordCode;
    cout << endl;
    writeProverb(wordCode);
}while (wordCode != 1 && wordCode != 2);

This code exits if user inputs 1 or 2. Stays otherwise. 如果用户输入1或2,则此代码退出。否则保留。

You want to have a do-while construct until you get a legal value, as Sakthi Kumar already pointed out. 正如Sakthi Kumar已经指出的那样,您希望在获得合法价值之前do-while一事。

However , you do not want to move the knowledge of what a legal value is up to main. 但是 ,您不想转移法律价值的主要知识。 Therefore, have the writeProverb method return if the value is legal or not. 因此,如果该值合法,则使writeProverb方法返回。 This keeps things in the proper abstraction level. 这样可以使事物保持在适当的抽象级别。 You should also consider updating the "menu" printing by using an object, thus tying everything together. 您还应该考虑通过使用对象来更新“菜单”打印,从而将所有内容捆绑在一起。

// ... 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;
}

This is basic logic, you should read about loops and how to use them. 这是基本逻辑,您应该阅读有关循环以及如何使用循环的知识。 This while loop continues while wordCode is not between 1 and 2 inclusive; 当wordCode 介于1和2之间时,此while循环继续进行; in other words, it continues until you get 1 or 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;
}

in this case,you can put the judging statement in the "main" function. 在这种情况下,您可以将判断语句放在“ main”函数中。 You can use a "while" statement to control the logic. 您可以使用“ while”语句来控制逻辑。

boolean flag = true;
while(flag){
    cout<<"input";
    cin>>wordCode;
    if(wordCode==1 || wordCode==2){
        flag=false;
        writeProverb(wordCode);
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM