Need C++ help,

I am learning C++ from an online resource. I tried to test part of there code and it isn't working. Here is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "stdafx.h"
#include <iostream>
 
using namespace std;
int main()
{
	int age1, age2, age3, age4, age5; // These two lines are meant to 
	int TotalAge, Averageage;			//are meant to declare my variables 
	{
		cout<< "Please enter the age of student 1:"; // The next number of lines ask for the input
		cin>> age1;									// of the different ages. 
		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>> age 4;
		cout<< "Please enter the age of student 5:";
		cin>> age5;
		TotalAge = age1 + age2 + age3 + age4 + age5; //The next two lines work to determine the average age 
		AverageAge =  TotalAge / 5
		cout<< "Average age of class is:" <<AverageAge;
	}
}

The errors that i get are as follow:

1>------ Build started: Project: 123, Configuration: Debug Win32 ------
1> 123.cpp
1>123.cpp(19): error C2065: 'age' : undeclared identifier
1>123.cpp(19): error C2143: syntax error : missing ';' before 'constant'
1>123.cpp(23): error C2065: 'AverageAge' : undeclared identifier
1>123.cpp(24): error C2146: syntax error : missing ';' before identifier 'cout'
1>123.cpp(24): error C2065: 'AverageAge' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I could really use some help please. Thank you. (By the way im kinda new to commenting on my program. I wasn't sure if you guys would need it.
Last edited on
Line 17: remove the space between age and 4.
Line 8: Rename Averageage to AverageAge (C++ is case sensitive)
LB totally helped you.
And I wanna tell you that you should know error code.
1. undeclared identifier <- check if you declare the variable(like age) you use in source code.
2. unresolved external symbol somethign.. <- you didn't define a funtion that you already declared.
3... I forgot error messages. I will post them again after chcking them.
Why do you have brackets on lines 9 and 23? Remove them.
Those braces are fine there - they just make a nameless scope. They will not cause any problems.
You are also missing a semicolon ( ; ) on line 22
Add a semicolon at the end.. So:
AverageAge = TotalAge / 5;
Thanks a lot, also, how do i stopped it from closing automatically at the end
closed account (zb0S216C)
Magicalyak wrote:
Thanks a lot, also, how do i stopped it from closing automatically at the end

Use std::cin.get( ). Never, ever use system( "Pause" ).

Wazzak
@Framework, except that that method only works if there is no newline left in the input stream. This is the better solution:

1
2
3
std::cin.sync();
std::cout << "Press any key to continue...";
std::cin.get();
It's better to use cin.ignore() than cin.get() because then if they type more than one character future extractions from cin will not read them.
Topic archived. No new replies allowed.