If I may suggest this code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string mystring, colour;
cout << "What is your favourite color? ";
getline(cin, colour);
cout << "That's groovy, man! Turns out " << colour << " is my favourite colour, as well!" << endl;
do {
cout << "Are you an adventurer, politician, or a dreamer? ";
getline(cin, mystring);
if(mystring=="adventurer") {
cout << "That's far out, man. You have +2 in all attributes!" << endl;
break;
}else if(mystring=="dreamer") {
cout << "That's dreamy, man. You have +5 in skill." << endl;
break;
}else if(mystring=="politician") {
cout << "That's psychadelic, man. You have +5 in IQ." << endl;
break;
}else{
cout << "You entered an invalid value. Please try again." << endl;
}
} while(true);
return 0;
}
|
From the code you shared, you weren't using the variables of
|
int adventurer, dreamer, politician;
|
Also, as others have stated, code organization is key in debugging your code and in overall readability. If this is an assignment in school, take these to heart. If your professor can't read it, then you'll probably get a bad grade.
It is always a good idea to declare your variables at the top of the scope and if you realize your not using some variables, remove them all together.
The reason your loop kept going through and through was because you had your mystring variable declared twice, once in the scrope of main and another inside the scope of your loop which was effectively destroying anything you had stored in that variable. Also, having that many checks in a loop condition can be confusion and hard to read. A better alternative is to have it loop indefinitely, and break from that loop as I have in the code above when a correct answer is given (notice the while(true)).
I also changed
to use the getline method. This is because if someone enters "dark yellow", cin will only read 'dark' and yellow will become a default answer for the next cin, which is the question of adventurer, dreamer, politician, resulting in a false answer given and the loop to be performed unnecessarily.