Need help with functions

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2009
Posts: 6
Reputation: mz_rigel is an unknown quantity at this point 
Solved Threads: 0
mz_rigel's Avatar
mz_rigel mz_rigel is offline Offline
Newbie Poster

Need help with functions

 
0
  #1
Sep 15th, 2009
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!

  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
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,402
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 225
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Need help with functions

 
1
  #2
Sep 15th, 2009
You might want to try compiling and working your way through the errors. These two lines will definitely cause issues.
  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);
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,425
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 115
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

Re: Need help with functions

 
0
  #3
Sep 15th, 2009
When calling a function, you don't have to but the type it returns before it.

For example, change line 23 to:

  1. calcAverage(score1, score2, score3);

Hope this helps.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 36
Reputation: KonkaNok is on a distinguished road 
Solved Threads: 3
KonkaNok's Avatar
KonkaNok KonkaNok is offline Offline
Light Poster

Re: Need help with functions

 
0
  #4
Sep 15th, 2009
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:
  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..
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC