D
davy.zou
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! :}
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! :}