while loop

Oct 18, 2017 at 6:52am
i have to create a code that will take the sum of the numbers between 0 and any number. example 0 to 5 = 15. my code is saying that i is not initialized.

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
  
#include <iostream>

using namespace std;

int main()
{
     int num = 1;
     int val;
     int sum;

     cout << "Enter an integer: ";
     cin >> val;

     while(val != 0)
     {
          int  i = num, i <= val, ++i;
          cout << "The sum from 0 to " << val << " is " << sum;
          sum += ++i;
     }
     cout << sum << endl;
     cout << "Goodbye! " << endl;

     return 0;
}
Oct 18, 2017 at 8:12am
1
2
  while(val != 0)
 int  i = num, i <= val, ++i

You appear to be trying to create some kind of mishmash of a while and an if . You can't do that. This is just a nonsense.

int i = num, i <= val, ++i
What are you trying to do with this line?


Currently, your while loop will never end, because val never changes.

Oct 18, 2017 at 6:49pm
the issue now is math. it compiles but then its a never ending loop and i just cant seem to get it to do the math it needs to do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
     int val;
     int sum = 0;
     int i = 1;

     cout << "Enter an integer: ";
     cin >> val;

     while(val != 0)
     {   
          sum += ++i;
          cout << "The sum from 0 to " << val << " is " << sum;
          cout << sum << endl;
     }
     cout << "Goodbye!" << endl;
     return 0;
}

Oct 18, 2017 at 7:36pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
    std::cout << "Enter an integer: ";
    int val = 0;
    std::cin >> val;

    int sum = 0;
    int i = 0;
    while(  ???   ) // hint: there's a variable which stores a value that is 
    {               //       supposed to grow up to the one given by the user
        sum += i++;
        std::cout << "The sum from 0 to " << i << " is " << sum << '\n';
    }
    std::cout << "Goodbye!\n";
    return 0;
}

Oct 18, 2017 at 7:48pm
SO now it does the math right. it just displays all the values. example
The sum from 0 to 5 is 1
The sum from 0 to 5 is 3
The sum from 0 to 5 is 6
The sum from 0 to 5 is 10
The sum from 0 to 5 is 15.

when its only suppose to display the last line for the value 15. and the program doesnt exit and it just stays open
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
#include <iostream>

using namespace std;

int main()
{
     int val;
     int sum = 0;
     int i = 1;

     cout << "Enter an integer: ";
     cin >> val;

     while(val != 0)
     {
          while(i <= val)
          {
               sum = sum + i;
               i++;
               cout << "The sum from 0 to " << val << " is " << sum << endl;
          }
     }
     cout << "Goodbye!" << endl;
     return 0;
}
Last edited on Oct 18, 2017 at 7:56pm
Oct 18, 2017 at 8:06pm
Line 20 is in the wrong place. It should be after line 21.

You still have the problem that you never change the value of val in the outer loop. Therefore your outer loop will never exit.

Oct 19, 2017 at 2:26am
So I only changed two things from your most recent code(see bold text). I changed your outer while to an if then just pulled your cout, outside the while loop. If the cout is inside the loop it'll print every time the while loop is executed, if its outside it'll print just once while still using the data gathered from the while loop itself. I hope I am explaining this well, and I hope this helps!

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
#include <iostream>

using namespace std;

int main()
{
	int val;
	int sum = 0;
	int i = 1;

	cout << "Enter an integer: ";
	cin >> val;

	if (val != 0)
	{
		while (i <= val)
		{
			sum = sum + i;
			i++;
		}
	}
	cout << "The sum from 0 to " << val << " is " << sum << endl;
	cout << "Goodbye!" << endl;
	return 0;
}
Topic archived. No new replies allowed.