943,923 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3739
  • C++ RSS
Apr 19th, 2005
0

Wierd error messages with calculator program

Expand Post »
Hi, it's been awhile since I've been here and the site has changed quite a bit, I like it. Anyway, I attempted to make a basic calculator program, but I'm getting some weird error messages. The error messages tell me that there are syntax errors, but I've looked through the code and I can't find any. Here is the code, and below that are the error messages:

C++ Syntax (Toggle Plain Text)
  1. /*
  2.  * BCalc:
  3.  * A calculator program with add, subtract, multiply, and divide functions.
  4.  *
  5.  * Programmer:
  6.  */
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. float number = 0;
  11.  
  12. int get_choice(int &choice);
  13. void handle_choice(int choice);
  14. float add_num(float number);
  15. float subtract_num(float number);
  16. float multiply_num(float number);
  17. float divide_num(float number);
  18.  
  19.  
  20. int main()
  21. {
  22. int choice;
  23.  
  24. while(get_choice(choice)); //while get_choice returns 1 call handle_choice function.
  25. {
  26. handle_choice(choice);
  27. }
  28.  
  29. return 0; //end of program
  30. }
  31.  
  32.  
  33. int get_choice(int &choice)
  34. {
  35. do
  36. {
  37. cout << "Select operation: \n";
  38. cout << "1 ~ addition.\n";
  39. cout << "2 ~ subtraction.\n";
  40. cout << "3 ~ multiplication.\n";
  41. cout << "4 ~ division.\n";
  42. cout << "5 ~ clear\n";
  43. cout << "6 ~ exit program.\n";
  44. cin >> choice;
  45.  
  46. if ((choice < 7 ) || (choice > 0))
  47. {
  48. cout << "invalid entry." << endl;
  49. }
  50.  
  51. if (choice == 6) //if choice equals 6 exit return 0 and exit program
  52. {
  53. return 0;
  54. }
  55. }while((choice < 7) || (choice > 0));
  56. return 1;
  57. }
  58.  
  59.  
  60. void handle_choice(int choice)
  61. {
  62. switch(choice)
  63. {
  64. case 1:
  65. number = add_num(number); //assign value returned by function to global variable number
  66. break;
  67.  
  68. case 2:
  69. number = subtract_num(number);
  70. break;
  71.  
  72. case 3:
  73. number = multiply_num(number);
  74. break;
  75.  
  76. case 4:
  77. number = divide_num(number);
  78. break;
  79.  
  80. case 5:
  81. number = 0; //if 5 is entered set number variable to 0
  82. break;
  83.  
  84. default:
  85. cout << "invalid entry" << endl;
  86. break;
  87. }
  88. }
  89.  
  90.  
  91. float add_num(float number)
  92. {
  93. float i = 0;
  94. float j = 0;
  95. char return_main = 'C';
  96.  
  97. do
  98. {
  99. if(number == 0) //if number is 0 prompt user for two numbers to add.
  100. {
  101. cout << "enter first number to add ";
  102. cin >> i;
  103. cout << "Enter second number to add ";
  104. cin >> j;
  105. number = i + j;
  106. cout << number;
  107. }
  108.  
  109. else //if number is not 0 prompt user for the number to be added
  110. {
  111. cout << "Enter number to add ";
  112. cin >> i;
  113. number += i;
  114. cout << number;
  115. }
  116.  
  117. cout << "enter R to return, C to continue addition ";
  118. cin >> return_main;
  119.  
  120. }while(return_main != 'r'); //while return_main is not r loop through the function.
  121.  
  122. return (number); //when return_main is r return the sum.
  123. }
  124.  
  125.  
  126. float subtract_num(float number)
  127. {
  128. float i, j;
  129. char return_main;
  130.  
  131. do
  132. {
  133. if(number == 0)
  134. {
  135. cout << "Enter number to subtract from ";
  136. cin >> i;
  137. cout << "Enter amount to subtract ";
  138. cin >> j;
  139. number = i - j;
  140. cout << number;
  141. }
  142.  
  143. else
  144. {
  145. cout << "Enter amount to subtract ";
  146. cin >> i;
  147. number -= i;
  148. cout << number;
  149. }
  150.  
  151. cout << "Enter R to return, C to continue subtraction ";
  152. cin >> return_main;
  153. }while(return_main != 'r');
  154.  
  155. return (number);
  156. }
  157.  
  158.  
  159. float multiply_num(float number)
  160. {
  161. float i, j;
  162. char return_main;
  163.  
  164. if(number == 0)
  165. {
  166. cout << "Enter number to multiply ";
  167. cin >> i;
  168. cout << " times ";
  169. cin >> j;
  170. number = i * j;
  171. cout << number;
  172. }
  173.  
  174. else
  175. {
  176. cout << number << " times ";
  177. cin >> i;
  178. number *= i;
  179. cout << number << endl;
  180. }
  181.  
  182. cout << "Enter R to return to main menu\n";
  183. cin >> return_main;
  184. }while(return_main != 'r');
  185.  
  186. return (number);
  187. }
  188.  
  189.  
  190. float divide_num(float number)
  191. {
  192. float i, j;
  193. char return_main;
  194.  
  195. do
  196. {
  197. if(number == 0)
  198. {
  199. cout << "Enter number to be divided ";
  200. cin >> i;
  201. cout << "divided by ";
  202. cin >> j;
  203. number = i / j;
  204. cout << number;
  205. }
  206.  
  207. else
  208. {
  209. cout << number << " divided by ";
  210. cin >> i;
  211. number /= i;
  212. cout << number << endl;
  213. }
  214.  
  215. cout << "Enter r to return, c to continue ";
  216. cin >> return_main;
  217.  
  218. }while(return_main != 'r');
  219.  
  220. return (number);
  221. }


C++ Syntax (Toggle Plain Text)
  1. BCalc.cpp
  2. C:\Program Files\Microsoft Visual Studio\VC98\BCalc.cpp(184) : error C2143: syntax error : missing ';' before 'while'
  3. C:\Program Files\Microsoft Visual Studio\VC98\BCalc.cpp(186) : error C2143: syntax error : missing ';' before 'return'
  4. C:\Program Files\Microsoft Visual Studio\VC98\BCalc.cpp(187) : error C2143: syntax error : missing ';' before '}'
  5. C:\Program Files\Microsoft Visual Studio\VC98\BCalc.cpp(187) : error C2143: syntax error : missing ';' before '}'
  6. C:\Program Files\Microsoft Visual Studio\VC98\BCalc.cpp(187) : error C2143: syntax error : missing ';' before '}'
  7. C:\Program Files\Microsoft Visual Studio\VC98\BCalc.cpp(191) : error C2143: syntax error : missing ';' before '{'
  8. C:\Program Files\Microsoft Visual Studio\VC98\BCalc.cpp(191) : error C2447: missing function header (old-style formal list?)
  9. Error executing cl.exe.
Similar Threads
Reputation Points: 12
Solved Threads: 0
Light Poster
the b is offline Offline
42 posts
since Sep 2004
Apr 19th, 2005
0

Re: Wierd error messages with calculator program

Match each { to its }. Are you trying to do a do...while loop without the do?
float multiply_num(float number)
{
	float i, j;
	char return_main;

	if(number == 0)
	{
		cout << "Enter number to multiply ";
		cin >> i;
		cout << " times ";
		cin >> j;
		number = i * j;
		cout << number;
	}

	else
	{
		cout << number << " times ";
		cin >> i;
		number *= i;
		cout << number << endl;
	}
		
	cout << "Enter R to return to main menu\n";
	cin >> return_main;
}while(return_main != 'r');

	return (number);
}
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Apr 20th, 2005
0

Re: Wierd error messages with calculator program

Quote originally posted by Dave Sinkula ...
Match each { to its }. Are you trying to do a do...while loop without the do?
}while(return_main != 'r');

	return (number);
}
:o Wow, that little bit made me feel kind of dumb, it helped out alot though, thanks.
Reputation Points: 12
Solved Threads: 0
Light Poster
the b is offline Offline
42 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Playing cards
Next Thread in C++ Forum Timeline: subtraction operator overload





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC