我的代码出了什么问题? [英] what is wrong with my code?

查看:93
本文介绍了我的代码出了什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一个程序的代码,假设能够解决这个问题,我可以将i从1升级到某个整数n。


但是,当我实现代码到Visual C ++时,它不起作用。

网站提供的原始示例代码:


unsigned int Sum(unsigned int n){

unsigned int result = 0;

for(unsigned int i = 1; i< = n; i ++){

结果+ = 1;

返回结果;

}

}


和此代码的网络链接是 http://www.brpreiss.com /books/opus4/html/page37.html


现在,我试图包括整个主要功能,cin和cout所有这些

所以我可以运行并测试它。现在代码看起来像这样:

----------------------------------- ----------------------------------------------

1 #include< iostream.h //赋值号5087

2

3unsigned int Sum(unsigned int n);

4

5unsigned int Sum(unsigned int n){

6

7 cout<<"请输入n:"< ;< endl;

8 cin>> n;

9

10 unsigned int result = 0;

11 for(unsigned int i = 1; i< = n; i ++){

12结果+ = 1;

13返回结果;

14}

15}

16

17无效主要{


a =总和;

cout<<" Sum is"<< a<< endl;


}

- -------------------------------------------------- --------

现在编译器告诉我我有两个错误。错误消息

是:

C:\Documents and Settings \Owner\My Documents\University\Data

结构和算法实例1 \Cpp1.cpp(17):错误C2182:

''main'':非法使用类型''void''


C:\Documents and Settings \Owner\My Documents\University\Data

结构和算法\示例1 \Cpp1.cpp(17):错误C2239:

意外令牌'''''''''''''''''''''''''''''''''''是和

非法使用void类型,这很奇怪,因为到目前为止,我所有的

作业都是使用void main完成的。另一个是一些

意外令牌。来人帮帮我!我是在茫茫茫茫大海的混乱之中淹没的! :}

解决方案

da ******@brentwood.bc.ca 写道:


16

17 void main {


a =总和;

cout<<" Sum is"<<<<< endl;


}

--------------------------------------- --------------------

现在编译器告诉我我有两个错误。错误消息

是:

C:\Documents and Settings \Owner\My Documents\University\Data

结构和算法实例1 \Cpp1.cpp(17):错误C2182:

''main'':非法使用类型''void''


C:\Documents and Settings \Owner\My Documents\University\Data

结构和算法\示例1 \Cpp1.cpp(17):错误C2239:

意外令牌'''''''''''''''''''''''''''''''''''是和

非法使用void类型,这很奇怪,因为到目前为止,我所有的

作业都是使用void main完成的。另一个是一些

意外令牌。来人帮帮我!我是在茫茫茫茫大海的混乱之中淹没的! :}



忽略其余的代码; main返回int,所以void main()是

格式错误。你在'main'之后也缺少括号。


-

Ian Collins。


< blockquote>


>

忽略其余的代码; main返回int,所以void main()是

格式错误。你在'main'之后也缺少括号。


-

Ian Collins。



大声笑。我不敢相信这很简单。谢谢。


这是什么意思?


C:\Documents and Settings \Owner \\ \\我的文件\大学\数据

结构和算法\示例1 \Cpp1.cpp(14):警告C4715:

''总和'':并非所有控制路径都返回值


I found the code for a program that is suppose to be able to work out
the sumation of i from 1 to a certain integer n.

however, when I implemented to code into Visual C++, it doesn''t work.
The original sample code provided by the website:

unsigned int Sum (unsigned int n) {
unsigned int result = 0;
for (unsigned int i=1; i<=n; i++) {
result+=1;
return result;
}
}

and the weblink for this code is http://www.brpreiss.com/books/opus4/html/page37.html

now, I tried to include the whole main function, cin and cout all that
stuff so I can run it and test it. Now the code looks like this:
---------------------------------------------------------------------------------
1#include <iostream.h//assignment number 5087
2
3unsigned int Sum (unsigned int n);
4
5unsigned int Sum (unsigned int n) {
6
7 cout<<"Please enter n: "<<endl;
8 cin>>n;
9
10 unsigned int result = 0;
11 for (unsigned int i=1; i<=n; i++) {
12 result+=1;
13 return result;
14 }
15}
16
17 void main {

a=Sum;
cout<<"Sum is "<<a<<endl;

}
-----------------------------------------------------------
Now the compiler tells me I have got two errors. The error messages
are:
C:\Documents and Settings\Owner\My Documents\University\Data
Structures and Algorithms\sample 1\Cpp1.cpp(17) : error C2182:
''main'' : illegal use of type ''void''

C:\Documents and Settings\Owner\My Documents\University\Data
Structures and Algorithms\sample 1\Cpp1.cpp(17) : error C2239:
unexpected token ''{'' following declaration of ''main''.

In other words, there are two things wrong with line 17. one is and
illegal use of type void, which is weird since just about all of my
assignments so far have been done with void main. The other is some
"unexpected token". Somebody help me! I am about the drown in the vast
sea of confusion! :}

解决方案

da******@brentwood.bc.ca wrote:

16
17 void main {

a=Sum;
cout<<"Sum is "<<a<<endl;

}
-----------------------------------------------------------
Now the compiler tells me I have got two errors. The error messages
are:
C:\Documents and Settings\Owner\My Documents\University\Data
Structures and Algorithms\sample 1\Cpp1.cpp(17) : error C2182:
''main'' : illegal use of type ''void''

C:\Documents and Settings\Owner\My Documents\University\Data
Structures and Algorithms\sample 1\Cpp1.cpp(17) : error C2239:
unexpected token ''{'' following declaration of ''main''.

In other words, there are two things wrong with line 17. one is and
illegal use of type void, which is weird since just about all of my
assignments so far have been done with void main. The other is some
"unexpected token". Somebody help me! I am about the drown in the vast
sea of confusion! :}

Ignoring the rest of the code; main returns int, so void main() is
ill-formed. You are also missing the parenthesis after "main".

--
Ian Collins.


>
Ignoring the rest of the code; main returns int, so void main() is
ill-formed. You are also missing the parenthesis after "main".

--
Ian Collins.


LOL. I can''t believe it is something that simple. Thank you.


What does this mean?

C:\Documents and Settings\Owner\My Documents\University\Data
Structures and Algorithms\sample 1\Cpp1.cpp(14) : warning C4715:
''Sum'' : not all control paths return a value


这篇关于我的代码出了什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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