473,664 Members | 2,728 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is wrong with my code?

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\Unive rsity\Data
Structures and Algorithms\samp le 1\Cpp1.cpp(17) : error C2182:
'main' : illegal use of type 'void'

C:\Documents and Settings\Owner\ My Documents\Unive rsity\Data
Structures and Algorithms\samp le 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! :}

May 25 '07 #1
22 2025
da******@brentw ood.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\Unive rsity\Data
Structures and Algorithms\samp le 1\Cpp1.cpp(17) : error C2182:
'main' : illegal use of type 'void'

C:\Documents and Settings\Owner\ My Documents\Unive rsity\Data
Structures and Algorithms\samp le 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.
May 25 '07 #2
>
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.

May 25 '07 #3
What does this mean?

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

May 25 '07 #4
da******@brentw ood.bc.ca wrote:
What does this mean?

C:\Documents and Settings\Owner\ My Documents\Unive rsity\Data
Structures and Algorithms\samp le 1\Cpp1.cpp(14) : warning C4715:
'Sum' : not all control paths return a value
Please retain some context.

If you look again, you will see that you have your return inside the for
loop. You probably want it outside of the loop.

--
Ian Collins.
May 25 '07 #5
On May 24, 10:23 pm, Ian Collins <ian-n...@hotmail.co mwrote:
davy....@brentw ood.bc.ca wrote:
What does this mean?
C:\Documents and Settings\Owner\ My Documents\Unive rsity\Data
Structures and Algorithms\samp le 1\Cpp1.cpp(14) : warning C4715:
'Sum' : not all control paths return a value

Please retain some context.

If you look again, you will see that you have your return inside the for
loop. You probably want it outside of the loop.

--
Ian Collins.
how is it that your can reply like right after someone makes a post?
not that I am nor grateful or something, I am very grateful, but do
you like get paid to do this?

May 25 '07 #6
How do I clear a variable at the end of a while loop?

May 25 '07 #7
da******@brentw ood.bc.ca wrote:
On May 24, 10:23 pm, Ian Collins <ian-n...@hotmail.co mwrote:
>davy....@brent wood.bc.ca wrote:
>>What does this mean?
C:\Document s and Settings\Owner\ My Documents\Unive rsity\Data
Structures and Algorithms\samp le 1\Cpp1.cpp(14) : warning C4715:
'Sum' : not all control paths return a value
Please retain some context.

If you look again, you will see that you have your return inside the for
loop. You probably want it outside of the loop.

--
Ian Collins.
Please don't quote people's signatures, the lines after the "-- ".
>
how is it that your can reply like right after someone makes a post?
not that I am nor grateful or something, I am very grateful, but do
you like get paid to do this?
I just happened to be sitting here!

--
Ian Collins.
May 25 '07 #8
On May 25, 1:58 pm, davy....@brentw ood.bc.ca wrote:
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 ishttp://www.brpreiss.co m/books/opus4/html/page37.html
You've made several transcription errors:
* You added extra { }
* You changed "result += i" to "result+=1"
* You changed ++i to i++

The first two drastically change the effect of the code.
1#include <iostream.h//assignment number 5087
(Please don't post line-number listings , it just
makes it harder for us to put your code in our
compilers).

There is no such standard header as <iostream.h.
The C++ standard came out in 1998 so that suggests
your learning materials are 10 years old or more.
You could remedy this by writing:
#include <iostream>
using namespace std;

which has a similar effect to what some old compilers
did when you wrote #include <iostream.h.
3unsigned int Sum (unsigned int n);
4
5unsigned int Sum (unsigned int n) {
Line 3 is redundant (although this isn't a terrible error).
17 void main {
Should be:
int main() {

main must return an int in Standard C++. While there
are some compilers that also allow it to return void,
you may find that your code fails when you try it on
a different compiler.
a=Sum;
Should be:
a = Sum();

You need to use the brackets to indicate that the
function should be called.
May 25 '07 #9
On May 25, 4:03 pm, Old Wolf <oldw...@inspir e.net.nzwrote:
On May 25, 1:58 pm, davy....@brentw ood.bc.ca wrote:
unsigned int Sum (unsigned int n) {

a=Sum;

Should be:
a = Sum();
In fact, it should be:
a = Sum(5);

or some other number as an argument to the function.
May 25 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
14706
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
72
5833
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for example, between a C/C++ programmers with a few weeks of experience and a C/C++ programmer with years of experience. You don't really need to understand the subtle details or use the obscure features of either language
121
10026
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
51
13349
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this wrong????? Function x select case z
56
4318
by: Cherrish Vaidiyan | last post by:
Frinds, Hope everyone is doing fine.i feel pointers to be the most toughest part in C. i have just completed learning pointers & arrays related portions. I need to attend technical interview on C. wat type of questions should be expected? Which part of C language do the staff give more concern? The interviewers have just mentioned that .. i will have interview on C. Also can anyone can help me with sites where i can go thru sample
46
4197
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do believe MSFT should do to improve C#, however. I know that in the "Whidbey" release of VS.NET currently
13
5035
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
98
4568
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
9
2117
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
20
2806
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
0
8348
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4185
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2764
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1759
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.