1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
//Function for opening the file
void openfile(ifstream &infile, ofstream &outfile, char filename[],int size)
{
//File name is charater variage. A certain number of char can be entered in
filename[size];
cout<<"Enter in a file name: ";
cin>>filename;
infile.open(filename);
//Enter in a file name for output
cout<<"\n\nEnter File name again: ";
cin>>filename;
outfile.open(filename);
}
//Function for closing the files
void closefile(ifstream &infile, ofstream &outfile)
{
infile.close();
outfile.close();
}
//Function for reading the student id
void idandanswerreader(char id[],int size, ofstream &out)
{
int index=0;
for(index=0;index<size;index++)
{
out<<id[index];
if (id[index]=='\0')
break;
}
}
void answerchecker(char answer[],int size, ofstream &out)
{
int index=0;
char youanswered;
int correctpoints=0;
int wrongpoints=0;
int onepoints=0;
for (index=0;index<size;index++)
{
youanswered=answer[index];
youanswered=(char)toupper(youanswered);
switch (youanswered)
{
case 'T':
correctpoints=correctpoints+2;
break;
case 'F':
wrongpoints=wrongpoints+1;
break;
default:
onepoints=onepoints+0;
}
}
out<<correctpoints+wrongpoints;
}
//Function: Parrel array to to store student ids and test answers.
void ReadStudentid_TestAnswers(ifstream &infile, ofstream& outfile,char studentid[], int idsize,char answers[],int answersize)
{
studentid[idsize];
answers[answersize];
while (infile>>studentid>>answers)
{
//Read data into infile
infile>>studentid;
infile>>answers;
//output data into outfile
outfile<<"\nID: ";
outfile<<studentid;
//output the answers on the file
outfile<<" Answers: ";
idandanswerreader(answers,10,outfile);
//Declare the totalpoints
outfile<< "Points: ";
answerchecker(answers,10,outfile);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//Declare varibalbe to open files. Decalre char array to store the text name of file
ifstream in;
ofstream out;
char filename[15];
//Function: To pen the files and make a file txt
openfile(in, out, filename, 15);
cout<<"\n\nProcessing data........";
//Call function to read stdent id and answers
char studentidnumber[10];
char testanswers[10];
ReadStudentid_TestAnswers(in,out,studentidnumber,10,testanswers,10);
//Function to close the files
closefile(in, out);
_getch();
return 0;
}
|