displaying arrays

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

displaying arrays

 
0
  #1
Oct 26th, 2007
I have a program that takes two separate arrays one of all the peoples names and the other of their test scores. I am not sure how to display the scores in a table view. here is my code so far
  1. #include <iostream>
  2. #include <string>
  3. #include <math.h>
  4. using namespace std;
  5.  
  6. void DisplayArrays(string a, int b)
  7. {
  8. cout << "Name " << "Score" << endl;
  9. cout << "===================" << "=====" << endl;
  10. cout << a << b;
  11. }
  12.  
  13. int main()
  14. {
  15. const int ATIME = 20;
  16. string studentNames[ATIME];
  17. int scores[ATIME];
  18.  
  19. cout << "When you have entered all students names and scores" << endl;
  20. cout << "type 'stop' to display the names and scores in a list view" << endl;
  21.  
  22.  
  23. for (int i = 0; i < ATIME ; i++)
  24. {
  25. cout << "Enter student name: "<< endl;
  26. cin >> studentNames[i];
  27. cout << endl;
  28.  
  29. if (studentNames[i] == "stop")
  30. {
  31. break;
  32. }
  33. cout << "Enter score: " << endl;
  34. cin >> scores[i];
  35. cout << endl;
  36.  
  37. }
  38. cout << endl;
  39. DisplayArrays(studentNames[ATIME], scores[ATIME]);
  40.  
  41. return 0;
  42. }
my problem is that i get an unexpected error in the DisplayArrays function. Can anyone tell my why i get his error and how to get rid of the error

thanks alot
Last edited by Ancient Dragon; Oct 27th, 2007 at 7:50 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: displaying arrays

 
0
  #2
Oct 26th, 2007
Do you even know what you are doing or are you just guessing honey?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
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

Re: displaying arrays

 
0
  #3
Oct 26th, 2007
I have an idea but I'm pretty new to the whole array thing so in that sense i'm clueless.

I know what I want to do just not what exactly to type to get it to do it.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,681
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 264
Lerner Lerner is offline Offline
Posting Virtuoso

Re: displaying arrays

 
0
  #4
Oct 26th, 2007
Unexpected error of what type?

Of hand I'd be concerned that when name == "stop" there is no corresponding score so trying to display one may cause a problem.

Alternatively, using ATIME as an index as in below:

DisplayArrays(studentNames[ATIME], scores[ATIME]);

is an error since the last valid index is ATIME - 1.
You probably want that statement within the for loop and use i as the index sinc DisplayArrays takes only a single string and a single int, not an array of string and and array of int. You could alter the parameters to DisplayArrays() to accept the whole arrays at once and then use a loop within DisplayArrays() to display all the data in the arrays if you wish, in which case, leaving the function call outside the for loop is appropriate.

But without knowing what the error is I may not even be barking up the right tree.

NB---please use code tags when posting code to this board.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: displaying arrays

 
0
  #5
Oct 26th, 2007
>I have an idea but I'm pretty new to the whole array thing so in that sense i'm clueless.
I know what I want to do just not what exactly to type to get it to do it.


In that case begin by reading a tutorial on arrays, and strings. That way, it will save us repeating ourselves.
Last edited by iamthwee; Oct 26th, 2007 at 4:49 pm. Reason: Added OP's quote cos lerner posted seconds before me!
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,412
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: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: displaying arrays

 
0
  #6
Oct 27th, 2007
delete line 3 because your program doesn't need math.h

and learn how to use code tags so that I don't have to add them for you.
Last edited by Ancient Dragon; Oct 27th, 2007 at 7:54 am.
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: Jul 2007
Posts: 54
Reputation: tracethepath is an unknown quantity at this point 
Solved Threads: 4
tracethepath's Avatar
tracethepath tracethepath is offline Offline
Junior Poster in Training

Re: displaying arrays

 
0
  #7
Oct 27th, 2007
you should put your line number 39 in a for loop

  1. for(int j=0; j<i; j++)
  2. DisplayArrays(studentNames[j], scores[j]);

and remove line 8 and 9 f4m displayarrays function and add in main b4 for loop or it will be displayed everytime the function iz called...
With Regards...
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



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

©2003 - 2009 DaniWeb® LLC