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

#include <iostream>
#include <string>
#include <math.h>
using namespace std;

double DisplayArrays(string studentName, int score)
{
	studentName[20];
	score[20]
	cout << "Name			" << "Score" << endl;
	cout << "=============================" << endl;
	for (int n = 0; n < 20; n++)
	{
		if (studentName[n] == "stop"|| score[n] == -1)
		{
			break;
		}
		cout << studentName[n] << "                      " << score[n] << endl;
		
	}
}

int main()
{  
	const int ARRAYTIME = 20;
	string studentNames[ARRAYTIME];
	int scores[ARRAYTIME];

	cout << "To end the loop enter 'stop' as student name and '-1' as score" << endl;
	
	
	
	for (int i = 0; i < ARRAYTIME ; i++)
	{
		cout << "Enter student name: ";
		cin >> studentNames[i];
		cout << "Enter score: ";
		cin >> scores[i];
		cout << endl;

		if (studentNames[i] == "stop" || scores[1] == -1)
		{
			break;
		}
		
	}
	cout << endl;
	DisplayArrays(studentNames[], scores[]);
	/***cout << "Name			" << "Score" << endl;
	cout << "=============================" << endl;
	for (int n = 0; n < ARRAYTIME; n++)
	{
		if (studentNames[n] == "stop"|| scores[n] == -1)
		{
			break;
		}
		cout << studentNames[n] << "                      " << scores[n] << endl;
		
	}
	*********/

	return 0;
}

Thanks for you help

Recommended Answers

All 2 Replies

Your []'s are in the wrong spot. Say

DisplayArrays( studentNames, scores );

and

double DisplayArrays( string studentName[], int score[] )

Hope this helps.

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

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.