#include <iostream>
#include <stdio.h>
#include <string>
usingnamespace std;
int main()
{
char correct;
do {
string name;
double age;
cout << "what is your first name?" << endl;
cin >> name;
cout << "how old are you?" << endl;
cin >> age;
cout << "your name is " << name << " and your are " << age << " years old?" << endl;
do {
cout << "is this correct? (y/n)" << endl;
cin >> correct;
} while (correct != 'n' || correct != 'y');
} while (correct == 'n');
return 0;
}
it keeps asking "is this correct? (y/n)" no matter what you put in
do {
cout << "is this correct? (y/n)" << endl;
cin >> correct;
} while (correct != 'n' || correct != 'y');
Look carefuly at the while expression, can you figure out what the problem is? Hint: the expression will be true if EITHER one is true. Think about whether you need this do while or not.
Also put these before the do while loop:
1 2
string name;
double age;
I would prefer to use a while loop instead of do while - do whiles are rarely needed and while loops are easier to read.
while( /* test condition */) {
// multiple lines of code here
}
The difference is that the test condition is evaluated first, and is better because you can see the test at the top rather than it being heaps of lines further down.
If you have a lot of code, it is better to call a function or functions to carry out the code in braces. this makes it easier to understand and is much clearer way of doing things.
I recommend you fix the first problem before trying to turn the do-while loop into a while loops. Trying to solve many "problems" at once can easily get confusing.