Jump to content

C++ Loops

Skreedles
Go to solution Solved by CornOnJacob,

It should be "firstnumber++" not "Firstnumber + firstnumber++". The ++ makes it go up by one, so you don't need more addition. You should also consider using a For loop:

for (i=firstnumber, i<=secondnumber, i++) {     cout << i;}

Forgive my bad syntax. I haven't used C++ in a long while.

I'm stumped, how do I write a loop where I put in a positive number, then I input another number and the loop counts until the otehr number and sums all of the numbers along the way.

 

Ex. I input 1,

Then I input 3

 

The program would go:

 

1

2

3

 

sum = 6.

 

how I understand it is:

 

cout << what is your first number?

cin >> firstnumber

 

cout << What is your second number?

cin>>secondnumber

 

while(firstnumber < secondnumber && firstnumber > 0)

      {

        firstnumber + firstnumber ++

       }

cout << firstnumber

 

I know I ahven't decalred all of my integers and etc. but this is the just of what I'm at.

Link to comment
Share on other sites

Link to post
Share on other sites

It should be "firstnumber++" not "Firstnumber + firstnumber++". The ++ makes it go up by one, so you don't need more addition. You should also consider using a For loop:

for (i=firstnumber, i<=secondnumber, i++) {     cout << i;}

Forgive my bad syntax. I haven't used C++ in a long while.

[spoiler=My Current PC]AMD FX-8320 @ 4.2 Ghz | Xigmatek Dark Knight Night Hawk II | Gigabyte GA-990FXA-UD3 | 8GB Adata XPG V2 Silver 1600 Mhz RAM | Gigabyte 3X Windforce GTX 770 4GB @ 1.27 Ghz/7.25 Ghz | Rosewill Hive 550W Bronze PSU | Fractal Design Arc Midi R2 | Samsung Evo 250 GB SSD | Seagate Barracuda 1TB HDD | ASUS VS239H-P | Razer Deathadder 2013 Partlist

 

LTT Build-Off Thread: http://linustechtips.com/main/topic/35226-the-ltt-build-off-thread-no-building-required/

Link to comment
Share on other sites

Link to post
Share on other sites

If you can say what you get when compiling this that might help ;) (i think, i am also a c++ noob :P)

Link to comment
Share on other sites

Link to post
Share on other sites

You should do your own homework.

 

I didn't say "hey guys do this for me!" I asked if anyone could help me with a problem I'm having, because I'm not understanding how it is written out in my text, nor how my instructortries to explains C++.

Link to comment
Share on other sites

Link to post
Share on other sites

It should be "firstnumber++" not "Firstnumber + firstnumber++". The ++ makes it go up by one, so you don't need more addition. You should also consider using a For loop:

for (i=firstnumber, i<=secondnumber, i++) {     cout << i;}

Forgive my bad syntax. I haven't used C++ in a long while.

+=

or

-= is what I use for operations if I remember correctly

Console optimisations and how they will effect you | The difference between AMD cores and Intel cores | Memory Bus size and how it effects your VRAM usage |
How much vram do you actually need? | APUs and the future of processing | Projects: SO - here

Intel i7 5820l @ with Corsair H110 | 32GB DDR4 RAM @ 1600Mhz | XFX Radeon R9 290 @ 1.2Ghz | Corsair 600Q | Corsair TX650 | Probably too much corsair but meh should have had a Corsair SSD and RAM | 1.3TB HDD Space | Sennheiser HD598 | Beyerdynamic Custom One Pro | Blue Snowball

Link to comment
Share on other sites

Link to post
Share on other sites

It should be "firstnumber++" not "Firstnumber + firstnumber++". The ++ makes it go up by one, so you don't need more addition. You should also consider using a For loop:

for (i=firstnumber, i<=secondnumber, i++) {     cout << i;}

Forgive my bad syntax. I haven't used C++ in a long while.

 

I finally found this in my book.

Link to comment
Share on other sites

Link to post
Share on other sites

it looks like you already know that you need to declare the integers, so that might be a good start

then, you could try pushing "compile" and see what the compiler has to say

Link to comment
Share on other sites

Link to post
Share on other sites

+=

or

-= is what I use for operations if I remember correctly

"++" is the same as "+= 1" and "--" is the same as "-= 1". Either one should work.

[spoiler=My Current PC]AMD FX-8320 @ 4.2 Ghz | Xigmatek Dark Knight Night Hawk II | Gigabyte GA-990FXA-UD3 | 8GB Adata XPG V2 Silver 1600 Mhz RAM | Gigabyte 3X Windforce GTX 770 4GB @ 1.27 Ghz/7.25 Ghz | Rosewill Hive 550W Bronze PSU | Fractal Design Arc Midi R2 | Samsung Evo 250 GB SSD | Seagate Barracuda 1TB HDD | ASUS VS239H-P | Razer Deathadder 2013 Partlist

 

LTT Build-Off Thread: http://linustechtips.com/main/topic/35226-the-ltt-build-off-thread-no-building-required/

Link to comment
Share on other sites

Link to post
Share on other sites

"++" is the same as "+= 1" and "--" is the same as "-= 1". Either one should work.

Yerp that's correct, x += y means add y to x and where as x++ means increment x so in loops if you just want to increase by 1 then you'd use x++ but if you'd want to increment lets say by a dynamic integer then you'd use x += y where y is the dynamic int.

Same thing goes for -= and -- but that instead subtracts and decrements

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah like @Nallown said it, however if the shorthand confuses you, its OK to use y = x + y.

Comb it with a brick

Link to comment
Share on other sites

Link to post
Share on other sites

"++" is the same as "+= 1" and "--" is the same as "-= 1". Either one should work.

 

Yerp that's correct, x += y means add y to x and where as x++ means increment x so in loops if you just want to increase by 1 then you'd use x++ but if you'd want to increment lets say by a dynamic integer then you'd use x += y where y is the dynamic int.

Same thing goes for -= and -- but that instead subtracts and decrements

 

Yeah like @Nallown said it, however if the shorthand confuses you, its OK to use y = x + y.

 

i think he needs a working algorithm first, maybe with a code which actually compiles, that'd be sweet

Link to comment
Share on other sites

Link to post
Share on other sites

i think he needs a working algorithm first, maybe with a code which actually compiles, that'd be sweet

@Skreedles already has this (which is the right idea, just broken):

while(firstnumber < secondnumber && firstnumber > 0)      {        firstnumber + firstnumber ++       }cout << firstnumber

If he changes it to this, then it should work just fine:

while(firstNumber <= secondNumber && firstNumber > 0) //Capitalization of vars makes it easier to read, no functional change{        cout << firstNumber;        firstNumber++;}

If he wants the sum of the numbers too, then he can do this:

while(firstNumber <= secondNumber && firstNumber > 0)      {        cout << firstNumber;        sumOfNumbers += firstNumber;        firstNumber++;      }cout << sumOfNumbers;

[spoiler=My Current PC]AMD FX-8320 @ 4.2 Ghz | Xigmatek Dark Knight Night Hawk II | Gigabyte GA-990FXA-UD3 | 8GB Adata XPG V2 Silver 1600 Mhz RAM | Gigabyte 3X Windforce GTX 770 4GB @ 1.27 Ghz/7.25 Ghz | Rosewill Hive 550W Bronze PSU | Fractal Design Arc Midi R2 | Samsung Evo 250 GB SSD | Seagate Barracuda 1TB HDD | ASUS VS239H-P | Razer Deathadder 2013 Partlist

 

LTT Build-Off Thread: http://linustechtips.com/main/topic/35226-the-ltt-build-off-thread-no-building-required/

Link to comment
Share on other sites

Link to post
Share on other sites

It should be "firstnumber++" not "Firstnumber + firstnumber++". The ++ makes it go up by one, so you don't need more addition. You should also consider using a For loop:

for (i=firstnumber, i<=secondnumber, i++) {     cout << i;}

Forgive my bad syntax. I haven't used C++ in a long while.

Thank you I ended up adapting off of this.

Link to comment
Share on other sites

Link to post
Share on other sites

Thank you I ended up adapting off of this.

Happy to help :)

If you're all set then hit "Mark Solved" on the post that solved your problem.

[spoiler=My Current PC]AMD FX-8320 @ 4.2 Ghz | Xigmatek Dark Knight Night Hawk II | Gigabyte GA-990FXA-UD3 | 8GB Adata XPG V2 Silver 1600 Mhz RAM | Gigabyte 3X Windforce GTX 770 4GB @ 1.27 Ghz/7.25 Ghz | Rosewill Hive 550W Bronze PSU | Fractal Design Arc Midi R2 | Samsung Evo 250 GB SSD | Seagate Barracuda 1TB HDD | ASUS VS239H-P | Razer Deathadder 2013 Partlist

 

LTT Build-Off Thread: http://linustechtips.com/main/topic/35226-the-ltt-build-off-thread-no-building-required/

Link to comment
Share on other sites

Link to post
Share on other sites

@Skreedles already has this (which is the right idea, just broken):

it looked more like a wrong syntax for a wrong algorithm

 

If he wants the sum of the numbers too, then he can do this:

that's actually the only thing that he asked for in the first post

 

he can do it like this

while(firstNumber <= secondNumber)         sum += firstNumber++;
Link to comment
Share on other sites

Link to post
Share on other sites

 

it looked more like a wrong syntax for a wrong algorithm

 

that's actually the only thing that he asked for in the first post

 

he can do it like this

while(firstNumber <= secondNumber)         sum += firstNumber++;

Might aswell use for then

for(int x = firstNumber; x <= secondNumber; x++){sum += x;}
Link to comment
Share on other sites

Link to post
Share on other sites

Might aswell use for then

for(int x = firstNumber; x <= secondNumber; x++){sum += x;}

i didn't go for a 'for' loop because the initialization is already done, by the way it's almost a matter of taste, the thing that the OP should consider is the usage of a totalizer and a loop

will he? won't he? god knows

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×