Following along a tutorial I entered code for a person to enter the age of five students.
Then, the program is supposed to divide the five ages and display the result.

It allows me to enter the ages, but as soon as I hit enter after inputting the fifth age, the window closes.

I have looked over the code, can't find anything wrong. Can someone help please.

/* This program calculates the average age of a class of
five students after prompting the user to enter the age 
of each student*/

#include <iostream.h>
main()
{
// declaration of variables, the age will be in whole numbers//
int age1, age2, age3, age4, age5;
int TotalAge, AverageAge;
//Take ages of the students from the user//
       cout << "Please enter the age of student 1:";
       cin >> age1;
       cout << "Please enter the age of student 2:";
       cin >> age2;
       cout <<"Please enter the age of student 3:";
       cin >> age3;
       cout <<"Please enter the age of student 4:";
       cin >> age4;
       cout << "Please enter the age of student 5:";
       cin >> age5;
//Calculate the total age and average age//
   TotalAge = age1 + age2 + age3 + age4 + age5;
   AverageAge = TotalAge / 5;
   //Display the result (average age)//
   cout << "Average age of class is:" <<AverageAge;
}

Also, how can someone (namely myself) tell if the code is written in C or C++?
Any pointers on the look, ie: the structure of my coding.

Recommended Answers

All 8 Replies

>>how can someone (namely myself) tell if the code is written in C or C++?
firstly by the header files. iostream is c++ only.

>>the window closes.
Because you have to add some code to stop it from doing that so that you can see the output. The console window normally closes as soon as the main() function ends.

int main()
{
    // other code here is not shown
    cin.get(); // stop the program
    return 0;
}

That was actually the first thing I tried after it wouldn't give me the average. Sorry, I guess I should've mentioned that.

Then you didn't read the program's output good enough. If you don't add "\n" at the end of the last cout line then the text will get merged with other text such as "press any key .." making it someone difficult to see your program's output.

There is no reason I see why your program doesn't display the average.

I don't see any reason either. I am dumbfounded. I am entering the code exactly as the tutorial shows. Everything is fine upto the dang average.

Again, I am entering the code as follows, including the

cin.get();
return 0;

/* This program calculates the average age of a class of five students after prompting the user to enter the age of each student. */
#include <iostream.h>
main ()
{
// declaration of variables, the age will be in whole numbers
int age1, age2, age3, age4, age5;
int TotalAge, AverageAge;
// take ages of the students from the user
  cout << "Please enter the age of student 1: ";
  cin >> age1; 
  cout << "Please enter the age of student 2: ";
  cin >> age2;
  cout << "Please enter the age of student 3: ";
  cin >> age3;
  cout << "Please enter the age of student 4: ";
  cin >> age4;
  cout << "Please enter the age of student 5: ";
  cin >> age5; // calculate the total age and average age
TotalAge = age1 + age2 + age3 + age4 + age5;
AverageAge = TotalAge / 5;
// Display the result ( average age )
cout << "Average age of class is: " << AverageAge;
cin.get(); 
return 0;
}

If possible AncientDragon, could you check out the tutorial?

http://howtoprogramc.googlepages.com/expressions.htm

Ty for your reply...

your program has a couple problems:

1) you must declare main like this: int main() . You can't leave off the return type.

2) you need to strip the '\n' character out of the keyboard buffer after each cin. There are several ways to accomplish it, but this method will work if the only character is '\n'. For a more complete discussion see Narue's thread here.

cin >> age1; 
cin.ignore();

use void main() instead
and add system("PAUSE") at the end after the cout statement

commented: void main() is a terrible idea. -5

use void main() instead
and add system("PAUSE") at the end after the cout statement

You obviously have no clue. Read this.

Adding system("PAUSE"); before the return 0; in your main will keep the window from closing...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.