# Help with code needed

• 09-27-2011
ToweyLeake
Help with code needed
I am in need of some help. I am a beginner in C++ and this is for an assignment I have due; however, I have written the code. I need help understanding how to fix my code so it may run correctly. Any help will be greatly appreciated.
Thanks-Towey

This is my assignment. The purpose of the assignment: To practice logic operations in C++.

Requirements:
1. The program will first display a menu as shown below.
2. The program will wait for the user to enter a letter of choice. The valid inputs are A, B and C.
3. After receiving the input from the user, your program will perform the computation chosen by the user and display the result back to the screen, as shown below.
4. if, or if/else statements are needed.
Steps to program:
A. Ask the user to enter the radius of the sphere. And then calculate the volume by the following formula: Volume = (4.0/3) * 3.14159 *radius * radius * radius
B. Ask the user to enter the measures of the three sides of the triangle, a, b, and c and then calculate the area by the following formula: Area = sqrt(s*(s-a)*(s-b)*(s-c)), where s = (a+b+c) /2.0
C. For this question, follow the steps below:
a. Ask the user to enter the three coefficients of the equation, a, b and c.
b. Calculate b*b – 4 * a* c
c. If the above result is negative, print “No real number solution”; otherwise calculate the solution by the following formula: x1 = (-b + sqrt(b*b-4*a*c))/(2.0*a), x2 = (-b - sqrt(b*b-4*a*c))/(2.0*a)
D. In order to use function sqrt, #include<cmath> is needed.

This is my code for said assignment:

Code:

```#include <iostream> #include <cmath> using namespace std; int main() {     char choice;    // Menu Choice     // Display the menu and get a choice.     cout << "\t\t User Menu\n\n";     cout << "A. Calculate the volume of a sphere.\n";     cout << "B. Calculate the area of a triangle with the measures of three sides.\n";     cout << "C. Calculate the solution(s) of a quadratic equation.\n";     cout << "Please enter a letter (upper case A, B, or C) of your choice: ";     cin >> choice;     // Do I need a validation input and if so will it look like this if (choice < C) cout << choice << "is an invalid choice.\n"; or switch         if(choice == A)         {             // Volume of sphere             double radius, volume;             cout << "Please enter the radius of circle: /n";             cin >> radius;             // Formula used to find volume             volume = (4.0/3) * 3.14159 * radius * radius * radius             cout << " The volume of the circle is: " << volume << endl;         }         else if(choice == B)         {             // Three sides of triangle             double a, b, c;             cout << "Please enter the measure of side A of triangle: ";             cin >> a;             cout << "Please enter the measure of side B of triangle: ";             cin >> b;             cout << " Please enter the measure of side C of triangle: ";             cin >> c;             // Input Validation             if(a + b > c && a + c > b && b + c > a)             {                 // An invalid set of sides was entered                 cout << "You have entered an invalid set of sides.\n";                 cout << "Please run the program again and enter correct side measurements.\n";             }             else             {                 double area, s;                 s = (a + b + c) / 2.0                 area = sqrt(s*(s-a)*(s-b)*(s-c))                 cout << " The area of the triangle is: " << area << endl;             }         }         else if(choice == C)         {             double a, b, c, solution, solution1, solution2;             cout << "Please enter the coefficient A from the equation: ";             cin >> a;             cout << "Please enter the coefficient B from the equation: ";             cin >> b;             cout << "Please enter the coefficient C from the equation: ";             cin >> c;             solution = b*b-4*a*c             if(solution < 0)             {                 cout << "No real number solution.";             }             else             {                 solution1 = (-b + sqrt(b*b-4*a*c))/(2.0*a)                 solution2 = (-b-sqrt(b*b-4*a*c))/(2.0*a)                 cout << "The first solution to the formula is: " << solution1 << endl;                 cout << "The second solution to the formula is: " << solution2 << endl;             }         }         return 0;     }```
These are my error messages:
Code:

```Towey\Documents\assignment2.cpp\assignment2.cpp||In function 'int main()':| Towey\Documents\assignment2.cpp\assignment2.cpp|18|error: 'A' was not declared in this scope| Towey\Documents\assignment2.cpp\assignment2.cpp|28|error: expected ';' before 'cout'| Towey\Documents\assignment2.cpp\assignment2.cpp|31|error: 'B' was not declared in this scope| Towey\Documents\assignment2.cpp\assignment2.cpp|56|error: expected ';' before 'area'| Towey\Documents\assignment2.cpp\assignment2.cpp|53|warning: unused variable 'area'| Towey\Documents\assignment2.cpp\assignment2.cpp|62|error: 'C' was not declared in this scope| Towey\Documents\assignment2.cpp\assignment2.cpp|75|error: expected ';' before 'if'| Towey\Documents\assignment2.cpp\assignment2.cpp|64|warning: unused variable 'solution1'| Towey\Documents\assignment2.cpp\assignment2.cpp|64|warning: unused variable 'solution2'| Towey\Documents\assignment2.cpp\assignment2.cpp|80|error: expected '}' before 'else'| Towey\Documents\assignment2.cpp\assignment2.cpp|83|error: 'solution1' was not declared in this scope| Towey\Documents\assignment2.cpp\assignment2.cpp|83|error: 'b' was not declared in this scope| Towey\Documents\assignment2.cpp\assignment2.cpp|83|error: 'a' was not declared in this scope| Towey\Documents\assignment2.cpp\assignment2.cpp|83|error: 'c' was not declared in this scope| Towey\Documents\assignment2.cpp\assignment2.cpp|84|error: expected ';' before 'solution2'| Towey\Documents\assignment2.cpp\assignment2.cpp|87|error: 'solution2' was not declared in this scope| Towey\Documents\assignment2.cpp\assignment2.cpp|91|error: expected unqualified-id before 'return'| Towey\Documents\assignment2.cpp\assignment2.cpp|92|error: expected declaration before '}' token| ||=== Build finished: 15 errors, 3 warnings ===|```
• 09-27-2011
hk_mp5kpdw
Code:

```if(choice == A) ... else if(choice == B) ... else if(choice == C)```
By themselves, the compiler is looking at those as if they were identifiers/names for variables which you haven't declared which is the reason for some of those errors. What you want to do is tell the compiler that they are characters and not identifiers which you can accomplish by the use of single quotes.
Code:

```if(choice == 'A') ... else if(choice == 'B') ... else if(choice == 'C')```
You also are missing some semicolons in several places in your code:
Code:

```// Formula used to find volume volume = (4.0/3) * 3.14159 * radius * radius * radius ... s = (a + b + c) / 2.0 area = sqrt(s*(s-a)*(s-b)*(s-c)) ... solution = b*b-4*a*c ... solution1 = (-b + sqrt(b*b-4*a*c))/(2.0*a) solution2 = (-b-sqrt(b*b-4*a*c))/(2.0*a)```
...which is the reason for those "expected ';'" errors. That one is really quite self-explanatory as far as error messages go.

You also have something of a logic error here:
Code:

`if(a + b > c && a + c > b && b + c > a)`
I'll let you figure out what it is for yourself.
• 09-27-2011
AndrewHunter
Did you bother to look at the line numbers the errors gave you?
Code:

```if(choice == A) //should be: if(choice == ' A ') same for the rest```
Code:

`volume = (4.0/3) * 3.14159 * radius * radius * radius //you are missing a ;`
You have similar problems throughout your code with missing ; and incorrect char comparisons. Fix all of those first. Additionally, actually look at the line numbers given with the error messages, these are all pretty straightforward.
• 09-27-2011
ToweyLeake
Thank you
Thank you both for the help. Also, I did look and fix some earlier problems; however, as I am incredibly new to C ++, I had never come across these error messages and wasn't sure how to correct them. Now that I know what they mean I can correct them myself on future programs. Regardless, thank you both for your help. I appreciate it greatly.