While loop only runs once.

The program works but it only runs through the for loop once i tried a while loop as well.

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
int main(){
    int quit = -1;              //val 1 and 2 numbers entered by user.
    vector<int> data(0);        //quit used in while loop.
    int val1;                   //program works but only runs through once and then quits.
    int val2;
    for(quit;quit<data.size();quit++);{     //tried using both while and for here.
        int choice;
        cout<<"[1]Quit\n"<<"[2]Run\n";
        cin>>choice;
        switch(choice){
            case 1:
                quit = data.size();
                break;

            case 2:
                cout<<"Enter in two Integers:\n";
                cin>>val1>>val2;
                data.push_back(val1);
                data.push_back(val2);
                cout<<data[0]<<"\n"<<data[1];
                break;

            default:
                cout<<"Error";
                break;
        }

    }
    return 0;
}
Last edited on
after first iteration quit will incremented by 1 that will make quit 0 and your condition quit < 0 fail so your loop will be over..
use some other logic..
I feel silly for not seeing this sooner - spent 5 minutes executing the code and rethinking everything I know about for loops until I saw...you have an extra semi-colon after your for loop

for(quit;quit<data.size();quit++);{

should be

for(quit;quit<data.size();quit++){

Note that a while loop makes more sense for this type of logic.

@Hitesh, the logic is fine, case 2 increases the size of the vector.
Last edited on
I replaced line 6 for(quit;quit<data.size();quit++) with
while(quit<data.size()) and it didn't work it just returned 0, but then I
put in while(quit!=data.size)and it worked.

Thank you.
Topic archived. No new replies allowed.