944,208 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 666
  • C++ RSS
Oct 26th, 2007
0

Trouble displaying array

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
cl3m0ns is offline Offline
30 posts
since Oct 2007
Oct 26th, 2007
0

Re: Trouble displaying array

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.
Last edited by twomers; Oct 26th, 2007 at 5:23 pm.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Oct 26th, 2007
0

Re: Trouble displaying array

i thought i did sorry
Reputation Points: 10
Solved Threads: 0
Light Poster
cl3m0ns is offline Offline
30 posts
since Oct 2007
Oct 26th, 2007
0

Re: Trouble displaying array

I realised you inlinecode-ed it. I edited my last post.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Oct 26th, 2007
0

Re: Trouble displaying array

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".
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 26th, 2007
0

Re: Trouble displaying array

thanks alot guys that worked perfectly!!
Reputation Points: 10
Solved Threads: 0
Light Poster
cl3m0ns is offline Offline
30 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Strings, parrallel arrays, storing data c++
Next Thread in C++ Forum Timeline: Confusing Question!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC