Help with code.

Jun 12, 2019 at 11:07pm
I assume if they input a negative number or a decimal you want them to re-enter right?

You can use a loop like this:

1
2
3
4
5
6
7
8
int radius = 0;
do
{
	cout << "Enter the radius of a circle: ";
	cin >> radius;
	if (radius <= 0)
		std::cout << "Try Again!\n";
} while (radius <= 0); //Also Repeats If Input Is 0 


To make sure decimal numbers aren't inputted, use the same concept:

1
2
3
4
5
6
7
8
9
double area = 0; //Notice It's A Double

do
{
	cout << "Enter your choice (1-4): ";
	cin >> area;
	if (int(area) == area)
		break;
} while (true);


You need area to be a double to even check for a decimal, because the variable type "int" can't hold an integer.

Simply, you don't need to adapt the code to handle decimals because "area" (Which isn't a good variable name since it doesn't actually hold the area) will never hold a decimal point. If the user inputs a decimal, the decimal part is sliced off and the whole number is kept.
Last edited on Jun 12, 2019 at 11:08pm
Jun 13, 2019 at 12:03am
Hello instigator,

DO NOT DELETE your original post just because it is solved. This is confusing people who may have benefited from your question/problem. Aslo people reading this thread will have no idea what is going on since the original post and code are missing.

I was about to enlighten youof some of the problems I saw, but I guess you are not interested.

Andy
Topic archived. No new replies allowed.