if statement help

I am having trouble with this part
1
2
3
4
5
6
7
8
9
10
11
if (choice == 1){
		cout << "Please enter the radius of the circle: ";
		cin >> radiusofcircle;
		areac = 3.14159 * radiusofcircle * radiusofcircle;
		if (areac <= 0){
			cout << "Invalid input! The radius must be a real number and greater than zero!" << endl;
			return 0;
		}
		cout << "The area of the circle is:  " << areac << endl;
		return 0;
	}


The specific part is (areac <=0) I want it so that when someone enters a really number the program will work and give them the area of the circle but once they enter in a number that is not real or isn't equal to ore greater than zero it should tell them "Invalid input! The radius must be a real number and greater than zero!" but it doesn't work on Microsoft visual studios when I run it, but if i were to run it on http://cpp.sh/ it works just fine i'm not sure what is wrong and I have tried many ways to fix it can anyone help me? So basically if anyone enters a negative or a not real numbers or letters or words such as yay it should work but if a positive number is entered it will work. Thanks for the help




Here is my whole code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  #include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main() {

	double choice;
	double areac;
	double radiusofcircle;
	double lengthofr;
	double widthofr;
	double arear;
	double heightoft;
	double baseoft;
	double areat;
	double areacc;

	cout << "\nWelcome to the Geometry Calculator!" << endl;
	cout << "\nSelect the program you would like to use." << endl;
	cout << "\n1. Calculator for the area of a circle" << endl;
	cout << "\n2. Calculator for the area of a rectangle" << endl;
	cout << "\n3. Calculator for the area of a triangle" << endl;
	cout << "\n4. Quit" << endl;
	cout << "\nEnter in your choice (1-4): ";
	cin >> choice;

	if (choice == 1){
		cout << "Please enter the radius of the circle: ";
		cin >> radiusofcircle;
		areac = 3.14159 * radiusofcircle * radiusofcircle;
		if (areac <= 0){
			cout << "Invalid input! The radius must be a real number and greater than zero!" << endl;
			return 0;
		}
		cout << "The area of the circle is:  " << areac << endl;
		return 0;
	}

	if (choice == 2){
		cout << "Please enter the length of the rectangle: ";
		cin >> lengthofr;
		cout << "Please enter the width of the rectangle: ";
		cin >> widthofr;
		arear = lengthofr * widthofr;
		if (widthofr >= lengthofr){
			cout << "Width cannot be greater than length!!" << endl;
			return 0;
		}
		arear = lengthofr * widthofr;
		if (arear <= 0){
			cout << "Invalid input! The Length and width must be real numbers and greater than zero!" << endl;
			return 0;
		}
		cout << "The area of the rectangle is: " << arear << endl;
		return 0;
	}

	if (choice == 3){
		cout << "Please enter the height of the triangle: ";
		cin >> heightoft;
		cout << "Please enter the base of the triangle: ";
		cin >> baseoft;
		areat = 0.5 * baseoft * heightoft;
		if (areat <= 0){
			cout << "Invalid input! The Length and height must be real numbers and greater than zero!" << endl;
			return 0;
		}
		cout << "The area of the triangle is: " << areat << endl;
		return 0;
	}

	if (choice == 4){
		cout << "Thank you for using the Geometry Calculator! The program is being terminated." << endl;
		return 0;
	}

	if (choice != 1 && choice != 2 && choice != 3 && choice != 4){
		cout << "The valid choices are 1 through 4. Run the Program agian and select one of those." << endl;
	}

	return 0;

}
Last edited on
You are concerned about whether or not the radius is valid, but your if statement condition is testing the areac variable.
i know the radius will be valid when a real number is entered. What i am concerned about is when someone enters a non real number or a negative or a letter such a Q it will give some weird output what i am trying to do is basically have the program ignore the bad inputs and tell the user whatever the inputted is invalid.
You are concerned about whether or not the radius is valid, but your if statement condition is testing the areac variable.
why do you keep repeating the same thing....?
Because that's where I think the error is.

If you are concerned about whether or not the radius is positive or not, your if statement condition should probably test whether the radius is positive or not, not the area.
Testing for garbage input:
1
2
3
4
5
6
7
8
9
#include <limits>
//...
double d;
std::cout << "enter real number: ";
while(!(std::cin >> d) { //if stream is in failed state after input, then something wrong
    std:: cout << "bad input\nenter real number: "
    std::cin.clear(); //Clear state flags
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //ignore wrong input
}


Testing for negatives:
1
2
if(d < 0) {
//... 
you can use

1
2
3
4
5
6

if(isdigit(radiusofcircle)>0) {

//compute area of circle
} 


to check whether the value entered is other than digits
Last edited on
Thanks everyone for the help i just changed (areac <=0) to (radiusofcircle <=0) and it fixed everything. :)
Topic archived. No new replies allowed.