My code isn't looping. Trying to count to 1000.

I want my code to count from '5 - 1000' (count by 5) when the user inputs a '1'.

The problem is, my for-loop isn't looping and only counts once (outputs 5) instead of to 1000, making me insert another input.

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

using namespace std;

int First = 5, Last = 1000;

int main()
{
    int num;
    
    cin >>num; //pick option 1 or 2
    
    
    for(First <= Last; First <= Last; First+5){ //declared loop with condition
        if (num == 1) { //if user picks 1.
        
                cout<<First<<" "; //output for option 1
                }
                
                
                else if (num == 2){ //if user picks 2.
                     cout<<"2 works. "<<endl; //output for option 2
                     }
                     
                     
            
            return main();
}
}
Last edited on
Because your for loop bracket encapsulates the "return main" on line 28, it keeps going to the top of your main function and starting over with
"cin>>num"
. And your third condition in your for loop doesn't increment. Use FIRST+=5 or..... FIRST = FIRST + 5.

Hope that helps
Last edited on
Worked perfectly! Thank you.
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
#include <cstdlib>
#include <iostream>

using namespace std;

int First = 5, Last = 1000;

int main()
{
    int num;
    
    cin >>num; //pick option 1 or 2
    
    
    for(First <= Last; First <= Last; First+=5){ //declared loop with condition
        if (num == 1) { //if user picks 1.
        
                cout<<First<<" "; //output for option 1
                }
                
                
                else if (num == 2){ //if user picks 2.
                     cout<<"2 works. "<<endl; //output for option 2
                     }
                     
                     
            

}
            return main();
}
I ran into 2 more bug with my code if you can help.

1. It doesn't let me input a number '1' or '2' after the code is finished.

2. When I enter '2' for input, it says "2 works" a bunch of times. It should only say the message once for everytime I input a '2'.
I believe you can't enter anything else because of your first condition in your for loop. Try changing it to FIRST = 5 and see if that helps. As for your second issue, it's going to output that message however many times your argument in your for loops says it will. Include "break;" statement inside your else if and that should solve it.
Last edited on
It worked thanks

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

using namespace std;

int First = 5, Last = 1000;

int main()
{
    int num;
    
    cin >>num; //pick option 1 or 2
    
    
    for(First = 5; First <= Last; First+=5){ //declared loop with condition
        if (num == 1) { //if user picks 1.
        
                cout<<First<<" "; //output for option 1
                }
                
                
                else if (num == 2){ //if user picks 2.
                     cout<<"2 works. "<<endl; //output for option 2
                     break; //keeps cout from outputting multiple times
                     }
                     

                     
                     
            

}
return main();
}
Last edited on
Topic archived. No new replies allowed.