I am new to coding and I'm trying to do a long do while
loop with nested if
statements but I'm having issues with getting my loop to actually loop.
Instead of getting help directly on my project, which has a very long code, I made a simple kind of like it version. It also does not loop. It will get to the end and ask the user if they want to try again but when "y" is entered it ignores the if statements.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string sodaChoice;
char answer = 'n';
do
{
cout << "Please choose your favorite soda from the following: Rootbeer, Diet Coke, Coke, Sprite" << "\n";
getline(cin, sodaChoice);
if (sodaChoice == "Rootbeer")
{
cout << "Me too!" << "\n";
}
else if (sodaChoice == "Diet")
{
cout << "Not me :(" << "\n";
}
else if (sodaChoice == "Coke")
{
cout << "It's alright" << "\n";
}
else if (sodaChoice == "Sprite")
{
cout << "only if I'm sick" << "\n";
}
else
{
cout << "That was not on the list";
}
cout << "would you like to try again?";
cin >> answer;
} while (answer == 'y'|| answer == 'Y');
return 0;
}
I thought maybe I needed a do loop in the do loop around the if statements but then I didn't know how to go about it. Any help would be appreciated. I have spent many, many hours trying to figure it out.
I tried to ask my teacher, but my college teaches from some generic curriculum that their dean wrote based on her book. He is not very enthusiastic about helping or teaching.
EDIT: Thank you all for the answers! As for this being a duplicate question, I haven't learned cin.ignore yet, so I wasn't aware that it was relevant to my issue. Thank you all for teaching me!
The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
Create a for- loop to repeatedly execute statements a fixed number of times. Create a while- loop to execute commands as long as a certain condition is met. Use if-else constructions to change the order of execution.
You can make the second IF-THEN statement part of an ELSE statement. Therefore, the second IF condition is not evaluated when the first IF condition is true.
Existing answers are correct in the case of the user behaving nicely and entering just a 'y' or an 'n' followed by a newline.
We can add a little safety with a modification to the .ignore() call by asking it to ignore all characters up to an including the next newline:
You will need to add this header include at the top of your translation unit:
#include <limits>
And you'll need to add this line before cin >> answer
:
// consumes and ignores as many characters as necessary until a newline.
// The newline is also consumed.
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Full program with test inputs here:
http://coliru.stacked-crooked.com/a/996e77559590ad6d
Unfortunately, it's not unusual for Computer Science teachers to know nothing about computer science. Don't worry about this or let it put you off learning how to write software - it's just a symptom of the fact that good software engineers get paid a lot more than good teachers.
Here is some useful reference material: https://en.cppreference.com/w/cpp/io/basic_istream/ignore
With
cin >> answer;
you read exactly one character. The problem is that you entered at least two characters when writing the answer. The actual 'y'
or 'n'
plus the Enter key, which is added as a newline in the input buffer.
This newline is then read by the next getline
call as an empty line.
There are a few solutions to this problem, for example using ignore
after reading into answer
. Or if you want only one-word inputs then you could use formatted input using >>
for sodaChoice
too, since it will by default skip leading white-space (like newlines).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With