For Loop Isn't Looping More Than Once

I need help with a for loop. On line 44 I'm trying to establish if the integer 'choice' == 5, I ask the user for a value to store in integer 'amount'. Afterwards, I start a for loop (but it doesn't work for some reason).

- I declared integer 'i' and initialized with value 0.
- The condition is true if integer 'i' is less than or equal to (<=) integer 'amount'.
- After the loop body, I want to add 1 to integer 'i' (i++).

This only results in one execution and nothing afterwards. I want my for loop to loop the value for variable 'amount'. I also want the console to output:

cout << "Contacting " << name << endl;

Every time a new name has been inputted. Thanks all.

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

using namespace std;

int main(){
    int choice, amount;
    string name;
	
    do {
        cout << "Make a choice from the following menu:" << endl;
        cout << "   1. Add a user" << endl;
        cout << "   2. Delete a user" << endl;
        cout << "   3. Modify a user" << endl;
        cout << "   4. View a user" << endl;
        cout << "   5. Contact a user" << endl;
        cin >> choice;
		
    } while ((choice <= 0) || (choice > 5));

    switch (choice) {
        case 1: {
            cout << "You chose 'add a user'" << endl;
        } break;
        case 2: {
            cout << "You chose 'delete a user'" << endl;
        } break;
        case 3: {
            cout << "You chose 'modify a user'" << endl;
        } break;
        case 4: {
            cout << "You chose 'view a user'" << endl;
        } break;
        case 5: {
            cout << "How many users? ";
            cin >> amount;
        } break;
        default: {
            cout << "Unknown choice: " << choice << endl;
        } break;
    }
	if(choice == 5){
	    cout << "Enter users:" << endl;

		for (int i = 0;(i <= amount);i++);{
			cin >> name;
			cout << "Contacting " << name << endl;
		}
	}
	else{
    return 0;}
}
Last edited on
I was able to do what I wanted with a do/while loop (starting on line 41), but I really would love to see how a for loop would work. Anybody? Thanks.

1
2
3
4
5
6
7
8
9
10
if(choice == 5){
	cout << "Enter users:" << endl;
		
	do{
		cin >> name;
		cout << "Contacting " << name << endl;
		amount = (amount - 1);
	}
	while(amount != 0);
}
Last edited on
} while ((choice <= 0) && (choice > 5));
When can choice ever be less than or equal to zero and be greater than 5 at the same time?

@jlb, I realized this when I tested out this condition, thanks for that. It runs the way it should now, I just with I could get the for loop to work.
for (int i = 0;(i <= amount);i++);{

you have used a semicolon; so an empty for loop
Last edited on
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
#include <iostream>

using namespace std;

int main(){
    int choice, amount;
    string name;
	
    do {
        cout << "Make a choice from the following menu:" << endl;
        cout << "   1. Add a user" << endl;
        cout << "   2. Delete a user" << endl;
        cout << "   3. Modify a user" << endl;
        cout << "   4. View a user" << endl;
        cout << "   5. Contact a user" << endl;
        cin >> choice;
		
    } while ((choice <= 0) && (choice > 5));

    switch (choice) {
        case 1: {
            cout << "You chose 'add a user'" << endl;
        } break;
        case 2: {
            cout << "You chose 'delete a user'" << endl;
        } break;
        case 3: {
            cout << "You chose 'modify a user'" << endl;
        } break;
        case 4: {
            cout << "You chose 'view a user'" << endl;
        } break;
        case 5: {
            cout << "How many users? ";
            cin >> amount;
        } break;
        default: {
            cout << "Unknown choice: " << choice << endl;
        } break;
    }
	if(choice == 5){
	    cout << "Enter users:" << endl;

		for (int i = 1; i <= amount; i++) {
			cin >> name;
			cout << "Contacting " << name << endl;
		}
	}
	else{
    return 0;}
}


there is the correct code. for the for loop i did i=1 because it will run 1 time when it equals 0, which then 1, then 2, that's 3 times. so i did i=1

so if you do 2 users, you don't have 3 times

then everything else was good.
Last edited on
Topic archived. No new replies allowed.