The main problem I see currently is that when asking the user for the shape, if the user enters more than one character the program will end. If the user enters a single character the program will only run if they had entered a 1,2, or 3. Any help with this issue would be greatly appreciated.
Also, any tips on style or even the program itself are greatly appreciated, negative comments or positive.
//This program asks the user for input and gives them the area of a shape.
#include <iostream>
using namespace std;
int main()
{
char finished;
do
{
if (!cin.good())
{
cin.clear();
cin.sync();
finished = 'y', 'Y';
continue;
}
char choice;
const double PI = 3.141592653589;
double radius, side, height;
cout << "This program will calculate the area of a circle, square, or triangle.\n";
cout << "Input 1, 2, or 3 where '1' is a circle, '2' is a square, and '3' is a triangle. \n";
cin >> choice;
switch(choice)
{
case '1' :cout << "What is the radius?\n";
cin >> radius;
cout << "\n";
if(radius >= 0)
{
cout << "The area of the circle is " << PI*radius*radius << " units squared.\n";
cout << "Would you like to continue? Y/N \n";
}
else
{
cout << "\n";
}
break;
case '2' :cout << "What is the length of a side?\n";
cin >> side;
if(side >= 0)
{
cout << "The area of the square is " << side*side << " units squared.\n";
cout << "Would you like to continue? Y/N \n";
}
else
{
cout << " \n";
}
break;
case '3' :cout << "What is the height?\n";
cin >> height;
if(height >= 0)
{
cout << "The area of the triangle is " << (height*height)/3 << " units squared.\n";
cout << "Would you like to continue? Y/N \n";
}
else
{
cout << "Your input is invalid. Would you like to continue? Y/N \n";
}
break;
default : cout << "\n";
break;
}
cout << "Your input is invalid. Would you like to continue? Y/N \n";
cin >> finished;
}while (finished == 'y' || finished == 'Y');
return 0;
}

New Topic/Question
Reply



MultiQuote







|