## Trending News

# C++ I don't understand how to fix my errors?

Hello

I'm trying making a simple calculator but I can't figure out by myself..

I'm using G++ under Ubuntu.

Can someone help me to understand my errors ?

My code:

1 #include <iostream>

2 #include <cmath>

3 #include <process>

4 #include <cstdlib>

5 #include <iomanip>

6 using namespace std;

7 using std::cout;

8 using std::cin;

9 using std::endl;

10

11 int main()

12 {

13 char ch[8];

14 int op;

15

16 while(1){

17

18 cout<<"\t1.addition"<<endl;

19

20 cout<<"\t2.subtraction"<<endl;

21

22 cout<<"\t3.division"<<endl;

23

24 cout<<"\t4.multiplication"<<endl;

25

26 cout<<"\t5.area of square"<<endl;

27

28 cout<<"\t6.square root"<<endl;

29

30 cout<<"\t7.pi"<<endl;

31

32 cout<<"\t8.percentage"<<endl;

33

34

35 cout<<"enter your option"<<endl;

36

37 cin>>op;

38

39

40 cout<<"you selected "<<op<<endl;

41

42 switch (int) (op)

43 {

44

45 case 1:

46

47 float a,c,sum;

48

49 cout<<"enter the first number"<<endl;

50

51 cin>>a;

52

53 cout<<"enter the 2nd number"<<endl;

54

55 cin>>c;

56

57 sum=a+c;

58

59 cout<<"sum =" <<setprecision(20)<<sum<<endl;

60

61 break;

62

63 case 2:

64

65 float a1,c1,sub;

66

67 cout<<"enter the first number"<<endl;

68

69 cin>>a1;

70

71 cout<<"enter the 2nd number"<<endl;

72

73 cin>>c1;

74

75 sub=a1+c1;

76

77 cout<<"subtraction =" <<setprecision(20)<<sub<<endl;

78

79 break;

80

81 case 3:

82

83 float a2,c2,div;

84

85 cout<<"enter the first number"<<endl;

86

87 cin>>a2;

88

89 cout<<"enter the 2nd number"<<endl;

90

91 cin>>c2;

92

93 div=a2/c2;

94

95 cout<<"division =" <<setprecision(20)<<div<<endl;

96

97 break;

98

99 case 4:

100

101 float a3,c3,mult;

102

103 cout<<"enter the first number"<<endl;

104

105 cin>>a3;

106

107 cout<<"enter the 2nd number"<<endl;

108

109 cin>>c3;

110

111 mult=a3*c3;

112

113 cout<<"multiply =" <<setprecision(20)<<mult<<endl;

114

115 break;

116

117 case 5:

118

119 float a4,area, c4=3.14;

121

122 cout<<"enter the radius"<<endl;

123

124 cin>>a4;

125

126 area=c4*(a4*a4)*(0.5);

127

128 cout<<"area of square =" <<setprecision(20)<<area<<endl;

129

130 break;

131

132 case 6:

133

134 double a6, square;

135

136 cout<<"enter the number"<<endl;

137

138 cin>>a6;

139

140 square = sqrt(a6);

141

142 cout<<"square root =" <<setprecision(20)<<square<<endl;

143

144 break;

145

146 case 7:

147

148 double a7=1,, aa, b=pow(2,-.5), t=.25, p=1, r;

149 int iterations, exit;

150

151 cin>>interations;

152 {

153 for(int i=0;i<iterations;i++)

154 {

155 aa=(a7+b)/2;

156 b=pow((a7*b),.5);

157 t=t-p*pow((a7-aa),2);

158 p=2*p;

159 r=(pow((a7+b),2))/(4*t);

160 a7=aa;

161 }

162 cout.precision(20);

163 cout << r;

164 cin >> a;

165 run=false;

166

167 break;

168

169 }

170

171 case 8:

172

173 float a8, c8, percentage;

174

175 cout<<"enter the first number"<<endl;

176

177 cin>>a8;

178

179 cout<<"enter the 2nd number"<<endl;

180

181 cin>>c8;

182

183 percentage = (float)a8/c8 * 100;

184

185 cout<<"percentage=" <<setprecision(20)<<percentage<<endl;

186

187 return 0;

188

189 }

190 }

191 }

ERRORS:

./calculator.cpp: In function ‘int main()’:

./calculator.cpp:43: error: expected primary-expression before ‘int’

./calculator.cpp:43: error: expected ‘)’ before ‘int’

./calculator.cpp:44: error: expected ‘;’ before ‘{’ token

./calculator.cpp:191: error: expected ‘}’ at end of input

./calculator.cpp:191: error: expected ‘}’ at end of input

Thanks in advance

### 5 Answers

- JazkaLv 71 decade agoFavorite Answer
I'm having a bit of trouble following your code but here's an alternative solution you could consider.

I got bored one day after class and decided to make a C++ calculator. I've extracted the relevant parts of my code (the entire program is 1,117 lines long so I'm not going to put it in - besides half of it isn't even relevant to what you've done).

(If however, you want a copy of it, just PM me (I think I've got the message function on - others have been able to message me) and I can upload copy onto some file sharing site.)

#include <iostream>

#include <iomanip>

#include <cmath>

using namespace std;

int main()

{

double num1, num2, i = 0, total, side;

int choice, numChoiceAdd, numAdd;

char choiceAdd;

do

{

cout << "Please select an option." << endl;

cout << "Select 1 for addition." << endl;

cout << "Select 2 for subtraction." << endl;

cout << "Select 3 for division." << endl;

cout << "Select 4 for multiplication." << endl;

cout << "Select 5 for the area of a square." << endl;

cout << "Select 6 for the square root." << endl;

cout << "Select 8 for pi." << endl;

cout << "Select 9 for percentage." << endl;

cin >> choice;

}

while (choice < 1 || choice > 9);

if (choice == 1)

{

do

{

cout << "Please enter a choice." << endl;

cout << "Enter 1 if you know the total number of numbers to be added." << endl;

cout << "Enter 2 if you don't know the total number of numbers to be added." << endl;

cin >> numChoiceAdd;

}

while (numChoiceAdd < 1 || numChoiceAdd > 2);

if (numChoiceAdd == 1)

{

cout << "How many numbers would you like to add up?" << endl;

cin >> numAdd;

for (i = 0; i < numAdd; i++)

{

cout << "Please enter a number." << endl;

cin >> num1;

total = total + num1;

}

cout << "The total sum of the numbers is " << total << "." << endl;

}

}

if (numChoiceAdd == 2)

{

do

{

cout << "Please enter a number." << endl;

cin >> num1;

total = total + num1;

cout << "Would you like to enter another number?" << endl;

cout << "Enter Y/y yes for yes, any other letter for no." << endl;

cin >> choiceAdd;

}

while (choiceAdd == 'Y' || choiceAdd == 'y');

cout << "The total sum of the numbers is " << total << "." << endl;

}

if (choice == 2)

{

cout << "Please enter a number." << endl;

cin >> num1;

cout << "Please enter a second number." << endl;

cin >> num2;

cout << "The difference between " << num1 << " and " << num2 << " is ";

cout << num1 - num2 << "." << endl;

}

if (choice == 3)

{

cout << "Please enter a number (numerator)." << endl;

cin >> num1;

cout << "Please enter a second number (denominator)." << endl;

cin >> num2;

if (num2 == 0)

{

cout << "Cannot divide by zero." << endl;

system("PAUSE");

exit(1);

}

else if (num2 != 0)

{

cout << num1 << " divided by " << num2 << " is " << num1 / num2 << "." << endl;

}

}

if (choice == 4)

{

cout << "Please enter a number." << endl;

cin >> num1;

cout << "Please enter a second number." << endl;

cin >> num2;

cout << "The product of " << num1 << " and " << num2 << " is ";

cout << num1 * num2 << "." << endl;

}

if (choice == 5)

{

cout << "Please enter the side length." << endl;

cin >> side;

cout << "The area of the square is " << pow(side, 2) << " units^2." << endl;

}

if (choice == 6)

{

cout << "Please enter a number." << endl;

cin >> num1;

if (num1 < 0 && num1 != -1)

{

cout << "Cannot calculate the square root of a negative number." << endl;

system("PAUSE");

exit(1);

}

else if (num1 == -1)

{

cout << "The square root of -1 is i." << endl; //You can leave this out if you want. It refers to imaginary numbers.

}

else if (num1 > 0)

{

cout << "The square root of " << num1 << " is " << sqrt(num1) << "." << endl;

}

}

/*

if (choice == 7)

{

}

if (choice == 8)

{

}

*/

system ("PAUSE");

return 0;

}

There are parts that I haven't done because I'm not sure what you want with them.

- KurtLv 51 decade ago
Why are you using a specific cast of op to int? It is already defined as integer.

- 1 decade ago
I dont think you need to typecast the op variable that your using in your switch case statement. try getting rid of the (int) before that and it should fix your problem.

- 1 decade ago
Line 152, you have a {, you can delete it. The compiler is having trouble to compile because of that early brace.

- How do you think about the answers? You can sign in to vote the answer.
- 1 decade ago
i have no experience at all with C++ plus but by the looks of those errors what was expect you could try to remove those expected items lol my guess

Source(s): pc gamer