943,718 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 400
  • C++ RSS
Sep 15th, 2009
0

Need help with functions

Expand Post »
I need help with my homework assignment for school. I was able to get everything to work fine when I just put everything in int main() without using functions but when I tried to separate everything into functions the program is not running correctly and although I don't get an error compiling when I try to run the program it keeps exiting out. Can someone please help? Thanks!

C++ Syntax (Toggle Plain Text)
  1. // This program calls 3 functions: the 1st one has the user enter an ID and 3 test scores which range from 0-50 and checks to see
  2. // if done correctly, if not then it asks the user again, the 2nd one takes the 3 test scores from the 1st function and then finds
  3. // the average, and the 3rd one accepts the average and id and prints the id followed by a line of stars based on the
  4. // whole # part of the average. The user can also repeat this as many times as they want.
  5.  
  6. #include "stdafx.h"
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. void getIdScores(int &id, int &score1, int &score2, int &score3);
  11. int calcAverage(int score1, int score2, int score3);
  12. void printStars(int avg, int id);
  13.  
  14. int main()
  15. {
  16. // declare variables
  17. int id, score1, score2, score3;
  18.  
  19. // call function getIdScores
  20. getIdScores(id, score1, score2, score3);
  21.  
  22. // call function calcAverage
  23. int calcAverage(int score1, int score2, int score3);
  24.  
  25. // call function printStars
  26. void printStars(int avg, int id);
  27.  
  28. return 0;
  29. }
  30.  
  31. void getIdScores(int &id, int &score1, int &score2, int &score3)
  32. {
  33. // loop if test scores are invalid
  34. while (true)
  35. {
  36. // get id #
  37. cout << "Please enter your ID#: ";
  38. cin >> id;
  39.  
  40. // get 3 test scores
  41. cout << "Please enter 3 test scores (separated by spaces & b/w 0-50): ";
  42. cin >> score1 >> score2 >> score3;
  43.  
  44. // test if it is in the right format
  45. if ((score1 > 0 && score1 < 50) ||(score2 > 0 && score2 < 50) || (score3 > 0 && score3 < 50)) break;
  46. cout << "Invalid test scores. Please try again." << endl;
  47. } // while
  48. } // getIdScores
  49.  
  50. int calcAverage(int score1, int score2, int score3)
  51. {
  52. // find average
  53. int avg = (score1 + score2 + score3) / 3;
  54.  
  55. return avg;
  56. } // average
  57.  
  58. void printStars(int avg, int id)
  59. {
  60. // print id to screen
  61. cout << "The id is " << id;
  62.  
  63. // loop to print out the number of stars of the average
  64. for (int i=0; i<avg; i++)
  65. {
  66. cout << "*";
  67. } // for
  68. } // printStars
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mz_rigel is offline Offline
6 posts
since Sep 2009
Sep 15th, 2009
1

Re: Need help with functions

You might want to try compiling and working your way through the errors. These two lines will definitely cause issues.
c++ Syntax (Toggle Plain Text)
  1. // call function calcAverage
  2. int calcAverage(int score1, int score2, int score3);
  3.  
  4. // call function printStars
  5. void printStars(int avg, int id);
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Sep 15th, 2009
0

Re: Need help with functions

When calling a function, you don't have to but the type it returns before it.

For example, change line 23 to:

C++ Syntax (Toggle Plain Text)
  1. calcAverage(score1, score2, score3);

Hope this helps.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Sep 15th, 2009
0

Re: Need help with functions

As ShawnCplus has mentioned, the calls to the functions are incorrect.
You aren't returning your average value from the calcAverage function into a variable so you won't be able to pass it into your printing function.

When you call the calcAverage function it should return into a variable:
C++ Syntax (Toggle Plain Text)
  1. int avg = calcAverage(int score1, int score2, int score3);
  2. //return value from this function is copied into the avg variable..

This will allow you to pass the avg variable into your print function. You might think about drafting up a simplified version and reading up on returning variables from functions.
Hope that helps..
Reputation Points: 46
Solved Threads: 3
Light Poster
KonkaNok is offline Offline
38 posts
since May 2009

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: curl and xerces-c
Next Thread in C++ Forum Timeline: Void functions for c++





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


Follow us on Twitter


© 2011 DaniWeb® LLC