How to end this code

Everything in this code works fine, so far...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{ cout << "What is your favourite colour? ";
string mystring;
string colour;
cin >> colour;
cout << "That's groovy, man! Turns out "; cout << colour; cout << " is my favourite colour, as well! \n";
do {
cout << "Are you an "; int adventurer; cout << "adventurer, "; int politician; cout << "policitian, or "; int dreamer; cout << "dreamer? ";
string mystring;
cin >> mystring;
if (mystring == "adventurer")
   cout << "That's far out, man. You have +2 in all attributes.\n";
if (mystring == "dreamer")
    cout << "That's dreamy, man. You have +5 in skill.\n";
if (mystring == "policitian")
    cout << "That's psychadelic, man. You have +5 in IQ.\n";
} while(mystring != "adventurer" && mystring != "dreamer" && mystring != "politician");
return 0;
}


...but whenever you type in the respective response for the second cin prompt, it will deliver the respective response but then restate the prompt.
how do I get the program to end after delivering an accepted response (either adventurer, politician, or dreamer) but restate the prompt when you enter anything else?

Thank you for the time!
Seems like I have seem this same assignment somewhere else on this forum..
Also when you do std::cout << you can put more than one thing in it you are only putting one thing.

std::cout << 'H' << 'e' << "ll" << 'o' << " World!" << std::endl;
^^Will compile. I would suggest fixing lines 10 and 12 also why in gods name are you declaring the variables after outputting txt? put the variable declarations on another line ( line 12 ).
And same with cout you can declare multiple variables of the same type at once.
int a , b , c , d , e , f , g , h;
Although the truth is..I don't see why you even have those variables..
Also you are declaring mystring twice on line 7 then again on line 13. btw when you are inputting strings you want to use getline not cin since cin stops at a whitespace and getline stops at a null character or new line.

And to fix your problem you want to make line 21 || not &&... You are saying if the string does not equal 3 different things at one time then to repeat it should be if it does not equal any of them then to repeat. Ps. Please learn proper indentation it will help you out in the long run especially for checking if you are missing a bracket.

A side note - Many people like to declare all the variables at the top of the local scope that is using them.

1
2
3
4
5
6
int main()
{
    int a , b , c , d;
    std::string input_color , input_personality;
    /*...*/
}


versus
1
2
3
4
5
6
7
8
9
10
int main()
{ std::string input_color
    std::cout << "enter color:";
    std::cin >> input_color;
    int b;

    std::string input_personality;
    int a , c , d;
    /*...*/
}
Last edited on
i really have to say this:
you - my friend- cannot write any medium sized or larger program, why!!!
cause if you extend this simple program to more than 100 lines of code, no one will ever try to read it.

please pay attention to readability principle, it is really important.
if i may, your same code must be written as:
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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main ()
{
    cout << "What is your favourite colour? ";

    string mystring;
    string colour;

    cin >> colour;
    cout << "That's groovy, man! Turns out "<< colour<< " is my favourite colour, as well! \n";
    do
    {
        int adventurer, policitian , dreamer;

        cout << "Are you an adventurer, policitian, or dreamer? ";
        string mystring;
        cin >> mystring;

        if (mystring == "adventurer")
            cout << "That's far out, man. You have +2 in all attributes.\n";
        if (mystring == "dreamer")
            cout << "That's dreamy, man. You have +5 in skill.\n";
        if (mystring == "policitian")
            cout << "That's psychadelic, man. You have +5 in IQ.\n";
    }
    while(mystring != "adventurer" && mystring != "dreamer" && mystring != "politician");

    return 0;
}


even after this code is written like that, you should take the advice giblit pointed out.
I've only just started learning c++ so forgive me if I use the wrong terms, but the reason it isn't doing what you want it to is that you redeclared 'mystring' inside the do-while loop, which then gets destroyed after the '}'. You then try to refer to it, but it uses the 'mystring' you declared earlier and because that is empty and isn't going to equal adventurer dreamer or politician, the check returns true and it loops round again.

If you just delete the string mystring; inside the do-while loop it should work as you have already declared mystring outside the scope of the loop earlier in the code, and the input will be stored there instead.

Also, politician is spelt wrong in there :).
Last edited on
Also change all those if's to a switch like this
switch(mystring)
{
case "adventurer" :
{
cout << "Far out, man\n";
Str += 2;
Def += 2;
}

case "dreamer" :
cout << "One statement needs no brace. CAUTION if no break it will continue down instead leave switch\n";

case "politician" :
{
cout << "Can use break to leave switch";
break;
}

default:
cout << "Will at least get here if no others, or dreamer and here if mystring == dreamer.";
}

And because its string's either make enum with all options or #define each beforehand.
Last edited on
@vidus
maybe you meant something more like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch( mystring[ 0 ] ) //switch are for chars and ints not strings
{
case 'a':
    std::cout << "Far out, man." << std::endl;
    str += 2;
    def += 2;
    break; //you were missing
case 'd':
    std::cout << "You are wrong you don't need braces at all that is like"
              << "using braces around this cout statement. The braces do nothing." << std::endl;
case 'p':
    std::cout << "The break is used after each statement to end the statement."
              << " not the switch. A switch loops through each option after the case until it hits a break." << std::endl;
    break; //edited because I forgot this but it isn't really necessary since it is the last item but you can put for the looks
}
Last edited on
closed account (NyhkoG1T)
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
 
cin >> colour;


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.
Topic archived. No new replies allowed.