loops and sums

Pages: 12
closed account (oz80RXSz)
...
Last edited on
Ask more specifically
closed account (oz80RXSz)
Ok i made the question more specific.
Ok do you know what a while (true) loop is?


integer current = 1
integer totalSum = 0

while != 5000

add current to totalSum

totalSum ++

end

if you want to print it use cout << totalSum;


(yes you do actually have to do a little work)
Last edited on
closed account (oz80RXSz)
.
Last edited on
How much C++ do you know?
Is this for a class?
*sigh to declare a integer you type something like this:
int current = 1;
note the semi colon to end the line
closed account (oz80RXSz)
a week much =]]
Actually not for a class. We did some examples about this in class but I don't get it,so now this is something I am trying to figure out how to do
When you want to declare a while loop you use something like this:
1
2
3
while (totalSum != 5000){
//do stuff
}


the curly bracket "{" starts it and "}"closes it
note that I asked it to do what we want in parentheses ()
and finally // is a comment
*sigh please pay attention next time around please
ok next to add something together you use this:
1
2
this += that;
//not that hard 


what this does is it adds this to that

basically this = this + that;

note the semicolon at the end its required
closed account (oz80RXSz)
#include <iostream>
using namespace std;
int main()
{
int Sum = 0
int current = 1

sum+1=sum


while (Sum!= 5000)

totalSum ++

cout<<totalSum;
cout<<The last number

endl;
}



isnt it something like this??
And finally watch this:
1
2
++this;
cout << this << endl;


note that this time I used ++var instead you can use both but this one is basically might take the sam amount of time to proccess but it won't take longer and to end the line you should use it like that

note the semicolon at the end

now put the code together

by the way what compiler do you use?
closed account (oz80RXSz)
Microsoft Visual
Did you read all I posted?
also use the code block when posting you code
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 current = 1

sum+1=sum


while (Sum!= 5000)

totalSum ++

cout<<totalSum;
cout<<The last number

endl;
}


also at the top your variable is capitalized at the top then you un-cap it when you add it the re-cap it in the while loop
why are you doing this on line 8:
sum+1=sum

if you want to do that you should use this instead:

sum += 1;

however this has the same effect:
++sum;

Also !!!!!!! DON'T FORGET TO USE SEMICOLONS AT THE END OF YOUR LINE
Last edited on
closed account (oz80RXSz)
oh ok
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 totalSum = 0
int current = 1

++totalSum


while (Sum!= 5000)

totalSum ++

cout<<totalSum;
cout<<The last number

endl;
}



so if i try this is it going to work
closed account (oz80RXSz)
ofcourse with the semicolons at the end
Pages: 12