I must write a program that writes the numbers between 1 and 200. The program will write the numbers to the screen in groups of 20, asking the user whether or not he wants to continue displaying the next group of numbers.
I have two problems, the first is that when I run the program the counter (i) does not appear on the screen.
The second problem is that I don't know how to do it so that when the value of (i) is greater than 20, that is, 21 (when the display of the group of 20 numbers has already been completed), the user has to enter the number 1 again to show him the next 20 numbers (for example, in this case up to 40)
#include <iostream>
using namespace std;
int main (){
int op=0;
for (int i=1; i<201; i++){
cout<<"Desea ver el siguiente conjunto de 20 numeros? "<<endl;
cout<<"1 = Continuar "<<endl;
cout<<"2 = Declinar "<<endl;
cin>>op;
switch(op){
case 1: cout<<i<<endl;
if(i>20){
cin>>op; } break;
case 2: return 0;break;
default: cout<<"Opcion invalida "<<endl;
}
}
return 0;
}
This is what is happening
The program does what you told it to. That would print the message on each iteration of the dialog.
You could put it this way:
In this way, you print 20 elements with the help of a function and ask if you want to continue. Making the code easier to understand.
Another way is to print the message every 20 iterations. You could achieve this with the help of an additional variable or check if
i % 20 == 0 && i > 0
.