#include <iostream>
usingnamespace std;
int main(){
char s;
int b, n, i, a;
do
{
cout<< "Menu" <<endl;
cout<< "1- Ciclo For" <<endl;
cout<< "2- Ciclo While" <<endl;
cout<< "3- Ciclo Do While" <<endl;
cout<< "4- Salir del programa" <<endl;
cin>>b;
if(b!=4)
{
cout<<"Introduzca hasta que numero desea la suma de los numeros pares"<<endl;
cin>>n;
switch(b){
case 1:
a=0;
for(i=2 ; i<=n ; i=i+2)
{
cout<<i+" ";
a= a+i;
}
break;
case 2:
i = 2;
a = 0;
while(i<=n)
{
cout<<i+" ";
a=a+i;
i=i+2;
}
break;
case 3:
i=2;
a=0;
do
{
cout<<i+" ";
a=a+i;
i=i+2;
}
while(i<=n);
break;
case'4':s='s';
break;
}//switch
cout<<"\n\nla suma de los pares hasta "<<n<<" es igual a "<<a<<endl;
}//if
cout<<"\n¿Desea volver ejecutar el programa s=si\n n=no?"<<endl;
cin>>s;
}//do
while(s=='s'|| s=='S');
return 0;
}
Consider checking for bad input, especially at line 19. If the input fails, let's say someone enters a letter instead of a number, all further input will fail and you may find yourself with the problem you described.
Try:
if(b!=4)
{
cout<<"Introduzca hasta que numero desea la suma de los numeros pares"<<endl;
cin>>n;
if (!cin.good())
{
n = 0;
cin.clear();
}
else
{
switch(b){
case 1:
...
...
}
}
...
}