954,136 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

function issues

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

cl3m0ns
Light Poster
30 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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

DisplayArrays( studentNames, scores );

and

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

Hope this helps.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

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 forfinally using CODE tags. Now we need better titles... :icon_wink:

WaltP
Posting Sage w/ dash of thyme
Moderator
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You