Some beginner code, To help understand some basic's!!

Posts 1–5 of 5 · Page 1 of 1
Some beginner code, To help understand some basic's!!
Hello, I am learning c++ , I know the basic's of java already but decided to set my focus on c++. Beneath is just some simple code to help someone beginning understand taking input from the user, adding them together etc using variables etc.

Code:
#include <iostream>
using namespace std;

int main()
{
double yourAge, mothersAge, fathersAge, siblingsAge, averageAge;

cout << "Simple test program, Thank you for your time" << "\n";
cout << "Please enter the age, Including how many months old you are:" << "\n"; cin >> yourAge;

cout << "Please enter the age, Including how many months old your mother is: " << "\n"; cin >> mothersAge;

cout << "Please enter the age, Including how many moths old your father is: " << "\n"; cin >> fathersAge;

cout << "Please enter the age, Inclduing how many months old your sibling is, if you have no siblings enter 0: " << "\n"; cin >> siblingsAge;

cout << "I will now add, all your familys ages together, And get the adverage age of your family!!" << "\n";

averageAge = yourAge + mothersAge + fathersAge + siblingsAge;

cout << averageAge / 4;
cout << "\n";

return 0;
}
Disclaimer: Please remove if no need, Didn't think this would be spamming.
Simple edit to prove something,

Ki*****cs gay!!
i suggest that you put in some user validation in after each "cin". its good programming practice and used to prevent the user for entering a ridiculous age like -9, and to prevent overflow. for now just set a while loop with the parameters you dont want the user to enter.

Code:
while(yourage <= 125 || yourage >=0)
{
      cout << "Please enter a real age";
      cin >> yourage;
}
Quote Originally Posted by Super_mario666 View Post
i suggest that you put in some user validation in after each "cin". its good programming practice and used to prevent the user for entering a ridiculous age like -9, and to prevent overflow. for now just set a while loop with the parameters you dont want the user to enter.

Code:
while(yourage <= 125 || yourage >=0)
{
      cout << "Please enter a real age";
      cin >> yourage;
}
As i go along, I shall post different sources, based around this one, Thanks for the tip!!
Sweet! Now I can get my family's average age! THANK YOU!
Rally Help-full. Now I learn C++ and help teach too..
Posts 1–5 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us