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.
#include <iostream>
#include <string> //enables string functions.
usingnamespace 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.
elseif (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 :)
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
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.
elseif (name == "cody")
cout << "Your mum goes to college" << endl;
else
cout << "You're a stand up guy!" << endl;
} while (name != quit)
#include <iostream>
#include <string> //enables string functions.
usingnamespace 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.
elseif (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