不知道这个错误意味着什么? [英] Don't know what this error means?

查看:66
本文介绍了不知道这个错误意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我是非常新的c ++,就像我昨晚开始学习一样。



无论如何,我正在编写一个简单的教程代码。



然而,我收到错误,其中我不知道如何处理甚至是什么意思。



我得到的错误是;



错误C2062:输入'int'意外

错误C2059:语法错误:';'



任何帮助表示赞赏!!



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!!

#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<<;

}

推荐答案

查看您的代码和错误消息。该消息告诉您在代码中找到错误的位置,并且通常至少会指定一个行号(在VS中,您可以通过双击错误消息直接转到该行,或按CTRL + G转到一行数字)



在这种情况下,它非常明显:

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:
int totalage;
int averageage;

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



编译器认为您尝试将变量声明为 cin 操作,这是不允许的 - 考虑一下,变量在逻辑上只存在于操作的持续时间,因此该值在其他地方不可用!

请尝试这样做:


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:

int totalage;
int averageage;
int age1;

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


OriginalGriff为您提供答案(并向我发送+5);让我给你一些进一步的建议。您已声明所有int变量并且尚未初始化它们。这是许多错误的根源。我试图区分以下两种代码编写方式:



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:

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




// Strategy 2
int x = ...;





在第一种情况下,代码提供了使用未定义变量的机制。这也涉及更多代码来做同样的事情。这也为程序员忘记变量提供了一种机制,这不是一个主要问题,但它只是编码胆固醇,它会阻塞你的编程动脉,(2)使代码更难理解。阅读它。



在第二种情况下,变量在一个语句中定义和初始化。这两个动作结合在一起,阅读代码的人会发现它更容易理解。为了进一步改进这一步骤(仅限C ++;在C中不起作用),在需要之前不应定义和初始化变量。对于上面的代码,totalage和averageage值应该从顶部的行移动,而是在你进行计算的两行上定义和初始化。



有许多研究表明,错误的数量与程序中的代码行数相关,同样,具有分布式逻辑的程序(使用多行执行一项操作)更难以理解和维护。简而言之,代码更少是更好的代码。



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".


Solution1和2非常好,但我建议你在C ++学习中再做一步并使用loop [ ^ ]。



示例代码:

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:
#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 ;)


这篇关于不知道这个错误意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆