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

#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;
}

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

Member Avatar for iamthwee

Do you even know what you are doing or are you just guessing honey?

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.

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.

Member Avatar for iamthwee

>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.

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.

you should put your line number 39 in a for loop

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.