Need help with my first program.

What we are supposed to do is answer the question "How far do we have to go to add 1,2,3,4,5,6... to reach 25000? The program is supposed to print out the sum and the last number added to make it go over 25000.

I am visually impaired, and our teacher only does lecture, he isn't very hands on. I have a note taker, but that isn't helping. Here's what I have so far.

# include <iostream>
using namespace std;

void main (void)

{
int sum = 1, n;
for (n=1; n<=25000; n=n+1)
sum= sum+n
cout <<"the sum of 1 through 25000 is"<<sum<<endl;
}
I think you should also print n in the output as they have also asked to print the last number added to make it go over 25000.
so
# include <iostream>
using namespace std;

void main (void)

{
int sum = 1, n;
for (n=1; n<=25000; n=n+1)
{
sum= sum+n;
}
cout <<"the sum of 1 through 25000 is"<<sum<<endl;
cout<<"The last number added is : "<<n<<endl;
}
THANK YOU!!!! I knew I was close, but wasn't sure exactly what I was missing.
The end condition for the loop should be sum < 25000, surely?
The initial value of sum should be zero, not one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{
    int sum = 0;
    int n = 0;

    while (sum < 25000)
    {
        n++;
        sum += n;
    }
    cout <<"the sum of 1 through " << n << " is "<<sum<<endl;
}
Last edited on
I'm not really good with all those shortcuts yet, so please be paitent with me. I have a few questions about this but this is what I came up with in well stupid beginner's terms.

# include <iostream>
using namespace std;

void main (void)

{
int sum = 0;
int n = 0;

while (sum < 25000)
{
n=n+1;
sum += n;
}
cout <<"the sum of 1 through 25000 is"<<sum<<endl;
cout<<"The last number added is : "<<n<<endl;
}

The first question I have is what is the purpose of the int n = 0; line?
Second, what is sum += n; ?

Thanks guys, you have all been a lot of help
what is the purpose of the int n = 0;

n needs to be given an initial value, before it is used later. Otherwise n could contain some random value, whatever happened to already reside in that memory location.

what is sum += n; ?

That's just a more concise way of putting sum = sum + n;

Operators, including ++, += etc. are explained in the tutorial page:
http://www.cplusplus.com/doc/tutorial/operators/

Hope this helps.



it does a LOT. More than you will EVER know! I'm working on getting my mac compiler up and running so I can see this in action!
GRR, it won't compile, this is where I wish I had access to my Windows PC and Visual Studio, I'm getting these errors.

untitled.cpp:5:210: warning: missing terminating ' character
untitled.cpp:5: error: missing terminating ' character
untitled.cpp:6: error: stray ‘\’ in program
untitled.cpp:40:60: warning: backslash-newline at end of file
untitled.cpp:40: error: stray ‘\’ in program
untitled.cpp:1: error: expected unqualified-id before ‘{’ token
What is the exact code that you are trying to compile?

By the way, it would help if you used code tags like this,

[code]your code here[/code]

- click on the <> button in the formatting options on the right.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# include <isotherm>
using namespace std;

void main (void)

{
int sum = 0;
int n = 0;

while (sum < 25000);
{
n=n+1;
sum += n;
}
cout <<"the sum of 1 through 25000 is"<<sum<<end;
cout<<"The last number added is : "<<n<<endl;
}
Thanks for sharing the code. I don't get any of the errors listed above. But there are some other errors.

Line 1, isotherm should be iostream
Line 4. void main should read int main - at least for my compiler. My understanding is that the use of void here is non-standard won't work on all compilers.
Line 10 - remove the semicolon - not needed here.
Line 15 - end should be endl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main ()

{
    int sum = 0;
    int n = 0;

    while (sum < 25000)
    {
        n=n+1;
        sum += n;
    }
    cout <<"the sum of 1 through 25000 is"<<sum<<endl;
    cout<<"The last number added is : "<<n<<endl;
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main ()

{
    int sum = 0;
    int n = 0;

    while (sum < 25000)
    {
        n=n+1;
        sum =sum+n;
    }
    cout <<"The sum of 1 through 25000 is "<<sum<<endl;
    cout<<"The last number added is : "<<n<<endl;
    return 0;

}


This should compile. Also, you should use proper indentation. I noticed that you put ; after while() which would run the loop infinitely as ; itself is a valid statement hence loop would return to it's condition after terminating at ;. So it would continue to repeat this cycle until condition becomes false (which wouldn't become false as you are not incrementing/decrementing anything..)I've fixed the issues in the above code and it should work fine.

Thanks guys, I don't know how the first line got messed up, probably got goofed while editing it so many times, whoops. Let me fix it, and try to compile again.

EDIT, I see what you mean about the infinite loop, I'm gonna keep these posts as a reference for future programs, it will help me a lot.
Last edited on
I don't think I am meant to get this thing done, now I jsut got a single error.
1
2
error: expected unqualified-id
{\rtf1\ansi\ansicpg1252\cocoartf1187\cocoasubrtf340


I've been using GCC, but I think it's time for a new compiler, this thing is too picky.
I've no idea what that error means.
However, I should ask, are you re-typing the code fresh each time? If you do that, you may get rid of one error and introduce another.

Better to just copy and paste the complete working code all in one step.

That's what I'm doing. My exact method is gcc -O program untitled.cpp then it would give me these stupid errors. I don't know why our school has C++ as a first year course.
Topic archived. No new replies allowed.