943,946 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 534
  • C++ RSS
Nov 7th, 2009
0

Need Help on Project

Expand Post »
Hi, my name is Anita. I'm new to this forum and I'm here because I am utterly frustrated with this program that I have been working on for days. This program is supposed to ask the user a series of arithmetic problems and report on how the user performs. The way we are supposed to go about doing this is broken into phases.(I will put exactly what the assignment says at the end) Anyway, I've got two major problems right now. I got up to phase III, and then could NOT figure out how to change the parameters of my doOneSet function to do addition, subtraction and multiplication. My second problem is that up until that point my program worked when I used the g++ compiler on the Mac computers at school. But when I tried to compile the same exact .cpp file using DevC++ at home, it's giving me weird long numbers. The problem with this assignment is how she wants it done.(see end of post)

C++ Syntax (Toggle Plain Text)
  1. //***************************************************************************
  2. //This program asks a series of arithmetic problems
  3. //and reports the user's performance
  4. //***************************************************************************
  5. #include<iostream>
  6. #include<iomanip>
  7. #include<cstdlib>
  8. #include<ctime>
  9. using namespace std;
  10.  
  11. void doOneSet();
  12. void doOneProblem();
  13. void generateOperands(int &num1, int &num2);
  14. void calcCorrectAnswer(int &result, int &userNum1);
  15. void checkAnswer(int result, int userNum1);
  16.  
  17.  
  18. int main()
  19. {
  20. srand(time(0));
  21. doOneSet();
  22.  
  23.  
  24. system("pause");
  25. return 0;
  26. }
  27. //***************************************************************************
  28. void doOneSet()
  29. {
  30.  
  31. doOneProblem();
  32. doOneProblem();
  33. doOneProblem();
  34. doOneProblem();
  35. doOneProblem();
  36. }
  37. //***************************************************************************
  38. void doOneProblem()
  39. {
  40. int num1, num2, result, userNum1;
  41.  
  42. generateOperands(num1, num2);
  43. calcCorrectAnswer(result, userNum1);
  44. checkAnswer(result, userNum1);
  45.  
  46. }
  47. //***************************************************************************
  48. void generateOperands(int &num1, int &num2)
  49. {
  50.  
  51.  
  52. num1=rand()%101;
  53. num2=rand()%101;
  54. }
  55. //***************************************************************************
  56.  
  57. void calcCorrectAnswer(int &result, int &userNum1)
  58. {
  59. int num1, num2;
  60.  
  61. cout<<num1<<"+"<<num2<<"="; //To make arithmetic problem
  62. cin>>userNum1;
  63. result=num1+num2;
  64.  
  65. }
  66. //**************************************************************************
  67. void checkAnswer(int result, int userNum1)
  68. {
  69.  
  70. if(userNum1==result)
  71. {
  72. cout<<"correct"<<endl;
  73. }
  74. else
  75. {
  76. cout<<"incorrect"<<endl;
  77. }
  78. }

Here is a link to my assignment. It's a .pdf file so I couldn't just copy and paste it here.
http://docs.google.com/fileview?id=0...M2ZmMGY5&hl=en

Thanks for any help!
Last edited by NitaB; Nov 7th, 2009 at 4:36 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
NitaB is offline Offline
63 posts
since Nov 2009
Nov 7th, 2009
0
Re: Need Help on Project
For the first problem you could put some if cases inside doOneSet( ) to catch the character passed into it, '+', '-' or '*'.
Then depending on what the character was do the appropriate calculation.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
AAAKsu is offline Offline
7 posts
since Nov 2009
Nov 7th, 2009
-7
Re: Need Help on Project
line 61 in the code you posted: That function displays random/wrong values because it is using uninitialized variables.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Nov 7th, 2009
0
Re: Need Help on Project
Quote ...
and then could NOT figure out how to change the parameters of my doOneSet function to do addition, subtraction and multiplication
There are many ways to go about this. Since you seem interested in changing the parameter list of your doOneSet() function... maybe we could do something like this (ok, I changed the parameter list of your doOneProblem() function instead.. it was easier for me)
C++ Syntax (Toggle Plain Text)
  1. void doOneSet()
  2. {
  3. //simple loop will call the doOneProblem() function 5 times
  4. //instead of having to write out each individual function call
  5. for(int i=0; i<5; i++)
  6. {
  7. //Here we will supply a random number: 0,1 or 2
  8. //we'll make 0 = add, 1 = subtract, and 2 = multiply
  9. doOneProblem(rand%3);
  10. }
  11. }

Now when you get down to calcCorrectAnswer(), you could have something like this:
C++ Syntax (Toggle Plain Text)
  1. void calcCorrectAnswer(int &result, int &userNum1, int& operation)
  2. {
  3.  
  4. int num1, num2;
  5.  
  6. switch(operation)
  7. {
  8. //addition
  9. case 0: cout << num1 << " + " << num2 << " = ";
  10. cin >> userNum1;
  11. result = num1 + num2;
  12. break;
  13. //subraction
  14. case 1: cout << num1 << " - " << num2 << " = ";
  15. cin >> userNum1;
  16. result = num1 - num2;
  17. break;
  18. //multiplication
  19. case 2: cout << num1 << " * " << num2 << " = ";
  20. cin >> userNum1;
  21. result = num1 * num2;
  22. break;
  23.  
  24. default: cout << "Error..!";
  25. }
  26.  
  27. checkAnswer(result, userNum1);
  28. }

Even if this isn't exactly what you want, feel free to modify it as you like.. it's mainly here just to give you an idea of what you can do in efforts to resolve your problem.

This is all untested/uncompiled code and may contain easy to fix errors. If anyone has a different approach to this problem please feel free to share with the crowd.
Last edited by Clinton Portis; Nov 7th, 2009 at 6:19 pm. Reason: ninjas made me edit my post.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Nov 7th, 2009
0
Re: Need Help on Project
@ Ancient Dragon: I fixed what you said and some other similar issues. Thanks. So now my code works and I'm at the same point.
@ Clinton Portis: the way my professor wants the main function to look is this:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. srand(time(0));
  4. doOneSet('+');
  5. doOneSet('-');
  6. doOneSet('*');
  7. }

The thing is, she went on maternity leave and my class went from being a regular class to an online, we gotta teach ourselves kind of class. My question at this point is by putting the +,-,* inside the () in my main function, do I need to add some kind of char parameter to doOneSet? I want to eventually put a switch in my calcCorrectAnswer function. My ultimate goal at this point is to create 5 random addition problems, then 5 random subtraction problems and 5 random multiplication problems. Below is my fixed code that only writes out the 5 addition problems. I am going to keep working on it to see what approach I would take to go forward. Any pointers? Thanks!
C++ Syntax (Toggle Plain Text)
  1. //***************************************************************************
  2. //This program asks a series of arithmetic problems
  3. //and reports the user's performance
  4. //***************************************************************************
  5. #include<iostream>
  6. #include<iomanip>
  7. #include<cstdlib>
  8. #include<ctime>
  9. using namespace std;
  10.  
  11. void doOneSet();
  12. void doOneProblem();
  13. void generateOperands(int &num1, int &num2);
  14. void calcCorrectAnswer(int num1, int num2, int &result, int &userNum1);
  15. void checkAnswer(int result, int userNum1);
  16.  
  17.  
  18. int main()
  19. {
  20. srand(time(0));
  21. doOneSet();
  22.  
  23.  
  24.  
  25. return 0;
  26. }
  27. //***************************************************************************
  28. void doOneSet()
  29. {
  30.  
  31. doOneProblem();
  32. doOneProblem();
  33. doOneProblem();
  34. doOneProblem();
  35. doOneProblem();
  36. }
  37. //***************************************************************************
  38. void doOneProblem()
  39. {
  40. int num1, num2, result, userNum1;
  41.  
  42. generateOperands(num1, num2);
  43. calcCorrectAnswer(num1, num2, result, userNum1);
  44. checkAnswer(result, userNum1);
  45.  
  46. }
  47. //***************************************************************************
  48. void generateOperands(int &num1, int &num2)
  49. {
  50.  
  51.  
  52. num1=rand()%101;
  53. num2=rand()%101;
  54. }
  55. //***************************************************************************
  56.  
  57. void calcCorrectAnswer(int num1, int num2, int &result, int &userNum1)
  58. {
  59.  
  60.  
  61. cout<<num1<<"+"<<num2<<"="; //To make arithmetic problem
  62. cin>>userNum1;
  63. result=num1+num2;
  64.  
  65. }
  66. //**************************************************************************
  67. void checkAnswer(int result, int userNum1)
  68. {
  69.  
  70. if(userNum1==result)
  71. {
  72. cout<<"correct"<<endl;
  73. }
  74. else
  75. {
  76. cout<<"incorrect"<<endl;
  77. }
  78. }


There are many ways to go about this. Since you seem interested in changing the parameter list of your doOneSet() function... maybe we could do something like this (ok, I changed the parameter list of your doOneProblem() function instead.. it was easier for me)
C++ Syntax (Toggle Plain Text)
  1. void doOneSet()
  2. {
  3. //simple loop will call the doOneProblem() function 5 times
  4. //instead of having to write out each individual function call
  5. for(int i=0; i<5; i++)
  6. {
  7. //Here we will supply a random number: 0,1 or 2
  8. //we'll make 0 = add, 1 = subtract, and 2 = multiply
  9. doOneProblem(rand%3);
  10. }
  11. }

Now when you get down to calcCorrectAnswer(), you could have something like this:
C++ Syntax (Toggle Plain Text)
  1. void calcCorrectAnswer(int &result, int &userNum1, int& operation)
  2. {
  3.  
  4. int num1, num2;
  5.  
  6. switch(operation)
  7. {
  8. //addition
  9. case 0: cout << num1 << " + " << num2 << " = ";
  10. cin >> userNum1;
  11. result = num1 + num2;
  12. break;
  13. //subraction
  14. case 1: cout << num1 << " - " << num2 << " = ";
  15. cin >> userNum1;
  16. result = num1 - num2;
  17. break;
  18. //multiplication
  19. case 2: cout << num1 << " * " << num2 << " = ";
  20. cin >> userNum1;
  21. result = num1 * num2;
  22. break;
  23.  
  24. default: cout << "Error..!";
  25. }
  26.  
  27. checkAnswer(result, userNum1);
  28. }

Even if this isn't exactly what you want, feel free to modify it as you like.. it's mainly here just to give you an idea of what you can do in efforts to resolve your problem.

This is all untested/uncompiled code and may contain easy to fix errors. If anyone has a different approach to this problem please feel free to share with the crowd.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
NitaB is offline Offline
63 posts
since Nov 2009
Nov 8th, 2009
0
Re: Need Help on Project
What you are suggesting is very possible, just change the desired function to accept a char parameter. Then at some point, you can use if/else logic based on that char argument to determine what type of problem ye' wish to produce.
Last edited by Clinton Portis; Nov 8th, 2009 at 12:16 am.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Nov 8th, 2009
0
Re: Need Help on Project
Thanks to everyone who has input help so far! I really appreciate it. Anyway, I'm almost finished but this last part is not working. It is my printReport function that is supposed to give a report of the user's overall performance. However, my set1Correct, set2Correct, and set3Correct variables always come out to be 0. It's like they aren't getting the value from my checkAnswer function.
This is my main function
C++ Syntax (Toggle Plain Text)
  1.  
  2. int main()
  3. {
  4. int probsPerSet;
  5. int set1Correct=0;
  6. int set2Correct=0;
  7. int set3Correct=0;
  8.  
  9. srand(time(0));
  10. getProbsPerSet(probsPerSet);
  11. doOneSet('+',probsPerSet);
  12. doOneSet('-',probsPerSet);
  13. doOneSet('*',probsPerSet);
  14. printReport(probsPerSet, set1Correct, set2Correct, set3Correct);
  15.  
  16.  
  17. system("pause");
  18. return 0;
  19. }

This is doOneSet function. I think I need to add another parameter here...
C++ Syntax (Toggle Plain Text)
  1. void doOneSet(char problemType, int probsPerSet)
  2. {
  3. int maxNum;
  4.  
  5. printHeader(problemType);
  6. getMaxNum(maxNum);
  7. for (int i=0;i<probsPerSet;i++)
  8. {
  9. doOneProblem(problemType,probsPerSet,maxNum);
  10. }
  11.  
  12. }
This is my doOneProblem function which may be where the problem is.
C++ Syntax (Toggle Plain Text)
  1. void doOneProblem(char problemType, int probsPerSet, int maxNum)
  2. {
  3. int num1, num2, result, userNum1, set1Correct, set2Correct, set3Correct;
  4.  
  5. switch (problemType)
  6. {
  7. case '+':generateOperands(num1, num2, maxNum);
  8. calcCorrectAnswer(num1, num2, result, userNum1, problemType);
  9. checkAnswer(result, userNum1, problemType,set1Correct,set2Correct,set3Correct,num1,num2);
  10. break;
  11. case '-':generateOperands(num1, num2, maxNum);
  12. calcCorrectAnswer(num1, num2, result, userNum1, problemType);
  13. checkAnswer(result, userNum1, problemType,set1Correct,set2Correct,set3Correct,num1,num2);
  14. break;
  15. case '*':generateOperands(num1, num2, maxNum);
  16. calcCorrectAnswer(num1, num2, result, userNum1, problemType);
  17. checkAnswer(result, userNum1, problemType,set1Correct,set2Correct,set3Correct,num1,num2);
  18. break;
  19. }
  20.  
  21.  
  22. }

My checkAnswer function: or maybe this is the problem. lol!
C++ Syntax (Toggle Plain Text)
  1. void checkAnswer(int result, int userNum1, char problemType, int &set1Correct, int &set2Correct, int &set3Correct, int num1, int num2)
  2. {
  3. if(userNum1==result)
  4. {
  5. cout<<"correct"<<endl;
  6. }
  7. else
  8. {
  9. cout<<"incorrect"<<endl;
  10. }
  11.  
  12. switch (problemType)
  13. {
  14. case '+': if (result=num1+num2)
  15. {
  16. set1Correct=set1Correct+1;
  17. }
  18. break;
  19. case '-':if (result=num1-num2)
  20. {
  21. set2Correct=set2Correct+1;
  22. }
  23. break;
  24. case '*':if (result=num1*num2)
  25. {
  26. set3Correct=set3Correct+1;
  27. }
  28. break;
  29. }
  30.  
  31. }
And last but not least, the printReport function:
C++ Syntax (Toggle Plain Text)
  1. void printReport(int getProbsPerSet, int set1Correct, int set2Correct, int set3Correct)
  2. {
  3. int totalProbs=getProbsPerSet*3;
  4. int totalProbsCorrect=set1Correct+set2Correct+set3Correct;
  5. float set1Percentage=set1Correct/getProbsPerSet*100;
  6. float set2Percentage=set2Correct/getProbsPerSet*100;
  7. float set3Percentage=set3Correct/getProbsPerSet*100;
  8. float totalPercentage=totalProbsCorrect/getProbsPerSet*3;
  9.  
  10. cout<<endl;
  11. cout<<"Set #1: You got "<<set1Correct<<" out of "<<getProbsPerSet<<"for ";
  12. cout<<set1Percentage<<"%"<<endl;
  13.  
  14. cout<<"Set #2: You got "<<set2Correct<<" out of "<<getProbsPerSet<<"for ";
  15. cout<<set2Percentage<<"%"<<endl;
  16.  
  17. cout<<"Set #3: You got "<<set3Correct<<" out of "<<getProbsPerSet<<"for ";
  18. cout<<set3Percentage<<"%"<<endl;
  19. }

Once again, thanks for any pointers.
Last edited by NitaB; Nov 8th, 2009 at 7:26 pm.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
NitaB is offline Offline
63 posts
since Nov 2009
Nov 9th, 2009
0
Re: Need Help on Project
Hi NitaB.
I think you should pass the variables set1Correct, set2Correct and set3Correct by reference from the very beginning of the program in main, that is in function printReport.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
AAAKsu is offline Offline
7 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Initializing string with iterators
Next Thread in C++ Forum Timeline: g++ warning with -Wconversion flag





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


Follow us on Twitter


© 2011 DaniWeb® LLC