function issues

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

Join Date: Oct 2007
Posts: 30
Reputation: cl3m0ns is an unknown quantity at this point 
Solved Threads: 0
cl3m0ns's Avatar
cl3m0ns cl3m0ns is offline Offline
Light Poster

function issues

 
0
  #1
Oct 26th, 2007
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

  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: function issues

 
0
  #2
Oct 26th, 2007
Your []'s are in the wrong spot. Say
  1. DisplayArrays( studentNames, scores );
and
  1. double DisplayArrays( string studentName[], int score[] )
Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: function issues

 
0
  #3
Oct 26th, 2007
Originally Posted by cl3m0ns View 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
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...
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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