consecutive numbers

can anyone tell me what is wrong with my code please. We have to come up with a code to promt the user to enter the size of the herd. Then come up with the sum of consecutive numbers add up to the number of the herd. Example: if the size of the heard is 5 then the number of ways the sum of consecutive numbers is 5 is 2. 5=5, 2+3=5. My code does not work though. can anyone help me out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
include<iostream>
using namespace std;

main()
{
int numHerd = 0;
int sum = 0;
int numOfWays = 1;
int numToTest = 0;
int counter = 0;

cout<< "enter the size of the herd";
cin >> numHerd;

numToTest=numHerd;
counter = numToTest-1;

while(numToTest>0)
  {
    sum=0;
  }

while (sum<numHerd)
  {
   sum = numToTest+counter;
   counter--;
  }
if(sum==numHerd)
  {
   numOfWays++;
  {
  numToTest--;

cout<< "The number of ways is " <<numOfWays<<endl;

return 0;
}
Hello rascon4444,

To get you started.

The first while loop is an endless loop. As ling as "numToTest" is greater than zero you will stay in the loop for ever because there is no way out of the loop. Also this while loop is not needed because on line 7 you define "sum" and set its value to zero. There is no need to do it again and especially in a while loop.

The second while loop I am not sure what you are trying to do there, but I do not believe it has much use.

The following if will only be checked once before the program ends. and line 32 will only be done once before the program ends.

My first thought is a nested for loop to check all the combination of numbers with an if statement in the inner for loop to check the combination and add to a counter.

I am going to test the for loop idea unless someone else has a better one.

Almost for got line 31 the curly brace is backwards.

Hope that helps,

Andy
Hello rascon4444,

Using the nested for loop the program outputs this:

Enter the size of the herd: 5

 The combination 1 + 4 = 5.

 The combination 2 + 3 = 5.

 The combination 3 + 2 = 5.

 The combination 4 + 1 = 5.

 The combination 5 + 0 = 5.

 The number of ways is 5.

 The number of ways is 6 if you include 5 = 5.


 Press Enter to continue


Just to give you an idea.

Andy
I have not learned the for loop in class. The teacher only gave us, while, else, and do loops. I looked at for loops out of class but still do not understand them fully. My professor said it can be done with the loops taught in class.
ignoring range based for a moment, all 3 classical loops can do the same work. The differences is just making it a little easier to read.

do-while ... is used so you can do the loop body once no matter what, and more times if needed by the condition. this avoids the ugly of having a bit of code outside the loop (initialize a variable to force 1 iteration, or repeated code block, etc). But if you can't use a do-while (for artificial reasons like professor rules) the work-around will do it.

while can do the work of a for loop but you may need extra conditional code or loop variable tracking inside the loop

for can pretend to be a while, just leave off the extra loop variable parts
eg for(; condition ;) is a while loop effectively.

classic for loop structure is not that hard, but it needs an explain:
for(initialize variable, condition, action that should really be loop variable modification)
or in real code:
for(int i = 0; i < 1000; i++) (loops from 0 to 999, which is 1000 times (zero counts as one go)

you can abuse for loops to do the loop body in the action area and to initialize other values that are not part of the loop structure. This can save space or be nice depending on what you are doing, but it can also be convoluted and hard to read, for example
for(int x = 3, int i = 0; i < 1000; cout<<i*x<<endl);

which is a lot to say that you can do it with a while loop for sure. See if you can get it to work without help, but ask if you get stuck.
will do. thank you guys so much for your help
Topic archived. No new replies allowed.