im trying to read information from a file, when i run my program all im getting is random garbage, my professor told me to use substrings, can anyone help me?
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
usingnamespace std;
#include "qhead.h"
constint MaxNameLen = 20;
struct WaitingList {
char ownerName[MaxNameLen];
char petName[MaxNameLen];
int emeCode;
};
int main ()
{
Queue waitingList;
string filename;
ifstream in;
in.open ("f:\\patient.txt");
if (!in) {
cerr << "Error: cannot open the file " << filename << endl;
system("pause");
exit (2);
}
char type;
string line;
while (in >> type)
{
if (type == 'H')
{
WaitingList* ptrstudent = (WaitingList*) waitingList.Dequeue ();
if (ptrstudent) {
cout << "Treated: " << ptrstudent->ownerName << ptrstudent->petName << endl;
delete ptrstudent;
}
}
elseif (type == 'A')
{
getline (in, line);
WaitingList* ptrnew = new (std::nothrow) WaitingList;
if (!ptrnew) {
cerr << "Error - out of memory\n";
system("pause");
exit (1);
}
// break line into substrings
waitingList.Enqueue (ptrnew);
cout << "Added patient: " << ptrnew->ownerName << ptrnew->petName << endl;
}
else
cerr << "Error: invalid transaction code encountered.\n"
<< "It was: " << type << endl;
}
in.close ();
system("pause");
return 0;
}
it should look like this
Added: Samuel Spade Fido
Added: John Jones Rover
Added: Betsy Ann Smithville Jenny
Treated: Samuel Spade Fido
Treated: John Jones Rover
Added: Lou Ann deVille Kitty
Treated: Lou Ann deVille Kitty
Added: Tom Smythe Fifi
Added: Marie Longfellow Jack
Treated: Marie Longfellow Jack
Treated: Betsy Ann Smithville Jenny
Treated: Tom Smythe Fifi