-
Problem with a function with arrays
You all were very helpful with my last program, I thought I could ask for help with this one. I am struggling with this one function. It compiles fine, but when I run the program to test this function, it doesn't end the do-while loop when I put -1 and thus, doesn't put the output. If someone could point me in the right direction, I would be appreciative. We are just learning about arrays, so I might have done the part with the arrays wrong, I'm not sure. According to the assignment, this is what the function is sapposed to do:
This function should print the prompting message and use a sentinel-controlled input loop to input the IDs and answers for the test. The input of the answers for a student should use a for loop nested inside the sentinel-controlled while loop. The in-parameter, numQuestions, tells how many times to go around this for loop, i.e. how many answers to input for each student. The function should store the answers for the first student in row 0 of the answer array, the answers for the second student in row 1 of the answer array, etc. The function should set the call-by-reference, out-parameter numStudents to the number of students who took the test, i.e. the number of times around the sentinel-controlled input loop.
Code:
int getIDsAnswers(int id[ ], int answer[ ][MAX_QUESTIONS], int numQuestions, int& numStudents)
{
int studentNum, answers;
numStudents=0;
cout << "\nEnter the IDs and test answers for all students in the class.\n";
cout << "Enter -1 for the ID to end the input:\n";
do
{
cin >> studentNum >> answers;
for (int x=0; x<MAX_STUDENTS; x++)
{
id[x]=studentNum;
for (int y=0; y<numQuestions; y++)
answer[x][y]=answers;
}
numStudents++;
}while (studentNum != -1);
cout << endl;
for (int z=0; z<MAX_STUDENTS; z++)
{
cout << id[z];
for (int w=0; w<numQuestions; w++)
cout << answer[z][w];
}
cout << endl;
return 0;
}
Last edited by krazykay; 07-30-2006 at 12:13 AM.
-
do a
cout << studentNum << endl;
before the while and see why it is failing to terminate when you type in a -1
-
I tried that it does nothing. I have to keep adding -1 until finally it seems to catch it. Here is the output and how I've changed the code. I'm including my whole code. If someone can help... PLEASE HELP!!!
Enter the number of questions on the test: 5
Enter the test key: 1 3 5 7 9
1 3 5 7 9
Enter the IDs and test answers for all students in the class.
Enter -1 for the ID to end the input:
11 1 3 5 7 9
12 3 1 5 7 9
13 3 3 5 7 9
14 1 1 5 7 9
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1
4
Press any key to continue . . .
Code:
#include <iostream>
using namespace std;
#include <stdlib.h>
const int MAX_QUESTIONS = 20;
const int MAX_STUDENTS = 50;
void getKey(int& numQuestions, int key[ ]);
int getIDsAnswers(int id[ ], int answer[ ][MAX_QUESTIONS], int numQuestions, int& numStudents);
void scoreTest(const int answer[ ][MAX_QUESTIONS], int numStudents, int numQuestions, const int key[ ], int score[ ]);
void selectionSort(int id[ ], int score[ ], int numStudents);
void computeHighLowAverage(int score[ ], int numStudents, int&high, int& low, double&avg);
int main ()
{
int key[MAX_QUESTIONS], id[MAX_STUDENTS], answer[MAX_STUDENTS][MAX_QUESTIONS];
int score[MAX_STUDENTS], numQuest, students, numStud;
getKey(numQuest, key);
students=getIDsAnswers(id, answer, numQuest, numStud);
system("PAUSE");
return 0;
}
void getKey(int& numQuestions, int key[ ])
{
int answer, input;
cout << "Enter the number of questions on the test: ";
cin >> numQuestions;
cout << "Enter the test key: ";
for (input=0; input < numQuestions; input++)
{
cin >> answer;
key[input]=answer;
}
cout << endl;
for (int x=0; x<input; x++)
cout << key[x] << " ";
cout << endl;
}
int getIDsAnswers(int id[ ], int answer[ ][MAX_QUESTIONS], int numQuestions, int& numStudents)
{
int studentNum, answers;
int x, y;
numStudents=0;
cout << "\nEnter the IDs and test answers for all students in the class.\n";
cout << "Enter -1 for the ID to end the input:\n";
do
{
numStudents++;
cin >> studentNum;
for (x=0; x<numStudents; x++)
{
id[x]=studentNum;
for (y=0; y<numQuestions; y++)
{
cin >> answers;
answer[x][y]=answers;
}
}
}while (studentNum != -1);
cout << endl;
for (int z=0; z<numStudents; z++)
{
cout << id[z];
for (int w=0; w<numQuestions; w++)
cout << " " << answer[z][w];
cout << endl;
}
cout << numStudents << endl;
return 0;
}
void scoreTest(const int answer[ ][MAX_QUESTIONS], int numStudents, int numQuestions, const int key[ ], int score[ ])
{
}
void selectionSort(int id[ ], int score[ ], int numStudents)
{
}
void computeHighLowAverage(int score[ ], int numStudents, int&high, int& low, double&avg)
{
}
Last edited by krazykay; 07-31-2006 at 01:55 PM.
-
The cout does not fix it, we are trying to catch the problem in the act with a print statement. I will take a look -- I did not have a compiler on the weekend.
-
AH ok what is happening is you enter the -1, then you proceed to loop and read in a bunch of answers (even though you really wanted to quit). Once you read in all the answers for the student that does not really exist, your -1 trips and you exit the function.
try this:
do
{
numStudents++;
cin >> studentNum;
if(studentNum != -1) //new code!
{ //add this
for (x=0; x<numStudents; x++)
{
id[x]=studentNum;
for (y=0; y<numQuestions; y++)
{
cin >> answers;
answer[x][y]=answers;
}
}
} //end for add this brace
}while (studentNum != -1);
or something along those lines.
Similar Threads
-
By sankar_de in forum C++
Replies: 5
Last Post: 08-10-2005, 07:35 AM
-
Replies: 0
Last Post: 07-20-2002, 10:12 PM
-
By Scott in forum VB Classic
Replies: 12
Last Post: 12-21-2001, 05:21 PM
-
By Brian Leung in forum VB Classic
Replies: 12
Last Post: 06-20-2000, 03:06 PM
-
By Dan in forum VB Classic
Replies: 0
Last Post: 03-17-2000, 06:14 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|