I just put this together for my C++ class. I've tested it thoroughly but I want to make sure I'm doing everything correctly. Tell me if you see anything that doesn't stick to standards or whatever. By the way, this is supposed to check the difference between the ages of two people. Thanks for your time.
PS - You'll probably notice some strange things about the code. For instance, I chose to put std:: in front of cout and cin. This was intentional (part of the requirements for this particular exercise).
Code:#include <iostream> #include <stdlib.h> typedef unsigned short int USHORT; int main() { char FirstPersonName[40]; char SecondPersonName[40]; USHORT FirstPersonAge; USHORT SecondPersonAge; USHORT AgeResult; std::cout << "This program calculates the age difference between two people. \n\n"; std::cout << "What is the name of the first person? \n"; std::cin >> FirstPersonName; std::cout << "How old is " << FirstPersonName << "? \n"; std::cin >> FirstPersonAge; std::cout << "What is the name of the second person? \n"; std::cin >> SecondPersonName; std::cout << "How old is " << SecondPersonName << "? \n"; std::cin >> SecondPersonAge; if (FirstPersonAge == SecondPersonAge) { std::cout << "Result: " << FirstPersonName << " and " << SecondPersonName << " are the same age. \n"; system("pause"); exit(0); } if (FirstPersonAge > SecondPersonAge) { AgeResult = FirstPersonAge - SecondPersonAge; std::cout << "Debug AgeResult: " << AgeResult << "\n"; if (AgeResult == 1) { std::cout << "Result: " << FirstPersonName << " is " << AgeResult << " year older than " << SecondPersonName << ". \n"; } else { std::cout << "Result: " << FirstPersonName << " is " << AgeResult << " years older than " << SecondPersonName << ". \n"; } } else { AgeResult = SecondPersonAge - FirstPersonAge; if (AgeResult == 1) { std::cout << "Result: " << SecondPersonName << " is " << AgeResult << " year older than " << FirstPersonName << ". \n"; } else { std::cout << "Result: " << SecondPersonName << " is " << AgeResult << " years older than " << FirstPersonName << ". \n"; } } system("pause"); return 0; }




Reply With Quote