The objective for this activity was for me to create a menu-driven program using a switch statement. For some reason, though, when I prompt the user to input a value, even if it isn't 4, the program outputs "Invalid Input." Is there something wrong in the way I've written my code.
Please, try and water-down your C++ vocabulary so that I may understand. I'm a new computer science major, and my biggest trouble this semester has been understanding the lingo. Thanks for all of your help in advance.
#include <iostream>
#include <cmath>
#include <string>
usingnamespace std;
int main()
{
int menuChoice;
cout << "1. Calculate the Area of a Circle.\n""2. Calculate the Area of a Rectangle.\n""3. Calculate the Area of a Triangle.\n""4. Exit Program.\n\n";
cin >> menuChoice;
double radius;
double area;
double width, height, base;
switch (menuChoice)
{
case'1': cout << "\n\nWhat's the radius? ";
cin >> radius;
radius = (radius * radius);
area = radius * 3.14;
cout << "Your area is: " << area << "!\n";
break;
case'2': cout << "\n\nWhat's the width and height? ";
cin >> width >> height;
area = width * height;
cout << area << " is your Area.";
break;
case'3': cout << "\n\nWhat's the base and height of the triangle? ";
cin >> base >> height;
area = ((1/2)*base*height);
cout << area << "is your Area";
break;
case'4': cout << "Good Bye! :)\n";
break;
default: cout << "Invalid Input.";
}
return 0;
}
Don't worry too much about it. Pretty soon, things in your mind, start to click, and before you know it, you'll be writing very complex programs, and you'll wonder why the beginning seemed so difficult. Well, anyway, that's how it was for me.