I must make a program that prints the numbers between 1 and 200, show the first numbers in groups of 20 (1 to 20) and when I have to show the new ones again the group of 20 (which in this case would be from 20 to 40) ask the user if he wants to continue to continue showing the ones that follow, until he reaches 200.
#include <iostream>
using namespace std;
int main (){
int op;
for(int i=1; i<201; i++){
cout<<i<<endl;
if(i>19){
cout<<"Desea continuar? "<<endl;
cout<<" 1 = Si "<<endl;
cout<<" 2 = No "<<endl;
cin>>op;
switch(op){
case 1: cout<<i<<endl;
if(i==40){
cout<<"Desea continuar: "<<endl;
cout<<" 1 = Si "<<endl;
cout<<" 2 = No "<<endl;
cin>>op;
switch(op){
case 1: cout<<i<<endl;
if(i>59){
cout<<"Desea continuar: "<<endl;
cout<<" 1 = Si "<<endl;
cout<<" 2 = No "<<endl;
cin>>op;
switch(op){
case 1: cout<<i<<endl;
if(i>79){
cout<<"Desea continuar: "<<endl;
cout<<"1 = Si "<<endl;
cout<<"2 = No "<<endl;
cin>>op;
switch(op){
case 1: cout<<i<<endl;
if(i>99){
cout<<"Desea continuar: "<<endl;
cout<<"1 = Si "<<endl;
cout<<"2 = No "<<endl;
cin>>op;
switch(op){
case 1: cout<<i<<endl;
if(i>119){
cout<<"Desea continuar: "<<endl;
cout<<"1 = Si "<<endl;
cout<<"2 = No "<<endl;
cin>>op;
switch(op){
case 1: cout<<i<<endl;
if(i>139){
cout<<"Desea continuar: "<<endl;
cout<<"1 = Si "<<endl;
cout<<"2 = No "<<endl;
cin>>op;
switch(op){
case 1: cout<<i<<endl;
if(i>159){
cout<<"Desea continuar: "<<endl;
cout<<"1 = Si "<<endl;
cout<<"2 = No "<<endl;
cin>>op;
switch(op){
case 1: cout<<i<<endl;
if(i>179){
cout<<"Desea continuar: "<<endl;
cout<<"1 = Si "<<endl;
cout<<"2 = No "<<endl;
cin>>op;
switch(op){
case 1: cout<<i<<endl;
}
}
case 2: return 0;
}
}
case 2: return 0;
}
}
case 2: return 0;
}
}
case 2: return 0;
}
}
case 2: return 0;
}
}
case 2: return 0;}
}
case 2: return 0;break;}
}break;
case 2: return 0;break;
}
}
}
return 0;
}
The problem I have is that when executing the code, it prints the first 20 numbers but when the first question pops up (whether I want to continue or not) and I say yes, it only prints an additional number (21) in Instead of the other 20 numbers that follow (up to 40), I am aware that my code is unorthodox and quite long, but I would like to leave it as it is, but understanding how nested switches work.
I don't understand the complication of your code, which can be written like this:
Regarding your problem: Every time you repeat the loop, the first condition is met:
Since any number after 19 will be greater than 19; therefore to each number it asks you the same question.
Your code can be rewritten like this: