Trouble With For Loops

I am a beginning CS major and have an assignment to code a program where the user assigns a starting number and an ending number. The values are taken into a for loop where the starting number increments by 1 until it reaches the end value. it should display the numbers incrementing and then the total of all the numbers incremented and how many times the loop ran. for example, a starting number of 1 and an ending number of 3 would display as "1 + 2 + 3 = 6. The loop ran 3 times." My problem is that without the break in the for loop, the loop runs without an end. Also, in the initial run, no matter which starting number is entered, the loop always starts at 0. Then, the starts at the previous end value. If you input a start number lower than the previous end number, it will only display the end number values and onward. I thought this would be prevented by reinitializing the values to 0 every time the user wants to try again? Can anyone give me a hint as to what I could be doing wrong? I don't want an answer, just a hint. Thank you for your time.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  #include <iostream>
#include <iomanip>

using namespace std;

int main ()
{

    char again = 'y';
    while (again == 'y')
    {
        int total = 0, counter = 0, startnum = 0, endnum = 0;

        cout << "\nEnter a starting number: ";
        cin >> startnum;
        cout << "\n";

        if (startnum < 0)
        {
        cout << "No negative numbers allowed. Converting to positive value.\n";
        startnum = startnum * (-1);
        }

        cout << "starting number is " << startnum;
        cout << "\nEnter an ending number (must be higher than starting number): ";
        cin >> endnum;

        while (endnum <= startnum)
        {
        cout << "Your end number can't be higher than the starting number. Enter a valid number: ";
        cin >> endnum;
        }

        cout << "\nend number is " << endnum << "\n";

        for (int startnum; endnum; startnum++)
        {
            total = total + startnum;

            if (startnum < endnum)
            {
                cout << startnum << " + ";
                counter += 1;
            }

            else
            {
                cout << startnum << " = " << total << "\n";
                cout << "The loop ran " << counter << " times.\n";
                break;

            }
        }

        cout << "after the loop the starting value is " << startnum << " end number is " << endnum << "\n";

        cout << "Run this again? (Y or N): ";
        cin >> again;
        again = tolower(again);

    }

}

for (int startnum; endnum; startnum++)
You're declaring another instance of startnum, which is different from the one in main. It is also uninitialised, leading to undefined behaviour.
Your condition (endum), if non-zero, will cause the for loop to run indefinitely.

startnum = startnum * (-1);
can be shortened to
startnum *= -1;
Likewise with total = total + startnum;
i see. i had to assign n = startnum and then assign n <= endnum. I thought by just putting startnum and endnum the variables would already be set in place since they were given values beforehand. now it works. I understand For Loops a lot better now, thank you.
Topic archived. No new replies allowed.