注册 登录
编程论坛 C++教室

此源程序错在什么地方

unsigend 发布于 2010-05-17 17:06, 745 次点击
#include<iostream>
void main()
{
 int i(1),sum(0);
 while(i<=10)
(
 sum+=i;
 i++;
 )
cout<<"sum="<<sum<<endl;
}
下面是编绎出错信息
------------Configuration: Cpp3 - Win32 Debug--------------------
Compiling...
Cpp3.cpp
f:\vcycx\cpp3.cpp(7) : error C2143: syntax error : missing ')' before ';'
f:\vcycx\cpp3.cpp(9) : error C2059: syntax error : ')'
f:\vcycx\cpp3.cpp(10) : error C2065: 'cout' : undeclared identifier
f:\vcycx\cpp3.cpp(10) : error C2297: '<<' : illegal, right operand has type 'char [5]'
f:\vcycx\cpp3.cpp(10) : error C2065: 'endl' : undeclared identifier
执行 cl.exe 时出错.

Cpp3.exe - 1 error(s), 0 warning(s)
7 回复
#2
最近不在2010-05-17 17:20
程序代码:
#include<iostream.h>   //头文件用<iostream.h>掉了.h或者#include<iostream> using namespace std;

void main()
{
int i(1),sum(0);
while(i<=10)       //循环体内用{},不是()
{
sum += i;
i++;
}
cout<<"sum="<<sum<<endl;
}
#3
小哨兵2010-05-19 20:44
呵呵学习了
#4
fhqbgmqj2010-05-21 14:09
while循环、for循环、判断的执行语句等用大括号{},函数参数、判断循环条件用()。

[ 本帖最后由 fhqbgmqj 于 2010-5-21 14:11 编辑 ]
#5
流氓之父2010-05-21 19:35
#include<iostream.h>
void main()
{
int i(1),sum(0);
while(i<=10)
{
sum+=i;
i++;
}
cout<<"sum="<<sum<<endl;
}
注意头文件的使用;2楼说的就很对啊!
#6
神左手2010-05-21 22:36
知道循环次数的最好用for语句

while(i<=10)改为for(int i;i<=10;i++)
#7
heliujin2010-05-23 07:42
头文件最好有标准C++的
#include<iostream>
using namespace std;
#8
maomingao2010-05-23 23:39
要是都像二楼一下这么细心的回答多好呀
1