Please Help

Hi, Can i get some help on the following code. I wrote it but it keeps giving me errors saying cin is ambiguous, and the else error no matching if.

using visual studio 32winproject here is the code

*Description: Create a program which prompts the user for an integer number representing the user's choice of beam type, 1 for an I-beam,
*2 for a rectangular beam, 3 for a cylindrical beam. After the user has chosen the type of beam, the program should prompt the user for
*for the necessacry measurements data (based on the type of beam), and then compute and display the beam's moment of inertia.*/

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
double I;
double B, H, b, h, r;
const double PI = 3.1416;
char choice;

cout << "1 I Beam ";
cout << "2 Rectangular Beam ";
cout << "3 Cylindrical Beam ";

cout << "Enter the type of beam and make a choice from the beams: ";
cin >> choice;

if (choice == '1');
{
cout << "Enter the width of the part of the I beam in inches: ";
cin >> b;
cout << "Enter the height of the part of the I beam in inches: ";
cin >> h;
cout << "Enter the Breath of the part of the I beam in inches: ";
cin >> B;
cout << "Enter the height of the part of the I beam in inches: ";
cin >> H;

I = ((B*H*H*H) - (b*h*h*h)) / 12;

cout << "The moment of Inertia I in inches is: " << I << endl;

}
else if (choice == '2');
{
cout << "Enter the breath of the Rectangular Beam in inches: ";
cin >> b;
cout << "Enter the height of the Rectangular Beam in inches: ";
cin >> h;

I = (b *(h*h*h)) / 12;
cout << "The moment of Inertia I in inches is: " << I << endl;
}
else if (choice == '3');
{
cout << "Enter the Radius of the Cylindrical beam: ";
cin >> r;

I = (PI * (r*r*r*r)) / 4;
cout << "The moment of Inertia I is: " << I << endl;

}

cin.ignore();
return 0;
}
Thanks
The problem is the ; at the end of the if, else and else-if statement.
If you remove them it should compile.
Thank you i did was you suggested and got the following errors:

Error 6 error C2059: syntax error : '}' f:\cmpsci235\programming exercise 1 chapter 4\programming exercise 1 chapter 4\programming exercise 1 chapter 4.cpp 68 1 Programming Exercise 1 Chapter 4
Error 5 error C2059: syntax error : 'return' f:\cmpsci235\programming exercise 1 chapter 4\programming exercise 1 chapter 4\programming exercise 1 chapter 4.cpp 67 1 Programming Exercise 1 Chapter 4
Error 2 error C2064: term does not evaluate to a function taking 1 arguments f:\cmpsci235\programming exercise 1 chapter 4\programming exercise 1 chapter 4\programming exercise 1 chapter 4.cpp 58 1 Programming Exercise 1 Chapter 4
Error 3 error C2143: syntax error : missing ';' before '.' f:\cmpsci235\programming exercise 1 chapter 4\programming exercise 1 chapter 4\programming exercise 1 chapter 4.cpp 66 1 Programming Exercise 1 Chapter 4
Error 7 error C2143: syntax error : missing ';' before '}' f:\cmpsci235\programming exercise 1 chapter 4\programming exercise 1 chapter 4\programming exercise 1 chapter 4.cpp 68 1 Programming Exercise 1 Chapter 4
Error 1 error C2143: syntax error : missing '}' before ';' f:\cmpsci235\programming exercise 1 chapter 4\programming exercise 1 chapter 4\programming exercise 1 chapter 4.cpp 58 1 Programming Exercise 1 Chapter 4
Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int f:\cmpsci235\programming exercise 1 chapter 4\programming exercise 1 chapter 4\programming exercise 1 chapter 4.cpp 66 1 Programming Exercise 1 Chapter 4
8 IntelliSense: expected a ';' f:\CMPSCI235\Programming Exercise 1 Chapter 4\Programming Exercise 1 Chapter 4\Programming Exercise 1 Chapter 4.cpp 57 2 Programming Exercise 1 Chapter 4
closed account (E0p9LyTq)
PLEASE, learn to use code tags, it makes reading your source MUCH easier
http://www.cplusplus.com/articles/jEywvCM9/

(Hint, you can edit your post and add code tags)

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
#include <iostream>

int main()
{
   double I;
   double B, H, b, h, r;
   const double PI = 3.1416;
   char choice;

   std::cout << "1 I Beam ";
   std::cout << "2 Rectangular Beam ";
   std::cout << "3 Cylindrical Beam ";

   std::cout << "Enter the type of beam and make a choice from the beams: ";
   std::cin >> choice;

   if (choice == '1')
   {
      std::cout << "Enter the width of the part of the I beam in inches: ";
      std::cin >> b;
      std::cout << "Enter the height of the part of the I beam in inches: ";
      std::cin >> h;
      std::cout << "Enter the Breath of the part of the I beam in inches: ";
      std::cin >> B;
      std::cout << "Enter the height of the part of the I beam in inches: ";
      std::cin >> H;

      I = ((B * H * H * H) - (b * h * h * h)) / 12.0;

      std::cout << "The moment of Inertia I in inches is: " << I << '\n';
   }
   else if (choice == '2')
   {
      std::cout << "Enter the breath of the Rectangular Beam in inches: ";
      std::cin >> b;
      std::cout << "Enter the height of the Rectangular Beam in inches: ";
      std::cin >> h;

      I = (b *(h * h * h)) / 12.0;
      std::cout << "The moment of Inertia I in inches is: " << I << '\n';
   }
   else if (choice == '3')
   {
      std::cout << "Enter the Radius of the Cylindrical beam: ";
      std::cin >> r;

      I = (PI * (r * r * r * r)) / 4.0;
      std::cout << "The moment of Inertia I is: " << I << '\n';

   }
}

This compiles successfully with MS Visual C++ 2017, I can't say if the equations are calculating the correct results. Not my area of expertise. :)

When doing math operations on floating point variables I recommend not using integer constants, it could skew the results with an integer math operation. For example, use 4.0 instead of 4.
Once I removed the precompiled header, stdafx.h, and the semi-colons after the if statements, I got it to compile as well.
Last edited on
Topic archived. No new replies allowed.