简体   繁体   中英

Loops in C++ (while)

I need to write a program in C++ where the user can do some computation, and when the computation is done, the program will ask the user if he wants to do another computation. I know how to write this in Python:

more = "y"
while (more == "y"):
    // computation code
    print "Do you want to do another computation? y/n "
    more = input()

I created a char variable and used it in the head of the loop, but I don't know how to implement the "input" function in C++. I tried using the cin.get(char_variable) function but the program seems to skip it entirely.

You can use cin >> char_variable; to get the value.
Don't forget:

#include <iostream>
using namespace std;

You can use a do-while loop, which basically runs the loop at least once. It runs, then checks the conditional, unlike the plain while loop which checks the conditional then runs. Example:

bool playAgain; //conditional of the do-while loop
char more; //Choice to play again: 'y' or 'n'


string input; /*In this example program, they enter
                their name, and it outputs "Hello, name" */
do{

    //input/output
    cout << "Enter your name: ";
    cin >> input;
    cout << "Hello, " << input << endl << endl;


    //play again input
    cout << "Do you want to play again?(y/n): ";
    cin >> more;
    cout << endl;


    //Sets bool playAgain to either true or false depending on their choice
    if (more == 'y')
        playAgain = true;
    else if (more == 'n')
        playAgain = false;

    //You can add validation here as well (if they enter something else)


} while (playAgain == true); //if they want to play again then run the loop again

I wrote it this way. I used cin for get char value from user.

#include <iostream>
using namespace std ;
int main ()
{
    char more;
    cout<<"Do you want to do another computation? y/n ";
    cin>>more;
    while(more=='y'){
        cout<<"Do you want to do another computation? y/n ";
        cin>>more;
    }
}

If you used cin.get, you probably had the following problem. If you run this code:

cout << "give a number:";
int n;
cin >> n;
cout << "give a char:";
char c;
cin.get(c);
cout << "number=" << n << endl;

you'll get the scenario

give a number:12
give a char:number=12

where it seems the cin.get() was ignored. In fact it was not. It read the "end-of-line" you typed after the number. You'll see it by adding

cout << "char code=" << (int) c << endl;

to see the numeric value of the char.

--- you'd better use cin >> c; instead because it waits for the first non-white char.

Your program is quite simple & easy. It goes like this :-

#include <iostream>
using namespace std;
int main()
{
    char ch='y';
    do
    {
        // compute....
        cout<<"do you want to continue ?: "<<flush;
        cin>>ch;
    }while (ch=='y' || ch=='Y');   // don't for get the semicolon
    return 0;
}

If you want to use while loop instead of do while , your code will be :-

#include <iostream>
using namespace std;
int main()
{
    char ch='y';
    while (ch=='y' || ch=='Y')
    {
        // compute....
        cout<<"do you want to continue ?: "<<flush;
        cin>>ch;
    }    // no semicolon needed as in `do while`
    return 0;
}

Simple ! Hope your code works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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