Help, why is it not working?

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>
#include <cmath> 
#include <iomanip> 
using namespace std;

int main()
{
	//declare variables 
	string name; 
    string ObamaFriend; 
	int m;

	//Enter values
	cout << "Enter your full name: " << name << endl;
	cout << "Barack H Obama, please enter your gender(m/f)? " << male <<  endl; 
	cout << "Mr. Barack H Obama, please enter your friend's name: " << ObamaFriend << endl; 
	cout << "Hello Mr/Mrs Bill Clinton, Barrack H Obama considered you as a friend!!" << endl;


	//terminate program
	system("pause");
	return 0;
}


Don't understand what's wrong with string??
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;
}
Last edited on
Wait, why are you using std:: ? and didn't I do it somewhat similar? I'm in a C++ class.
Last edited on
I have to do it simple, using the basics, can you tell me why string is not right?

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
#include <iostream> 
#include <string>
#include <cmath> 
#include <iomanip> 
using namespace std;

int main()
{
	//declare variables 
	string name; 
    string ObamaFriend; 
	char gender;

	//Enter values
	cout << "Enter your full name: " << name << endl;
	cout << "Barack H Obama, please enter your gender(m/f)? ";
	cin >> gender; 

	cout << "Mr. Barack H Obama, please enter your friend's name: " << ObamaFriend << endl; 
	cout << "Hello Mr/Mrs Bill Clinton, Barrack H Obama considered you as a friend!!" << endl;


	//terminate program
	system("pause");
	return 0;
}
I wrote:
First, to input values you need to use std::cin or std::getline, not std::cout.
Look at line 15 and 19.

I wrote:
Third, you never actually DO anything with the variables.
Look at line 16, 19 and 20. You have just hardcoded in answers you expect to get.

I wrote:
On a side note, line 22 is not generally considered good practice, and to use system commands you should really be including <cstdlib>.
Look at line 24.

Did you look at anything APART from the problem with gender? My code is there if you want to see a possible way of doing it (that works). It does only use the 'basics'. If you want, though, you could replace all the std:: with using namespace std; at the top of the program.

EDIT:
The reason why I prefer to use std:: over using namespace std; is because often using namespace std; can cause name conflicts, where you accidentally name a variable or function the same as something in the standard library. Doing it like this, so that it splits the code into the standard library's functions and my functions, mean that I have an easier time of preventing errors.
Last edited on
Ok thanks, so if delete all the std:: do I just put the namespace std; at the beginning where I put it?
OK so I did it like this but there is an extra space after Mr/Mrs Bill Clinton

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
#include <iostream>
#include <string>
using namespace std; 

int main() {
    string name;
    string other;
    char gender;

    cout << "Enter your full name: ";
    getline(cin, name); // get their full name, including spaces.

    cout << name << ", please enter your gender (m/f): ";
    cin >> gender;
    cin.ignore(); // ignore the newline left in the buffer

    // determine whether to start with Mr or Ms based on gender
    cout << (gender == 'm' ? "Mr. " : "Ms. ") << name << ", please enter your friend's name: ";
    getline(cin, other);

    cout << "Hello Mr/Mrs " << other << ", " << name << " considered you as a friend!!" << endl;

 
    
	//terminate program
	system ("pause");
	return 0; 
}
Last edited on
Thanks, think I got it.
Topic archived. No new replies allowed.