I need a shove in the right direction with an issue I am having when comparing data in an array with data input by a user. Here is the entire homework problem:
Write a program that does the following:
Opens a data file that contains:
o On the very first line, a single integer that indicates how many student records
there are in the file
o each line after that contains a student's name, and 3 test scores (separated by
spaces)
Reads the value on the first line, and creates 3 dynamic arrays of exactly that many
elements (to store test scores) and 1 dynamic array to store the student's name
Input from the file student names and three scores each to fill all 4 arrays
Provide an interactive menu that will then allow the user to:
o display scores for a single test
- list all student names and their score for the chosen test
o display all scores and an average for a single student
- You will need to search the name array for a student first, then use the
index
- write function that takes the name array and and the student's name to look
for as parameters, and returns the index if found, -1 if not
o display class averages for the three tests, along with the high and low scores.
Here is my code:
/*********************************************/
/* Program: Test_scores.cpp */
/* Author: Doug Mainwaring */
/* */
/* Description: */
/* Opens file of test scores and */
/* produces output based on user */
/* selections */
/* Comments: */
/* */
/*********************************************/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int search_name(const string find_name[], const string in_name, int size1);
// Function to search for an inputted student name in the name[] array
int main()
{
int size, *test1, *test2, *test3, score1, score2, score3, choice2;
char choice, dummy;
string *name, student, find_student;
ifstream infile;
ofstream outfile;
cout << "hello\n";
infile.open("scores.txt");
if (infile.fail())
{
cout << "Cannot open data file\nProgram halted\n\n";
exit(1);
}
infile >> size;
test1 = new int[size];
test2 = new int[size];
test3 = new int[size];
name = new string[size];
for (int i = 0; i < size; i++)
{
infile >> student >> score1 >> score2 >> score3;
name[i] = student;
test1[i] = score1;
test2[i] = score2;
test3[i] = score3;
}
do
{
system("cls");
cout << "Welcome to Test Score 2000\n\n\n"
<< "Please choose from the following:\n\n"
<< "A. Display all student scores for a single test\n"
<< "B. Display all scores and an average for a single student\n"
<< "C. Display class average for each test along with the highest and lowest score\n"
<< "Q. End Program\n\n"
<< "<=SELECTION=> ";
cin >> choice;
choice = toupper(choice);
switch (choice)
{
case 'A':
{
do
{
system("cls");
cout << "Please input which test scores you would like to view - 1, 2, or 3.\n"
<< "Input 0 to return to main menu ==> ";
cin >> choice2;
switch (choice2)
{
case 1:
{
cout << "\nTest " << choice2 << endl
<< "========================================\n";
for (int i = 0; i < size; i++)
{
cout << name[i] << "\t\t" << test1[i] << endl << endl;
}
cout << "\n\nHit Enter to continue....";
cin.ignore(100,'\n');
cin.get(dummy);
break;
}
case 2:
{
cout << "\nTest " << choice2 << endl
<< "========================================\n";
for (int i = 0; i < size; i++)
{
cout << name[i] << "\t\t" << test2[i] << endl << endl;
}
cout << "\n\nHit Enter to continue....";
cin.ignore(100,'\n');
cin.get(dummy);
break;
}
case 3:
{
cout << "\nTest " << choice2 << endl
<< "========================================\n";
for (int i = 0; i < size; i++)
{
cout << name[i] << "\t\t" << test3[i] << endl << endl;
}
cout << "\n\nHit Enter to continue....";
cin.ignore(100,'\n');
cin.get(dummy);
break;
}
case 0:
break;
default:
{
cout << "\nIncorrect input, please try again!\n";
cout << "\n\nHit Enter to continue....";
cin.ignore(100,'\n');
cin.get(dummy);
}
}
} while (choice2 != 0);
}
break;
case 'B':
{
do
{
system("cls");
int i;
cout << "Please choose from the following students or input QQQ to return:\n\n";
for (int num = 0; num < size; num++)
{
cout << name[num] << endl;
}
cout << "\n==> ";
cin >> find_student;
i = search_name(name, find_student, size);
if (i < 0)
cout << "Name not found, please try again!\n";
else
{
cout << "\nStudent\tTest 1\tTest 2\tTest 3\tAverage\n"
<< "======================================="
<< endl << name[i] << "\t" << test1[i] << "\t" << test2[i]
<< "\t" << test3[i] << "\t" << ((test1[i] + test2[i] + test3[i]) / 3) << endl << endl;
}
cout << "\n\nHit Enter to continue....";
cin.ignore(100,'\n');
cin.get(dummy);
} while (find_student != "QQQ" || find_student != "qqq");
}
break;
case 'C':
{
system("cls");
int average1 = 0, average2 = 0, average3 = 0, hinum1, lownum1, hinum2, lownum2, hinum3, lownum3, max1 = 0, max2 = 0, max3 = 0,
min1 = 100, min2 = 100, min3 = 100;
for (int i = 0; i < size; i++)
{
average1 += test1[i];
average2 += test2[i];
average3 += test3[i];
hinum1 = test1[i];
hinum2 = test2[i];
hinum3 = test3[i];
lownum1 = test1[i];
lownum2 = test2[i];
lownum3 = test3[i];
if (hinum1 > max1)
max1 = hinum1;
if (hinum2 > max2)
max2 =hinum2;
if (hinum3 > max3)
max3 = hinum3;
if (lownum1 < min1)
min1 = lownum1;
if (lownum2 < min2)
min2 = lownum2;
if (lownum3 < min3)
min3 = lownum3;
}
cout << "\nTest\tAverage\t\tHigh Score\tLow Score\n"
<< "=================================================\n"
<< "1\t" << (average1 / size) << "\t\t" << max1 << "\t\t" << min1 << endl
<< "2\t" << (average2 / size) << "\t\t" << max2 << "\t\t" << min2 << endl
<< "3\t" << (average3 / size) << "\t\t" << max3 << "\t\t" << min3 << endl << endl;
}
cout << "\n\nHit Enter to continue....";
cin.ignore(100,'\n');
cin.get(dummy);
break;
}
}while (choice != 'Q');
return 0;
}
int search_name(const string find_name[], const string in_name, int size1)
{
int index = 0, not_found = -1;
while (in_name != find_name[index] && index <= size1)
index++;
if (in_name != find_name[index] && index > size1)
return not_found;
else
return index;
}
When executing this code and selecting option B to display a single student, if a user inputs a name that matches, there is no problem. However, if a user inputs an incorrect name, instead of returning a -1 and reverting to the "Name not found" statement, the program crashes and refers to a "bad pointer" and "cannot evaluate expression." What am I missing here? If the input matches are returns the proper int from the function, where is the problem when attempting to return the -1 int?
Thank you in advance.
Lattimer

Ask A New Question
Reply





MultiQuote





|