Help!

Hi I am fairly new to the C++ scene and I have stumbled upon a problem in my code, when I "start without debugging" the program runs, but wont let me input the name for the Second Person, only the age.
I was wondering if anyone could give me a reference or link to help me look for the problem. Thanks in advance!.

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
47
 //Eli's Lab 3 : This program calculates the age difference between two people.
#include <iostream>
#include <string>

int main(void)
{
	//typedef defined and declaring
	typedef unsigned short int(USHORT);
	USHORT FirstPersonAge;
	USHORT SecPersonAge;

	std::string Greetings(" This program calculates the age difference between two people. ");
	std::cout << Greetings << std::endl;

	//Propts users full name
	std::cout << " What is the name of the first person?" << std::endl;
	std::string FirstPersonName;
	getline(std::cin, FirstPersonName);
	// Prompts users age and outputs the persons age
	std::cout << " How old is " + FirstPersonName + " ? " << std::endl;
	std::cin >> FirstPersonAge;

	std::cout << " What is the name of the second person?" << std::endl;
	std::string SecPersonName;
	getline(std::cin, SecPersonName);

	std::cout << " How old is " + SecPersonName + " ? " << std::endl;
	std::cin >> SecPersonAge;

	//Declare and calculate the differnce
	int sub;
	sub = FirstPersonAge - SecPersonAge;

	//  If statements for result.
	if (FirstPersonAge > SecPersonAge)
		std::cout << " Result: " << FirstPersonName << " is " << sub << " years older than " + SecPersonName << " . ";
	if (FirstPersonAge < SecPersonAge)
		std::cout << " Result: " << SecPersonName << " is " << sub << " years older than " + FirstPersonName << " . ";
	if (FirstPersonAge == SecPersonAge)
		std::cout << " Result: " << FirstPersonName << " and " << SecPersonName << " are the same age. ";

	std::cout << std::endl;


	return 0;
}
Last edited on
Whats the error? Also having the void in int main() is not necessary, only a C thing
When I run the program, its prompts the first person name and age, but for the second person, the program outputs the text, but wont let me input the 2nd person name (skips the user input part), only lets you input the age.

My teacher asked me to do write this program using int main (void).
What IDE are you using? Also I see in "how olds" you use "+"s to add text. Just use "<<"s. The reason why is because thats what operator C++ cout uses
How about this?

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
47
48
// PersonAgeCalculation.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <string>

int main(void)
{
	//typedef defined and declaring
	typedef unsigned short int(USHORT);
	USHORT FirstPersonAge;
	USHORT SecPersonAge;

	std::string Greetings(" This program calculates the age difference between two people. ");
	std::cout << Greetings << std::endl;

	//Propts users full name
	std::cout << " What is the name of the first person?" << std::endl;
	std::string FirstPersonName;
	std::cin >> FirstPersonName;
	// Prompts users age and outputs the persons age
	std::cout << " How old is " << FirstPersonName << " ? " << std::endl;
	std::cin >> FirstPersonAge;

	std::cout << " What is the name of the second person?" << std::endl;
	std::string SecPersonName;
	std::cin >> SecPersonName;

	std::cout << " How old is " << SecPersonName << " ? " << std::endl;
	std::cin >> SecPersonAge;

	//Declare and calculate the differnce
	int sub;
	sub = FirstPersonAge - SecPersonAge;

	//  If statements for result.
	if (FirstPersonAge > SecPersonAge)
		std::cout << " Result: " << FirstPersonName << " is " << sub << " years older than " + SecPersonName << " . ";
	if (FirstPersonAge < SecPersonAge)
		std::cout << " Result: " << SecPersonName << " is " << sub << " years older than " + FirstPersonName << " . ";
	if (FirstPersonAge == SecPersonAge)
		std::cout << " Result: " << FirstPersonName << " and " << SecPersonName << " are the same age. ";

	std::cout << std::endl;

	system("pause");
	return 0;
}
I don't see anything wrong, but instead of USHORT why not use int?
After line 21, there's a newline remaining in the input buffer and that's getting picked up in the getline on line 25. Putting std::cin.ignore(); after line 21 should clear that newline so you can enter a second name.

You might want to use the absolute value of the difference calculated for sub. There's an abs() function in the cmath header. If the second person is older than the first, sub currently has a negative number.

Result: Jane is -1 years older than John .
Last edited on
Strange
thescriptergeek-
At the moment I am using Visual Studio's 2015. One of the criteria for my lab was the use of typdef statements.

xxvms-
I read in my book it is not recommenced to use the "pause" though.
Last edited on
Ant1g33k

there are different ways to keep console from closing that is one of the ways, you can use it or you can remove it. it is up to you.
Thanks everybody! All your input has helped me to solve the problems !!!!!!!!!!!!! :D have an awesomeeeeeeeeeeee day!!
Topic archived. No new replies allowed.