0

When I enter two numbers the sum comes.. but when I input character as yes not y... Then it goes into an infinite loop... can anyone explain why? int a,b; char ch; do { cout<<"Enter two numbers: "; cin>>a>>b; cout<<"The sum is "<<a+b <<"\nDo you want to try again (y/n): "; cin>>ch; }while(ch=='Y'||ch=='y');

10th Oct 2016, 1:14 PM
Soutik
Soutik - avatar
2 Answers
0
#include <iostream> #include <stdio.h>//biblioteca para usar la funcion para limpiar el buffer using namespace std; int main() { int a,b; char ch; do { fflush (stdin); cout<<"Enter number 1: "; cin>>a; cout<<"enter number 2: "; cin>>b; fflush(stdin);//limpiando el buffer cout<<"The sum is "<<a+b <<"\nDo you want to try again (y/n): "; cin>>ch; }while(ch=='Y'||ch=='y'); return 0; }
10th Oct 2016, 3:34 PM
Edwin Alberto Guerrero Acosta
Edwin Alberto Guerrero Acosta - avatar
0
necesitas limpiar el buffer de entrada.... le hice unas cuantas modificaciones a tu codigo por comodidad.....saludos desde colombia
10th Oct 2016, 3:36 PM
Edwin Alberto Guerrero Acosta
Edwin Alberto Guerrero Acosta - avatar