| | |
displaying arrays
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
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
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <math.h> using namespace std; void DisplayArrays(string a, int b) { cout << "Name " << "Score" << endl; cout << "===================" << "=====" << endl; cout << a << b; } int main() { const int ATIME = 20; string studentNames[ATIME]; int scores[ATIME]; cout << "When you have entered all students names and scores" << endl; cout << "type 'stop' to display the names and scores in a list view" << endl; for (int i = 0; i < ATIME ; i++) { cout << "Enter student name: "<< endl; cin >> studentNames[i]; cout << endl; if (studentNames[i] == "stop") { break; } cout << "Enter score: " << endl; cin >> scores[i]; cout << endl; } cout << endl; DisplayArrays(studentNames[ATIME], scores[ATIME]); return 0; }
thanks alot
Last edited by Ancient Dragon; Oct 27th, 2007 at 7:50 am. Reason: add code tags
•
•
Join Date: Jul 2005
Posts: 1,681
Reputation:
Solved Threads: 264
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.
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.
>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.
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*
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.
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.
you should put your line number 39 in a for loop
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...
C++ Syntax (Toggle Plain Text)
for(int j=0; j<i; j++) 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...
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Whats the difference between...plz respond in the next 5-10min
- Next Thread: Calculating the cardinality of an array
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






