Question:
How to create a yes/no loop in C++?
Triangles
2012-06-25 10:55:23 UTC
So currently I have a program that lets the user input 2 point values, and the program will calculate the distance between the two points.
How do I make it so that once the program is done executing, it'll ask if the user wants to go through the program again, and the user can answer with Yes or No or anything that starts with Y or anything that starts with N (I believe I'll just use a cin.ignore(INT_MAX, '\n') thing for that?)? And if the user answers yes, it'll go again, but if the user answers no, it'll display a thank you message?
I've been reading around, and some say to use a "while" statement??
I'm really new to c++. Please help? Haha thanks!
Six answers:
?
2012-06-25 11:40:37 UTC
EDIT:



@cronin, What do you mean i am sending a bool? I think it is you who does not know what the are talking about. Look at the sample run i provided (note, i made a typo earlier, whoopty-doo, it happens.. the error is since fixed)



This is what the user asked



"it'll ask if the user wants to go through the program again, and the user can answer with Yes or No or anything that starts with Y or anything that starts with N"



Basically she wants anything that starts with "Y or N', better known as a char



Now i agree, you CAN use a string, but the user stated they are new to C++, so why should you confuse them early on? Using char instead of string is more straight forward



Please dont reply unless you have something meaningful to bring to the discussion, your "act" is getting tiring. I can understand if you replied with a better solution, but you dont even do that



There is not only 1 right way to do things in programming. You come off like there is only 1 right way to do things. I am just presenting an alternative solution



-----------------------------------



Do/While loops are most applicable for "Yes/No" type of programs, because you are guaranteed to go thru the loop atleast once before you ask the user if they want to loop again



Since you said you are new, you should read up on do/while loops here: http://www.learncpp.com/cpp-tutorial/56-do-while-statements/



So im going to give you sample code, you can run it and incorporate it into your program



(sample run): http://codepad.org/DubJT22E



---------------------------------------------------



#include

using namespace std;

int main()

{

    char response='n';



    do{



        // DO STUFF HERE



        cout<<"Do you want to loop again?(Y/N): ";

        cin >> response;



    }while(toupper(response )== 'Y');

    // ^this loop executes atleast one time



    cout<<"BYE!";

}
?
2016-10-13 05:07:53 UTC
Why not use a do-on a similar time as loop, somewhat of pasting your code in there two times? it might decrease your code in 0.5. additionally, an basic trick for checking to verify if x or x2 is NaN (not a selection, like the IND blunders) is to assessment them to themselves. if (x != x) will return fake if the fee is IND. you should apply this to create an if-else that ought to tell the consumer their although if or not they have a valid answer or not, somewhat than giving a blanket warning formerly. that's a stable initiate for a beginner. save up the stable artwork!
Jared M
2012-06-25 11:08:29 UTC
The most common way to solve this is to surround the question-asking code in a while loop. Set the while condition variable to true before the loop, then when you ask if the user wants to keep going, set it to false if they say no. This will break you out of the loop. I'll pseudocode this for you below:



keepGoing = true

while(keepGoing) {

## Question asking code

keepGoing = prompt("Keep going?")

}

print("Thank you!")



Now like I said, that's quickly whipped up pseudocode, and you'll have to spruce it up to actually work in c++, but that should get you started. =]



EDIT: Cronin, you're thinking of this wrong. Using a break(); to break out of a while loop is generally a less desirable solution, but that's not what's happening here. There's a HUGE difference in how the code is executed when the while condition becomes false vs. when you use the break command.
?
2012-06-25 11:03:59 UTC
Yeah a while loop is good... set the variable you want to check to y before the while loop and enclose anything else you want repeated in the loop..



string input = "y";

while(input[0] =='y'){



//blah blah... all your code goes here



getline(cin,input);

input[0] = tolower(input[0]); //make the first letter lowercase

}

cout<<"Thank you. Goodbye!"<
Cronin
2012-06-25 12:06:13 UTC
Jeff- your code works fine



Jared- breaking out of a while true look is bad programming pactice



keperkjr- do you even read the posters before posting bad code... She wants ANYTHING that starts with y.



You can't cin a char in C++ without getting a newline in the buffer AND you are sending a bool to toupper... if you type y it fails if you type yes it fails...



Keperkjr please learn C++ before trying to help people with c++
anonymous
2012-06-27 15:44:30 UTC
no doubt


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...