Trouble displaying array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 30
Reputation: cl3m0ns is an unknown quantity at this point 
Solved Threads: 0
cl3m0ns's Avatar
cl3m0ns cl3m0ns is offline Offline
Light Poster

Trouble displaying array

 
0
  #1
Oct 26th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,861
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Trouble displaying array

 
0
  #2
Oct 26th, 2007
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.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 30
Reputation: cl3m0ns is an unknown quantity at this point 
Solved Threads: 0
cl3m0ns's Avatar
cl3m0ns cl3m0ns is offline Offline
Light Poster

Re: Trouble displaying array

 
0
  #3
Oct 26th, 2007
i thought i did sorry
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,861
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Trouble displaying array

 
0
  #4
Oct 26th, 2007
I realised you inlinecode-ed it. I edited my last post.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Trouble displaying array

 
0
  #5
Oct 26th, 2007
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".
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 30
Reputation: cl3m0ns is an unknown quantity at this point 
Solved Threads: 0
cl3m0ns's Avatar
cl3m0ns cl3m0ns is offline Offline
Light Poster

Re: Trouble displaying array

 
0
  #6
Oct 26th, 2007
thanks alot guys that worked perfectly!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC