Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Im extremely new to c++ as in I started learning last night.

Anyway, I'm writing a simple code for a tutorial.

However, I am getting errors in which I have no idea how to deal with or even what they mean.

The errors I'm getting are;

error C2062: type 'int' unexpected
error C2059: syntax error : ';'

Any help is appreciated!!

C++
#include<iostream>
using namespace std;

void main()

{
	int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10;
	int totalage;
	int averageage;

	cout<<"Please enter the age of Student 1:\n";
	cin>>int age1;

	cout<<"Please enter the age of Student 2:\n";
	cin>>int age2;

	cout<<"Please enter the age of Student 3:\n";
	cin>>int age3;

	cout<<"Please enter the age of Student 4:\n";
	cin>>int age4;

	cout<<"Please enter the age of Student 5:\n";
	cin>>int age5;

	cout<<"Please enter the age of Student 6:\n";
	cin>>int age6;

	cout<<"Please enter the age of Student 7:\n";
	cin>>int age7;

	cout<<"Please enter the age of Student 8:\n";
	cin>>int age8;

	cout<<"Please enter the age of Student 9:\n";
	cin>>int age9;

	cout<<"Please enter the age of Student 10:\n";
	cin>>int age10;

	totalage = age1+age2+age3+age4+age5+age6+age7+age8+age9+age10;
	averageage = totalage/10;

	cout<<"The average age of the class is" <<averageage<<;

}
Posted
Updated 9-Aug-13 20:49pm
v2
Comments
Mohibur Rashid 10-Aug-13 2:54am    
when you pass a variable, you do not need to pass the variable type just the variable.
example
cin>>age1; is valid
cin>>int age1; is invalid, int was unexpected there
Dennis C. Dietrich 10-Aug-13 3:10am    
Yep. Why don't you repost your comment as a solution? :)
Mohibur Rashid 10-Aug-13 4:57am    
:)

Look at your code, and the error message. The message tells you where in your code it finds the error, and will normally specify at least a line number (In VS, you can go direct to the line by double clicking the error message, or pressing CTRL+G to go to a line number)

In this case it's pretty obvious:
CSS
int totalage;
int averageage;

cout<<"Please enter the age of Student 1:\n";
cin>>int age1;

The compiler thinks you are trying to declare a variable as part of the cin operation, which is not allowed - think about it, the variable would logically only exist for teh duration of the operation, so the value wouldn't be available elsewhere!
Try this instead:
CSS
int totalage;
int averageage;
int age1;

cout<<"Please enter the age of Student 1:\n";
cin>>age1;
 
Share this answer
 
v2
Comments
MyOldAccount 10-Aug-13 12:46pm    
Great explanation! +5!
Maciej Los 10-Aug-13 17:39pm    
Agree!
Please, see my answer ;)
H.Brydon 10-Aug-13 18:09pm    
+5. Ears burning?
OriginalGriff gives you the answer (and a +5 to him from me); let me give you some further advice. You have declared all of your int variables and have not initialized them. This is the source of many bugs. I am trying to differentiate the following 2 styles of code writing:

C++
// Strategy 1
int x;
// ...
x = ...;

// Strategy 2
int x = ...;


In the first case, the code provides a mechanism for using an undefined variable. This also involves more code to do the same thing. This also provides a mechanism for the programmer to 'forget' about a variable, which is not a major problem, but it is just coding cholesterol which (1) clogs up your programming arteries and (2) makes the code harder to understand for someone reading it.

In the second case, the variable is defined and initialized in one statement. These two actions go together, and someone reading the code will find it easier to understand. To improve this a step further, (C++ only; doesn't work the same in C) the variable should not be defined and initialized until it is needed. For your code above, the totalage and averageage values should be moved from the lines at the top and instead be defined and initialized on the two lines where you do the calculations.

There are many studies showing that the number of bugs correlate with the number of lines of code in a program, and likewise that programs with distributed logic (using multiple lines to do one thing) are harder to understand and maintain. In short, "less code is better code".
 
Share this answer
 
Comments
Maciej Los 10-Aug-13 17:36pm    
Good answer, +5!
Initializing variables is a good practice.
Please, see my answer ;)
Solution1 and 2 are very good, but i would suggest you to do one more step in C++ learning and to use loop[^].

Example code:
C++
#include<iostream>
using namespace std;
 
void main()
 
{
	int studentscount=0;
	int age=0;
	int totalage=0;
	int averageage=0;
 
       	cout<<"Please enter the count of Students:\n";
	cin>>studentscount;
        for (int i=1;i<=studentscount;i++)
        {
        	cout<<"Please enter the age of Student:" << i << "\n";
        	cin>>age;
                totalage += age;
        }
  	averageage = totalage/studentscount;
 	cout<<"The average age of the class is" <<averageage<<;
}</iostream>


Note: Straight from head... Not tested! You have been warned ;)
 
Share this answer
 
v4
Comments
pasztorpisti 10-Aug-13 17:53pm    
+5 for the effort but you forgot to untab/escape your code! :-P
You should also handle invalid input (failbit and its friends in case of iostream with an additional inner loop). In case of invalid input the invalid data remains in the input stream and that is a pain in the ass to handle with iostreams (like many other simple tasks). If you write in something invalid then your loop starts spinning as all attempts to read cin will fail... btw, have I mentioned enough times that I hate c++ iostreams for several reasons? I'm a big OO fun but c++ iostreams sucks... Its a step back from the old plain C stdio in simplicity and ease of use in some aspects. Try to handle the invalid input and you will see what I mean, then do the same with scanf...
H.Brydon 10-Aug-13 18:06pm    
Great analysis; I owe you another +5 - watch for it (I gave you several others in the last day or two). I also share your affinity for OO and disdain for iostreams.
Maciej Los 10-Aug-13 18:08pm    
Thank you ;)
pasztorpisti 10-Aug-13 18:11pm    
Thank you! I've already given your 5 for the bug-avoiding coding style related advices. :-)
H.Brydon 10-Aug-13 18:14pm    
What goes around comes around. Keep watching. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900