Void Functions

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 14
Reputation: ASFtlink is an unknown quantity at this point 
Solved Threads: 0
ASFtlink ASFtlink is offline Offline
Newbie Poster

Void Functions

 
0
  #1
20 Days Ago
This program ask user which arithmetic operation they want to play with then generates a random equation and lets the user answer it. When the user stop playing the game the program calculates the number of correct and answers and wrong answers.

I need some help because the program crashes when doing divisions.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <cstdlib>
  7. #include <iomanip>
  8.  
  9. //include header file
  10. #include "lab_17_head.h"
  11.  
  12. using namespace std;
  13.  
  14. int main( )
  15. {
  16.  
  17. //variable declaration
  18. int number1, number2; //vars to store int values
  19. double intResult, doubleResult, result; //vars to store the result
  20. char action; //var for store the operator
  21. char flag; //var to stor the choice
  22. int plusWins, plusLosses, plusTotal, //plus stats for winning, losing, and total games played
  23. minuWins, minuLosses, minuTotal, //minus stats for winning, losing, and total games played
  24. multWins, multLosses, multTotal, //multiplication stats for winning, losing, and total games played
  25. diviWins, diviLosses, diviTotal; //division stats for winning, losing, and total games played
  26. double answer; //var for answer from the user
  27.  
  28. //initializations
  29. plusWins = plusLosses = plusTotal = 0;
  30. minuWins = minuLosses = minuTotal = 0;
  31. multWins = multLosses = multTotal = 0;
  32. diviWins = diviLosses = diviTotal = 0;
  33.  
  34. //show welcome message
  35. welcome();
  36.  
  37. //enter your choice
  38. getChoice(flag);
  39.  
  40. //start loop to play the game
  41. while(flag=='y' || flag=='Y')
  42. {
  43. //get operator
  44. getOperator(action);
  45.  
  46. //four cases for action
  47. switch(action)
  48. {
  49. case '+': //do addition
  50. //get two random numbers of two digits
  51. getTwoNumbers(number1, number2);
  52. //add
  53. intResult = add(number1, number2);
  54. //getAnsers from the user
  55. getAnswer(number1, number2, action, answer);
  56. //win or loss
  57. addGame(intResult, static_cast<int>(answer), plusWins, plusLosses, plusTotal);
  58. break;
  59.  
  60. case '-': //do substraction
  61. //get two random numbers of two digits
  62. getTwoNumbers(number1, number2);
  63. //add
  64. intResult = minus(number1, number2);
  65. //getAnsers from the user
  66. getAnswer(number1, number2, action, answer);
  67. //win or loss
  68. minuGame(intResult, static_cast<int>(answer), minuWins, minuLosses, minuTotal);
  69. break;
  70.  
  71. case '*': //do multiplication
  72. /***************************************************
  73. This part is for you to complete
  74. ***************************************************/
  75. //get two random numbers of two digits
  76. getTwoNumbers(number1, number2);
  77. //multiply
  78. intResult = multiply(number1, number2);
  79. //getAnsers from the user
  80. getAnswer(number1, number2, action, answer);
  81. //win or loss
  82. multiGame(intResult, static_cast<int>(answer), multWins, multLosses, multTotal);
  83. break;
  84.  
  85.  
  86. case '/': //do division
  87. /***************************************************
  88. This part is for you to complete
  89. ***************************************************/
  90. //get two random numbers of two digits
  91. getTwoNumbers(number1, number2);
  92. //add
  93. intResult = divide(number1, number2);
  94. //getAnsers from the user
  95. getAnswer(number1, number2, action, answer);
  96. //win or loss
  97. diviGame(result, static_cast<int>(answer), diviWins, diviLosses, diviTotal);
  98. break;
  99.  
  100.  
  101. default:
  102. cout<<action<<" is not a valid operator ." <<endl;
  103. }
  104.  
  105. //get choice
  106. getChoice(flag);
  107. }
  108.  
  109. //show stats
  110. statsShow(plusWins, plusLosses, plusTotal, minuWins, minuLosses, minuTotal,
  111. multWins, multLosses, multTotal, diviWins, diviLosses, diviTotal);
  112.  
  113. //byebye
  114. bye();
  115.  
  116. //well done and exit
  117. return 0;
  118. }

This is the header used

  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. #ifndef LAB_17_HEAD_H
  7. #define LAB_17_HEAD_H
  8.  
  9. //show welcome message
  10. void welcome()
  11. {
  12. cout<<" Welcome to my math world and enjoy some training ...... "<<endl;
  13. return; //no value is returned here
  14. }
  15.  
  16. //get the choice
  17. void getChoice(char & ch) //use reference paramter to receive the choice
  18. {
  19. cout<<"\n Would you like to play the game? (y/n) => ";
  20. cin>>ch; //get the choice
  21. return;
  22. }
  23.  
  24. //get operator
  25. void getOperator(char & action) //use reference paramter to receive the action
  26. {
  27. cout<<"\n Which operation do you like to play? (+, -, *, /) => ";
  28. cin>>action;
  29. return; //get the choice
  30. }
  31.  
  32. //get two random numbers of two digits
  33. void getTwoNumbers(int & number1, int & number2) //use reference paramter to receive the action
  34. {
  35. unsigned seed; //local variable for random seed
  36.  
  37. //get a random seed
  38. cout<<"\n Give me an integer for random =>";
  39. cin>>seed;
  40.  
  41. //set the random seed
  42. srand(seed);
  43.  
  44. //get two random numbers of single digit
  45. number1 = rand()%10;
  46. number2 = rand()%10;
  47.  
  48. return;
  49. }
  50.  
  51.  
  52. //do addition
  53. int add(const int number1, const int number2) //const value parameters
  54. {
  55. return number1 + number2; //return the sum
  56. }
  57.  
  58. //do substraction
  59. int minus(const int number1, const int number2) //const value parameters
  60. {
  61. return number1 - number2; //return the substraction
  62. }
  63.  
  64. //do addition
  65. int multiply(const int number1, const int number2)//const value parameters
  66. {
  67. return number1 * number2; //return the multiplication
  68. }
  69.  
  70. //do addition
  71. double divide(const int number1, const int number2)//const value parameters
  72. {
  73. return static_cast<double>(number1) / static_cast<double>(number2);
  74. //return the division
  75. }
  76.  
  77. //getAnsers from the user
  78. void getAnswer(const int number1, const int number2, const char action,
  79. double & answer)
  80. {
  81. cout<<endl<<number1<< " " <<action <<" " <<number2<< " = ? ";
  82. cin>>answer;
  83. }
  84.  
  85. //addition win or loss
  86. void addGame(const int intResult, const int answer,
  87. int & plusWins, int & plusLosses, int & plusTotal)
  88. {
  89. if (answer == intResult) //decide winning
  90. plusWins++;
  91. else //or loss
  92. plusLosses++;
  93.  
  94. plusTotal++; //counting the games
  95. }
  96.  
  97. //substrcation win or loss
  98. void minuGame(const int intResult, const int answer,
  99. int & minuWins, int & minuLosses, int & minuTotal)
  100. {
  101. /*********************************************************
  102. This part is for you to complete
  103. *********************************************************/
  104. if (answer == intResult) //decide winning
  105. minuWins++;
  106. else //or loss
  107. minuLosses++;
  108.  
  109. minuTotal++;
  110. }
  111.  
  112. //multiplication win or loss
  113. void multiGame(const int intResult, const int answer,
  114. int & multWins, int & multLosses, int & multTotal)
  115. {
  116. /*********************************************************
  117. This part is for you to complete
  118. *********************************************************/
  119. if (answer == intResult) //decide winning
  120. multWins++;
  121. else //or loss
  122. multLosses++;
  123.  
  124. multTotal++;
  125. }
  126.  
  127. //division win or loss
  128. void diviGame(const double result, const double answer,
  129. int & diviWins, int & diviLosses, int & diviTotal)
  130. {
  131. /********************************************************
  132. This part is for you to complete
  133. ********************************************************/
  134. if (answer == result) //decide winning
  135. diviWins++;
  136. else //or loss
  137. diviLosses++;
  138.  
  139. diviTotal++;
  140.  
  141. }
  142.  
  143. //show stats
  144. void statsShow(const int plusWins, const int plusLosses, const int plusTotal,
  145. const int minuWins, const int minuLosses, const int minuTotal,
  146. const int multWins, const int multLosses, const int multTotal,
  147. const int diviWins, const int diviLosses, const int diviTotal)
  148. {
  149. double plusScore, //local variable for scores
  150. minuScore,
  151. multScore,
  152. diviScore;
  153.  
  154. //compute the scores
  155. plusScore = plusTotal==0 ? 0: static_cast<double>(plusWins)/static_cast<double>(plusTotal);
  156. plusScore *= 100;
  157. minuScore = minuTotal==0? 0: static_cast<double>(minuWins)/static_cast<double>(minuTotal);
  158. minuScore *= 100;
  159. multScore = multTotal==0? 0: static_cast<double>(multWins)/static_cast<double>(multTotal);
  160. multScore *= 100;
  161. diviScore = diviTotal==0? 0: static_cast<double>(diviWins)/static_cast<double>(diviTotal);
  162. diviScore *= 100;
  163.  
  164. cout<<endl;
  165.  
  166. //for addition
  167. cout<<" You played "<<plusTotal<<" additions."<<endl
  168. <<" You won " <<plusWins <<" many times." <<endl
  169. <<" You lost "<<plusLosses<<" many times." <<endl
  170. <<" Your addition score is " <<setprecision(2)<<fixed<<showpoint
  171. << plusScore
  172. <<endl<<endl;
  173.  
  174. //for substraction
  175. cout<<" You played "<<minuTotal<<" substractions."<<endl
  176. <<" You won " <<minuWins <<" many times." <<endl
  177. <<" You lost "<<minuLosses<<" many times." <<endl
  178. <<" Your substration score is " <<setprecision(2)<<fixed<<showpoint
  179. << minuScore
  180. <<endl<<endl;
  181.  
  182. //for multiplication
  183. cout<<" You played "<<multTotal<<" multiplications."<<endl
  184. <<" You won " <<multWins <<" many times." <<endl
  185. <<" You lost "<<multLosses<<" many times." <<endl
  186. <<" Your multiplication score is " <<setprecision(2)<<fixed<<showpoint
  187. << multScore
  188. <<endl<<endl;
  189.  
  190. //for division
  191. cout<<" You played "<<diviTotal<<" divisions."<<endl
  192. <<" You won " <<diviWins <<" many times." <<endl
  193. <<" You lost "<<diviLosses<<" many times." <<endl
  194. <<" Your division score is " <<setprecision(2)<<fixed<<showpoint
  195. << diviScore
  196. <<endl<<endl;
  197.  
  198. }
  199.  
  200. //byebye
  201. void bye()
  202. {
  203. cout<<"Hope you enjoyed playing this game. Byebye." <<endl<<endl;
  204. return;
  205. }
  206.  
  207.  
  208. #endif
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,673
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso
 
1
  #2
20 Days Ago
You should check where the program crashes, and read the error message(s).

Check your arguments closely - what's being used here, and what is being used without having a value assigned?
  1. intResult = divide(number1, number2);
  2. //getAnsers from the user
  3. getAnswer(number1, number2, action, answer);
  4. //win or loss
  5. diviGame(result, static_cast<int>(answer), diviWins, diviLosses, diviTotal);
I'm confused. You store a result to intResult, but that value returned is a double. What do you really mean?

Shouldn't that value you got back from the divide( ) function be what's sent to diviGame( ) for evaluation?

And in diviGame(), you are trying to compare two doubles for equality - that's a crapshoot at best.

Why do you put the const modifier on so many of the parameters that are passed by value? That's not a common thing to do.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: abhi_elementx is an unknown quantity at this point 
Solved Threads: 6
abhi_elementx's Avatar
abhi_elementx abhi_elementx is offline Offline
Junior Poster
 
0
  #3
20 Days Ago
I'm confused. You store a result to intResult, but that value returned is a double. What do you really mean?
I see that the intResult variable is a double type.

There are issues with the types alright.
PEACE !
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: abhi_elementx is an unknown quantity at this point 
Solved Threads: 6
abhi_elementx's Avatar
abhi_elementx abhi_elementx is offline Offline
Junior Poster

Re: Void Functions

 
0
  #4
20 Days Ago
OK. I've tried some changes and seems it's working now(hopefully)

change your diviGame() function to

  1. diviGame(intResult, answer, diviWins, diviLosses, diviTotal);

Change you divide() function(in the header file) to :
  1. double divide(const int number1, const int number2)//const value parameters
  2. {
  3.  
  4. return double((double)number1 / (double)number2);
  5. //return the division
  6. }

and that should return an answer with correct precision.

Hope that helps.
Cheers
PEACE !
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 14
Reputation: ASFtlink is an unknown quantity at this point 
Solved Threads: 0
ASFtlink ASFtlink is offline Offline
Newbie Poster
 
0
  #5
20 Days Ago
I see...I changed "intResult" to "doubleResult" and it doesn't crash anymore, but now the program takes any answer to a division which has decimals or are in fraction form as wrong answer, any idea on how to fix this? sorry I;m new to C++

Oh and the adding and subtracting parts were already in the program I just has to complete multiplying and diving so I just followed the format on adding and subtracting.

Edit: Sorry abhi_elementx I didn't see you posts I'm going to try that now, thanks.
Last edited by ASFtlink; 20 Days Ago at 3:06 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: abhi_elementx is an unknown quantity at this point 
Solved Threads: 6
abhi_elementx's Avatar
abhi_elementx abhi_elementx is offline Offline
Junior Poster
 
0
  #6
20 Days Ago
no probs
PEACE !
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 14
Reputation: ASFtlink is an unknown quantity at this point 
Solved Threads: 0
ASFtlink ASFtlink is offline Offline
Newbie Poster
 
0
  #7
20 Days Ago
Ok your way it does get decimals, but it still wont get fractions it takes them as wrong answers
ex. "5 / 6 = ?" and instead of typing "0.8333..." I answer "5/6" and it takes it as wrong.
Any ideas?
Last edited by ASFtlink; 20 Days Ago at 3:24 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: abhi_elementx is an unknown quantity at this point 
Solved Threads: 6
abhi_elementx's Avatar
abhi_elementx abhi_elementx is offline Offline
Junior Poster
 
0
  #8
20 Days Ago
I tried this:
Welcome to my math world and enjoy some training ......

Would you like to play the game? (y/n) => y

Which operation do you like to play? (+, -, *, /) => /

Give me an integer for random =>3

1.2
6 / 5 = ? 1.2

Would you like to play the game? (y/n) => n

You played 0 additions.
You won 0 many times.
You lost 0 many times.
Your addition score is 0.00

You played 0 substractions.
You won 0 many times.
You lost 0 many times.
Your substration score is 0.00

You played 0 multiplications.
You won 0 many times.
You lost 0 many times.
Your multiplication score is 0.00

You played 1 divisions.
You won 1 many times.
You lost 0 many times.
Your division score is 100.00


Shudnt this be the output?
PEACE !
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 14
Reputation: ASFtlink is an unknown quantity at this point 
Solved Threads: 0
ASFtlink ASFtlink is offline Offline
Newbie Poster
 
0
  #9
20 Days Ago
Yes that's right but you did 6/5, try the random integer of 3.
it is 5/6 which is 0.83333333333333333333333333333... so you need that in a fraction but it takes the fraction as wrong, I just need it to be able to take fractions.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: crash1989 is an unknown quantity at this point 
Solved Threads: 1
crash1989 crash1989 is offline Offline
Newbie Poster
 
0
  #10
20 Days Ago
Always Check for division with zero .. the rand might get it zero
Reply With Quote Quick reply to this message  
Reply

Tags
functions, void

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC