| | |
Trouble displaying array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
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
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.
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.
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 264
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".
![]() |
Similar Threads
- displaying two dimensional array in columns help (C++)
- Having trouble displaying an Arrays. Please help. (C)
- unserialize an array! spitting out jibberish (PHP)
- LC3 displaying numbers in registers (Assembly)
- Problem on array based list (C)
Other Threads in the C++ Forum
- Previous Thread: Strings, parrallel arrays, storing data c++
- Next Thread: Confusing Question!
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






