944,048 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1261
  • C++ RSS
Oct 26th, 2007
0

function issues

Expand Post »
I have this program it allows the users to enter a name and a score it saves all the names to one array and all the scores to another. The program then displays all the names and scores in a list.

I want to have the code for displaying the the list of names and scores be in a separate function. The code works in the int main() function but as soon as i put it in a separate function the code no longer works

here is my code i would just like to know what i need to do to make the list work in the displayfunctiion() instead of the int main () function

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <math.h>
  4. using namespace std;
  5.  
  6. double DisplayArrays(string studentName, int score)
  7. {
  8. studentName[20];
  9. score[20]
  10. cout << "Name " << "Score" << endl;
  11. cout << "=============================" << endl;
  12. for (int n = 0; n < 20; n++)
  13. {
  14. if (studentName[n] == "stop"|| score[n] == -1)
  15. {
  16. break;
  17. }
  18. cout << studentName[n] << " " << score[n] << endl;
  19.  
  20. }
  21. }
  22.  
  23. int main()
  24. {
  25. const int ARRAYTIME = 20;
  26. string studentNames[ARRAYTIME];
  27. int scores[ARRAYTIME];
  28.  
  29. cout << "To end the loop enter 'stop' as student name and '-1' as score" << endl;
  30.  
  31.  
  32.  
  33. for (int i = 0; i < ARRAYTIME ; i++)
  34. {
  35. cout << "Enter student name: ";
  36. cin >> studentNames[i];
  37. cout << "Enter score: ";
  38. cin >> scores[i];
  39. cout << endl;
  40.  
  41. if (studentNames[i] == "stop" || scores[1] == -1)
  42. {
  43. break;
  44. }
  45.  
  46. }
  47. cout << endl;
  48. DisplayArrays(studentNames[], scores[]);
  49. /***cout << "Name " << "Score" << endl;
  50. cout << "=============================" << endl;
  51. for (int n = 0; n < ARRAYTIME; n++)
  52. {
  53. if (studentNames[n] == "stop"|| scores[n] == -1)
  54. {
  55. break;
  56. }
  57. cout << studentNames[n] << " " << scores[n] << endl;
  58.  
  59. }
  60. *********/
  61.  
  62. return 0;
  63. }


Thanks for you help
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
cl3m0ns is offline Offline
30 posts
since Oct 2007
Oct 26th, 2007
0

Re: function issues

Your []'s are in the wrong spot. Say
C++ Syntax (Toggle Plain Text)
  1. DisplayArrays( studentNames, scores );
and
C++ Syntax (Toggle Plain Text)
  1. double DisplayArrays( string studentName[], int score[] )
Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Oct 26th, 2007
0

Re: function issues

Click to Expand / Collapse  Quote originally posted by cl3m0ns ...
I have this program it allows the users to enter a name and a score it saves all the names to one array and all the scores to another. The program then displays all the names and scores in a list.

I want to have the code for displaying the the list of names and scores be in a separate function. The code works in the int main() function but as soon as i put it in a separate function the code no longer works
Why? Does it print one name only? Does it print junk? Does the program crash? Does the program cause nuclear detonation taking out Greenland?

The more vague you are, the more useless help is.

Thank you for finally using CODE tags. Now we need better titles...
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006

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: Need text file related help
Next Thread in C++ Forum Timeline: using GetPixel() outside client window





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


Follow us on Twitter


© 2011 DaniWeb® LLC