Originally Posted by
Horse22
relatively new to c++ and to the forum. wrote a program that divides two integers:
i understand that a digit that is more than 10 digits long cannot be fed into a or b. But what I don't understand is why would the program loop infinitely without pausing for an input in c when a huge number is entered (besides c doesn't have a value for while to respond). have search the faqs and threads but couldn't find what i wanted.
When using cin to accept numerical data, it is wise to test to see if everything is OK (what if the user inters a non-numeric character or does someting else to cause cin to enter a "fail" state).
Try this:
Code:
#include <iostream>
using namespace std;
int main()
{
unsigned long a, b, c;
do
{
cout << "\n\nEnter a and b respectively." << endl;
cin >> a;
cout << "You entered a = " << a << endl;
if (!cin) {
cout << "cin is NULL after reading a" << endl;
break;
}
cin >> b;
cout << "You entered b = " << b << endl;
if (!cin) {
cout << "cin is NULL after reading b" << endl;
break;
}
cout << "a/b= " << (double)a / b << endl;
cout << "Continue? 1=yes, 0=no"<< endl;
cin >> c;
cout << "You entered " << c << endl;
if (!cin) {
cout << "cin is NULL after reading c" << endl;
break;
}
}while(c);
return 0;
}
Then enter 11111111111 and 22222222222, or whatever large numbers (or non-numbers) that you feel like
Regards,
Dave