I have a program that takes two arrays. One is a persons name. the other is their test score and displays it. I have an if statement nested in the for loop used to fill the array that allows you to enter stop as a name and it will put a break in the loop so that if you have less thatn 20 students you can stop entering names and scores when you are done by simply typing stop.

My problem is when i type stop in the loop it ends the for loop and the next line in my code is to display the students names and scores back to you. it displays all the names correctly unitl it gets to the name stop. after stop it displays a bunch of random numbers. I don't want it to display these numbers i want it to only display up to the name stop

could someone please explain how i would stop it from doing this.

here is my code

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;

	for (int n = 0; n < ARRAYTIME; n++)
	{
		cout << studentNames[n] << endl;
		cout << scores[n] << endl;
	}

	return 0;

how do i get it to only display up to the name stop

I'm new to c++ so if this is a dumb question sorry. Thanks for your help

Dude, I told you to use code tags!

One option would be to read the value to a temp string. Test that for "stop". If it is stop assign the new element that value. Else break. Then outside of the loop test that string for "stop" and do what you want then.

i thought i did sorry

I realised you inlinecode-ed it. I edited my last post.

EIther test the name before you enter it into the name array and break out of the loop if it equals "stop" without entering "stop" into the array (which is what I think twomers is suggesting) or, alternatively, leave "stop" as a name in the array, but when you display the array check if the name equals "stop". If it does, break out of the loop before you would print "stop" or the junk in the int array that has the same index as "stop".

thanks alot guys that worked perfectly!!

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.