Declare char and if statements

Hi cplusplus folks
i am really new to c++, i have only been learning it for about a week and i am using stroustrups programming book.
and now ive come across a problem.
im doing a "write to a friend letter program" with the usual cout and cin ask and answer princip.. so basically it takes all the basic stuff; objects, types and values, char, int, double, and string
so yea the problem, and the drill question im trying to do is (taken from the book):

"Declare a char variable called friend_sex and initialize its value to 0.
Prompt the user to enter an m if the friend is male and an f if the friend is
female. Assign the value entered to the variable friend_sex. Then use
two if-statements to write the following:"
so yea heres my code (everything works down to the if statements, i dont know how to make it interact with user-input):

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
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <windows.h>
#include <winuser.h>
using namespace std;

int main()
{
	cout << "Please enter your first_name\n"; //user types in name
	string first_name; // string variable declares first_name
	cin >> first_name; // cin reads first_name
	cout << "Hello, " << first_name << "\n"; // cout writes "hello" and user input first_name
	cout << "Enter the name of the person you want to write to\n"; // cout writes out and asks a question
	string name; // string variable declares name
	cin >> name; // cin reads the declared variable; name
	cout << "Dear, " << name << "\n"; // cout writes out dear and user input (name)
	cout << "How are you? I am fine. I miss you.\n"; // cout writes out
	
	cout << "Have you seen friend_name lately?\n"; // cout writes out
	string friend_name; // string variable declares friend_name 
	cin >> friend_name; // cin reads friend_name
	cout << "Yes, " << friend_name << "\n"; // cout writes output and user input out
	
	char friend_sex = 0;
	char m;    // is this neccesary?
	char f;
	cout << "type m if your friend is a male.\n";
	cout << "type f if your friend is a female.\n";
	cout << "friend_sex?!\n";
	cin >> // what to type here?
	
	if( friend_sex == m )
	{
		cout << "Please ask him to call me.\n";
	}
	else if( friend_sex == f)
	{
		cout << "Please ask her to call me.\n";
	}
	else
	{
		cout << "Only F or M please...\n";
		cin.get();	
	
	return 0;
}
}


My problem is i dont know how to use the char variable as the book says with the friend_sex value is 0 so it prints out the f or m thats asked from the user, cause theres barely any info given on how char works.. it says something about converting char to int, but i dont see how thats gonna do any good here when its not numbers, but letters that is the input... cause i know how to do it if it was a program with 1,2,3,4,5 answers etc.. but not with a char
can anyone help or give some hints? much appreciated thx
-bob
Last edited on
Simply do a cin to friend_sex just as you would as if it were an integer.
 
cin >> friend_sex;


BTW, your checking for m or f won't work as you have it.
Variables m and f are uninitialized.
You need to initialize them.
1
2
const char m = 'm';
const char f = 'f';


Another way to to simply compare friend_sex to the character literal.
 
if( friend_sex == 'm' )  // assumes the user entered lower case only 




This will get the user input and display the proper message, however it does not check that they entered f or m. If the user enters anything else nothing will happen.

1
2
3
4
5
6
char ch;
cin >> ch;
if(ch == 'f')
  cout << "Please ask her to call me\n";
else if(ch == 'm')
  cout << "Please ask him to call me\n";


What concepts did the book just cover? Usually they design the exercises to emphasize something you just learned.
Last edited on
@AbstractionAnon thanks alot man it works now! :)
much appreciated :)

so i was wondering if you could help me with another question i got?
because i dont understand this as the question asks
"Declare a char variable called friend_sex and initialize its value to 0.
Prompt the user to enter an m if the friend is male and an f if the friend is
female. Assign the value entered to the variable friend_sex. Then use
two if-statements to write the following:"

why do i need to initialize the char variable's value to 0?
char friend_sex = 0;
is it like the int declaration?
int Choice;
because i dont have any "friend_sex" referrer in my code, that reads user input on "friend_sex" like when using string to hold a variable fx
string friend_name;
so it doesnt make much sense to me :P
Last edited on
@RubiconJosh
a link to the book im using: (or well pdf as i got it online)
http://www.stroustrup.com/Programming/

But i think it covers everything that is to know about c++
or the chapter im working on includes:
3.1 Input
3.2 Variables
3.3 Input and type
3.4 Operations and operators
3.5 Assignment and initialization
3.5.1 an example delete repeated words
3.6 Composite assignment operators
3.7 Names
3.8 Types and objects
3.9 Type safety
safe conversions
unsafe conversions

so yea thats pretty much it, i just really couldnt find much about char, cause yea as you said there should be some coverage about it, which i also found weird i couldnt, so i had to ask here x)
why do i need to initialize the char variable's value to 0?

Technically you don't, since the first thing you do prompt for the value.

Whether to initialize variables when they're declared depends on a couple of things.
1) Are there paths through the code that might bypass initilization?
2) Do you test the value to see if you've already prompted for it? i.e. if it's non-zero, then I've already asked for it. Not the case in you simple letter, but in a complex program, you might only ask this under certain conditions.
3) There's a style of programming that insists that every variable is initialized. IMO, this is of questionable benefit when #1 is false and adds initialization overhead to functions where variables are initialized unnecessarily.

is it like the int declaration?

yes.
i dont have any "friend_sex" referrer

I don't know what you mean by this. You refer to friend_sex in lines 31 and 35.


Whether to initialize variables when they're declared depends on a couple of things.
1) Are there paths through the code that might bypass initilization?
2) Do you test the value to see if you've already prompted for it? i.e. if it's non-zero, then I've already asked for it. Not the case in you simple letter, but in a complex program, you might only ask this under certain conditions.
3) There's a style of programming that insists that every variable is initialized. IMO, this is of questionable benefit when #1 is false and adds initialization overhead to functions where variables are initialized unnecessarily.


that makes sense now

I don't know what you mean by this. You refer to friend_sex in lines 31 and 35.


ah yea nevermind :p i was too fast there, cuz i see it got it initialized in the
char friend_sex = 0; before the male or female part begins
but again big thanks for helping out
Topic archived. No new replies allowed.