That... is most definitely wrong. In a number of ways.
First, to input values you need to use
std::cin or
std::getline, not
std::cout. Second, you declared the gender to be an integer, rather than a
char, and gave the wrong name on line 16 anyway. Third, you never actually DO anything with the variables.
On a side note, line 22 is not generally considered good practice, and to use
system commands you should really be including
<cstdlib>. Also, lots of people prefer to avoid
using namespace std; and would rather prepend everything relevant with
std::.
Here is your code cleaned up:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <iostream>
#include <string>
int main() {
std::string name;
std::string other;
char gender;
std::cout << "Enter your full name: ";
std::getline(std::cin, name); // get their full name, including spaces.
std::cout << name << ", please enter your gender (m/f): ";
std::cin >> gender;
std::cin.ignore(); // ignore the newline left in the buffer
// determine whether to start with Mr or Ms based on gender
std::cout << (gender == 'm' ? "Mr. " : "Ms. ") << name << ", please enter your friend's name: ";
std::getline(std::cin, other);
std::cout << "Hello Mr/Mrs " << other << ", " << name << " considered you as a friend!!" << std::endl;
std::cin.get(); // an alternative to system("pause") - press ENTER to quit
return 0;
}
|