question regarding conditional statements

hello...

first - i'd like to clarify that i am in my first few hours of using C++, and have very very little programming experience prior to this (a few hours messing with python).

my friend emailed me a program that they created in a class using python that asks the user's name and gender and subsequently "sings" happy birthday to them, replacing dear with "mr." or "ms.". she suggested that i try to do this, in addition to some of the (i believe) well known Euler problems...
---

i wanted to create a program that asks the user their name, gender - and then says : "goodnight, _____ (name)"

this is as far as i've gotten, and i'm not sure how to set up the "if, then" statements. i know that they involve "?" and ":"



any help would be appreciated:


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
using namespace std;

int main ()
{
	string name;
	cout << "what is your name?: ";
	getline (cin, name);
	cout << "hello, " << name << ".\n";
	string gender;
	cout << "are you male or female?: ";
	getline (cin, gender);




----
p.s. please take it easy with any glaring errors that i have...i'm trying to learn, i realize some if it might seem silly!


thanks so much,
brendan
i should clarify that i wanted to use the conditional statements to apply a title before their name....

thanks again,

b
there's two way to do this. One is with the ternary operator (the ? : you mentioned), but that's generally harder to read and understand. The other is with an if statement.

If statements are pretty simple:

1
2
3
4
5
6
7
8
if(gender == "male")  // if the user typed "male"
  cout << "Mr. ";
else if(gender == "female") // otherwise, if they typed "female"
  cout << "Ms. ";
else  // otherwise, if they typed anything else
  cout << "Dr. ";

cout << name << ".\n";


Just note the difference between == (comparison) and = (assignment). A common newbie mistake is to use assignment instead of comparison:

 
if(gender = "male")  // bad! 



The ternary operator is like a compact if statement:

 
cout << (gender == "male") ? "Mr. " : "Ms. ";


The syntax is expression ? value_if_true : value_if_false;


I would avoid using the ternary operator for now and just stick with if/else.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main ()
{
	string name;
	cout << "What is your name?";
	getline (cin, name);
	string gender;
	cout << "what is your gender?"; 
	getline (cin, gender);
	if (gender == "male") 
		cout << "Mr. ";
	else if (gender == "female")
		cout << "ms. ";
	else 
		cout << "Dr. ";
	cout << "Good Evening, " << gender << name << ".\n";
	return 0;
}


=

[Session started at 2009-12-29 10:00:06 -0600.]
What is your name?brendan
what is your gender?male
Mr. Good Evening, malebrendan.

The Debugger has exited with status 0.
[Session started at 2009-12-29 10:01:22 -0600.]
What is your name?delilah
what is your gender?female
ms. Good Evening, femaledelilah.

The Debugger has exited with status 0.

---------------------------------------------
sorry if this is annoying....i'm working with it, but in the case that i don't get it in the next few hours, some help would be appreciated.


b
This is what you're doing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// this gets a line from the user.  The user's line gets put in 
// your 'gender' string
getline (cin, gender);

// Now you check your gender string
//  to see if the user input "male"
if (gender == "male")   // if they did..
  cout << "Mr. ";     // then COUT the title string

//.. else if stuff here


// then you cout the following:
cout << "Good Evening, "  // output "Good Evening"

<< gender  // output your gender string
       // remember this is what the user input.  IE:  "male"

<< name << ".\n";  // then output the user name and new line 


This is why you get the "Mr. Good Evening, malebrendan". You cout the title, then good evening, then the gender.

Maybe instead of having cout in your if statements, you can assign a string to hold the title, and then output the title instead of the gender.
thanks so much


very obliged,
brendan
Topic archived. No new replies allowed.