Wierd error messages with calculator program

Reply

Join Date: Sep 2004
Posts: 42
Reputation: the b is an unknown quantity at this point 
Solved Threads: 0
the b the b is offline Offline
Light Poster

Wierd error messages with calculator program

 
0
  #1
Apr 19th, 2005
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:

  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. }


  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,773
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 308
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Wierd error messages with calculator program

 
0
  #2
Apr 19th, 2005
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);
}
“The essential notion of a socialist society is force.”
— Milton Friedman
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 42
Reputation: the b is an unknown quantity at this point 
Solved Threads: 0
the b the b is offline Offline
Light Poster

Re: Wierd error messages with calculator program

 
0
  #3
Apr 19th, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 3456 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC