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
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace 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;
}
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.
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.