Hello, as you can tell from my username I'm completely new at C++ and have no idea how to write a function that won't chuck up any errors!
What I'm trying to do is to compare an inputted student's ID number to ones already stored in my current array and this way the user can sort of search for a student in the record. Here's my current code:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void clrscr()
{
system("cls");
}
void pause()
{
system("echo.");system("echo.");system("pause");
}
void displayMenu(string msg)
{
clrscr();
cout << msg << "\n\nMAIN MENU\n"
"\n0. Exit"
"\n1. Search for a student"
"\n2. List students enrolled in a course"
"\n3. List students eligible to graduate"
"\n4. List all students"
"\n5. Update a student record"
"\n6. Add a student record"
"\n7. Delete a student record"
"\n\nYour choice is ->";
};
const int MAXRECORD = 500;
struct stdRecord
{
string studentID;
string studentName;
int courseCode;
int creditPoint;
};
bool stdIDComp(string, string); //function prototype for cas1 function
int main()
{
const int NUMRECS = 3;
const int IDNUM = 8;
int option, i, j, total = 0;
string word, stdIDInput, msg="Please type in the number of the corresponding \ntask you wish to perform then press enter.";
stdRecord stdRec[NUMRECS]={{"15000000","Joshua Andrew Smith", 3506, 240},
{"16666666", "Jack Williams", 3506, 180},
{"17000010", "Lily Jones", 3639, 110}};
do
{
displayMenu(msg);
cin.clear();
if(!(cin >> option))
{
if(!cin.eof())
{
cin.clear();
cin >> word;
}
continue;
}
switch(option)
{
case 1:
cout << "Please type in the student ID number\nof the student you want to search,\nthen press enter.\n";
cin >> stdIDInput;
cout << "\nStudent ID Student Name Course Code Credit Points\n\n";
cout << setiosflags(ios::left);
if (stdIDComp(stdIDInput, stdRec[j].studentID)) //****HERE, why doesn't this work????
{
for (j = 0; j < NUMRECS ; j++)
{
cout << setw(12) << stdRec[j].studentID
<< setw(21) << stdRec[j].studentName
<< setw(13) << stdRec[j].courseCode
<< setw(5) << stdRec[j].creditPoint << endl;
}
}
else
{
cout << "Not Found.\n";
}
pause ();
break;
case 2://to be filled
case 3://to be filled
case 4://to be filled
cout << "\nStudent ID Student Name Course Code Credit Points\n\n";
cout << setiosflags(ios::left);
for (i = 0; i < NUMRECS; i++)
cout << setw(12) << stdRec[i].studentID
<< setw(21) << stdRec[i].studentName
<< setw(13) << stdRec[i].courseCode
<< setw(5) << stdRec[i].creditPoint << endl;
pause ();
break;
case 5://to be filled
case 6://to be filled
case 7://to be filled
default:
cout << "No options from choices one to seven have been typed.";
pause ();
break;
}
} while (option!=0);
return 0;
}
bool stdIDComp(string stdIDIn, string student) //the function for case1
{
if (stdIDIn.compare(student) == 0) return true;
return false;
}
The menu display and search works fine, but it crashes just before it displays its results from the search!!! I don't know why! Any direction would be MUCH APPRECIATED! Please and thank-you!
C++newbie chick.