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
|
#include <iostream>
using namespace std;
int main()
{
char letter = ' ';
cout << "Welcome to a collection of four programs." << endl;
cout << "To choose one of the programs, enter one of the letters: A, B, C or D." << endl;
cout << "The letter A will start program 1." << endl;
cout << "The letter B will start program 2." << endl;
cout << "The letter C will start program 3." << endl;
cout << "The letter D will start program 4." << endl;
cout << "" << endl;
cout << "1.Program for calculating the area of a square." << endl;
cout << "2.Program for calculating the perimeter of a circle ." << endl;
cout << "3.Program for calculating the ratio of two numbers." << endl;
cout << "4.Program for calculating the product of two numbers ." << endl;
cin >> letter;
letter = toupper(letter);
if (letter != 'A' && letter != 'B' && letter != 'C' && letter != 'D')
{
cout << "Error: You didn't enter any of the letters: A, B, C or D." << endl;
}
if (letter == 'A')
{
int side;
cout << "You chose the program for calculating an area of a square." << endl;
cout << "Enter a value for the side of the square:" << endl;
cin >> side;
cout << "The area of a square with a=" << side << " is " << side*side << "." << endl;
}
if (letter == 'B')
{
float radius;
float L = 2*radius*3.14; //weird error again
cout << "You chose the program for calculating the perimeter of a circle." << endl;
cout << "Enter a value for the radius of the circle:" << endl;
cin >> radius;
cout << "The perimeter of a circle with a radius of " << radius << " is " << 2*radius*3.14 << "." << endl;
}
if (letter == 'C')
{
float first;
float second;
cout << "You chose the program for calculating the ratio of two numbers." << endl;
cout << "Enter a value for the first number:" << endl;
cin >> first;
cout << "The value of the first number is " << first << ".Enter a value for the second number:" << endl;
cin >> second;
cout << "The ratio between the numbers " << first << " and " << second << " is " << first/second << "." << endl;
}
if (letter == 'D')
{
float first;
float second;
float third = first*second; //weird error
cout << "You chose the program for calculating the product of two numbers." << endl;
cout << "Enter the value of the first number:" << endl;
cin >> first;
cout << "The value of the first number is " << first << ".Enter a value for the second number:" << endl;
cin >> second;
cout << "The product of the numbers " << first << " and " << second << " is " << first*second << "." << endl;
}
return 0;
}
|
So far, one problem. I will enter more ifs between the ifs to display errors if needed, but for example
{
float first;
float second;
float third = first*second; //weird error
cout << "You chose the program for calculating the product of two numbers." << endl;
cout << "Enter the value of the first number:" << endl;
cin >> first;
cout << "The value of the first number is " << first << ".Enter a value for the second number:" << endl;
cin >> second;
cout << "The product of the numbers " << first << " and " << second << " is " << third << "." << endl;
}
This prints out incorrect information. Instead i have to use << first*second << Any other ways to avoid this and define it so that it can be faster?
Edit:Heres another interesting thing
If i enter a lowercase letter it wont print an error or anything, thats why i made a = A, or tried to at least, i wanted to make any lowercase letters which are entered to take the value of the same letter, except uppercase.
Edit: if (letter == 'A' || letter == 'a')
Fixes the problem, ill just edit the rest of the code