Need help with this class problem

I'm very new to computer science. My teacher gave us an assignment to greet someone, ask for their age, and determine what year they were born in. For example: Hello Bob, you are 21 years old and you were born in 1999!

I came up with this, but it's 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
25
26
27
28
29
30
31
32
33
34
35
36
// This program will greet the user and ask for their name and age.
#include <iostream>
using namespace std;
int main()
{
	int name, age, year_born;

		// Ask for the user's name
		cout << "Hello, what's you name? ";
		cin >> name;
		

		// Ask for the user's age
		cout << "What is your age? ";
		cin >> age;
		
		
		// Now we must calculate the user's age (assume their birthday is on January 1st)

		// The current year is 2020

		int year_now = 2020;

		// Now we subtract the user's age from the current year, which is 2020
		year_born = 2020 - age;

		// Display the user's name, age, and the year they were born
		cout << "hello " << name;
		cout << "you are " << age;
		cout << "you were born in " << year_born << endl;



		

}
Last edited on
Hello Joni852,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Your first problem is that you are trying to store a string in an "int", This does not work and will cause "cin" to fail and be unusable the rest of the program.

You need to include the header file <string> and use a std::string for "name". Also "std::getline()" would be a better choice than formatted input (cin >> name).

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

using namespace std;

int main()
{
	int age, year_born;
	std::string name;

	// Ask for the user's name
	cout << "Hello, what's you name? ";
	std::getline(std::cin, name);


After that the program look OK, but I will give it a test to see what happens.

Andy
The class just began, and it's for beginners. Our teacher never taught us "string" or "std::getline(std::cin,"

Last edited on
Hello Joni852,

Looking at your code I misunderstood the instructions.

My teacher gave us an assignment to greet someone, ask for their age
Nothing here says anything about asking for or inputting their name, just greet. That means line 9 needs to change from the prompt to just a greeting.

Line 22 defines a variable that is never used, but should be.

This should give you an idea of what you can do:
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
// This program will greet the user and ask for their name and age.
#include <iostream>

using namespace std;

int main()
{
	// The current year is 2020
	constexpr int YEAR_NOW = 2020;  // <--- Changed. And moved. Or you could use "const".

	int age, year_born;

	// Great the user.
	cout << "Hello...\n"; // <--- This needs changed to a greeting.


	// Ask for the user's age
	cout << "\nWhat is your age? ";
	cin >> age;


	// Now we must calculate the user's age (assume their birthday is on January 1st)

	// Now we subtract the user's age from the current year, which is 2020
	year_born = YEAR_NOW - age;

	// Display the user's name, age, and the year they were born
	cout << "\nYou are " << age;
	cout << " and you were born in " << year_born << endl;

	return 0;  // <--- Not required, but makes a good break point.
}


Andy
Registered users can post here. Sign in or register to post.