Question about my beginner code.

Alright guys I just have a question basically about my coding "form" and how to fix what I assume is a rather easy problem. If you have any tips on how to better set up my code to make it more elegant it would be greatly appreciated.

#include <iostream>
#include <string>   //enables string functions.
using namespace std;

int main()
{
    string name;  // declares the string "name"

    cout << "Please enter your name (lowercase letters)" << endl;  //asks user for name and stores in string name.
    getline(cin, name);
        if (name == "joe");                                         // tests who the user is and then prints out response
            cout << "You smell get off my couch" << name << endl;   // specifically for who they are.
        if (name == "cody")
            cout << "Your mum goes to college" << endl;
        else
            cout << "You're a stand up guy!" << endl;

return 0;
}

//Just a fun little thing between friends trying to learn programming.



I'm finding that it is printing both the cout response specifically for joe and cody, but is also printing out the else. How do I change it to make it just print one response.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>   //enables string functions.
using namespace std;

int main()
{
    string name;  // declares the string "name"

    cout << "Please enter your name (lowercase letters)" << endl;  //asks user for name and stores in string name.
    getline(cin, name);
        if (name == "joe")//;                                         // tests who the user is and then prints out response
            cout << "You smell get off my couch" << name << endl;   // specifically for who they are.
        else if (name == "cody")
            cout << "Your mum goes to college" << endl;
        else
            cout << "You're a stand up guy!" << endl;

return 0;
}

//Just a fun little thing between friends trying to learn programming. 


Notice that in original code there was 1 if structure and 1 if-else structure :)
Last edited on
Ah lol thank you so much. Noob mistake lol. Quick response is greatly appreciated thank you sir =]
Last edited on
I do have one more question. How would I put a loop in this program that would keep restarting it over and over again until a break command was entered. I know about for loops and I was experimenting with it but I couldn't get it to do what I wanted I'll post the code but it will probably be a mess
Last edited on
Try using a do/while loop.

1
2
3
4
5
6
7
8
9
10
11
12
cout << "Please enter your name(type quit to quit)";
do
{
getline(cin, name)
     if (name == "joe")//;                                         // tests who the user is and then prints out response
            cout << "You smell get off my couch" << name << endl;   // specifically for who they are.
        else if (name == "cody")
            cout << "Your mum goes to college" << endl;
        else
            cout << "You're a stand up guy!" << endl;

} while (name != quit) 

Last edited on
this is what I have but I am getting errors at the end. What exactly does "do" do? I've only read about for and while loops

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
#include <iostream>
#include <string>   //enables string functions.
using namespace std;

int main()
{
    string name;  // declares the string "name"
    int i;

    cout << "Please enter your name (lowercase letters)" << endl;  //asks user for name and stores in string name.
    do
    {
    getline(cin, name);
        if (name == "joe")                                        // tests who the user is and then prints out response
            cout << "You smell get off my couch" << endl;   // specifically for who they are.
       else if (name == "cody")
            cout << "Your mum goes to college" << endl;
        else
            cout << "You're a stand up guy!" << endl;

return 0;
} while (name != quit) 



//Just a fun little thing between friends trying to learn programming. 


it says it expects ";" before { which I dont really understand
1
2
return 0;
} while (name != quit)


return 0 should be outside of the do while, i didn't realize i copied that in there.
Last edited on
You haven't put 'quit' in quotation marks, so it's trying to compare the name string to a non-existent string called 'quit'. It should be:

while (name != "quit")
Topic archived. No new replies allowed.