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