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!.
//Eli's Lab 3 : This program calculates the age difference between two people.
#include <iostream>
#include <string>
int main(void)
{
//typedef defined and declaring
typedefunsignedshortint(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;
}
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).
// PersonAgeCalculation.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <string>
int main(void)
{
//typedef defined and declaring
typedefunsignedshortint(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;
}
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.