onimkron
OSNN Senior Addict
- Joined
- 17 Jan 2004
- Messages
- 414
I'm pretty much new to C++, having just started about 5 weeks back. I've gone through if, else, what, do statements, but one thing i'm currently fumbling on is probably so easily to implement, but I'm having difficulty with
Basically, the user inputs a number, and with it uses it as a radius and finds the area of a circle...works perfectly fine there. I've added an error checking bit, so it loops back to asking for a number if a character is entered (say, the letter 'a' instead of 1).
I want to stop asking for a correct, numerical answer after 10 wrong attempts, hence the count interger statement near the top. However, although I can make it understand that there have been ten error attempts (the 'test' statement proves this), how do I make it so it just outpusts a cout statement along the lines of '10 attempts exceeded, closing program' etc?
I apologise if this sounds long-winded. It just sounds like there's an actual simple solution for this, yet I can't just figure it out
Code:
// A program to calculate the area of a circle
#include <iostream>
using namespace std;
int main ()
{
float radius;
const double PI = 3.14159265359;
unsigned count;
count = 0;
do
{
if ( !cin.good() )
{
cerr << "Error" << endl;
cin.clear (0);
cin.ignore( 128, '\n' );
count = count + 1;
cout << "Test:" << count << endl;
}
cout << "Enter radius:";
cin >> radius;
}
while ( !cin.good() );
radius = radius * radius; // Get radius and multiply by itself (squared)
cout << "Area of circle: " << PI * radius << endl;
return 0;
}
Basically, the user inputs a number, and with it uses it as a radius and finds the area of a circle...works perfectly fine there. I've added an error checking bit, so it loops back to asking for a number if a character is entered (say, the letter 'a' instead of 1).
I want to stop asking for a correct, numerical answer after 10 wrong attempts, hence the count interger statement near the top. However, although I can make it understand that there have been ten error attempts (the 'test' statement proves this), how do I make it so it just outpusts a cout statement along the lines of '10 attempts exceeded, closing program' etc?
I apologise if this sounds long-winded. It just sounds like there's an actual simple solution for this, yet I can't just figure it out