Need Help on Project

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

Join Date: Nov 2009
Posts: 42
Reputation: NitaB is an unknown quantity at this point 
Solved Threads: 0
NitaB NitaB is offline Offline
Light Poster

Need Help on Project

 
0
  #1
Nov 7th, 2009
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)

  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 5
Reputation: AAAKsu is an unknown quantity at this point 
Solved Threads: 2
AAAKsu's Avatar
AAAKsu AAAKsu is offline Offline
Newbie Poster
 
0
  #2
Nov 7th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,616
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning
 
-7
  #3
Nov 7th, 2009
line 61 in the code you posted: That function displays random/wrong values because it is using uninitialized variables.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 480
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 57
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Pro in Training
 
0
  #4
Nov 7th, 2009
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)
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 42
Reputation: NitaB is an unknown quantity at this point 
Solved Threads: 0
NitaB NitaB is offline Offline
Light Poster
 
0
  #5
Nov 7th, 2009
@ 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:
  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!
  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. }


Originally Posted by Clinton Portis View Post
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)
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 480
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 57
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Pro in Training
 
0
  #6
Nov 8th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 42
Reputation: NitaB is an unknown quantity at this point 
Solved Threads: 0
NitaB NitaB is offline Offline
Light Poster
 
0
  #7
Nov 8th, 2009
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
  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...
  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.
  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!
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 5
Reputation: AAAKsu is an unknown quantity at this point 
Solved Threads: 2
AAAKsu's Avatar
AAAKsu AAAKsu is offline Offline
Newbie Poster
 
0
  #8
Nov 9th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC