"""The history teacher at your school needs help in grading a True/False test. The students' IDs and test answers are stored in a file. The first entry in the file contains answers to the test in the form:
TFFTFFTTTTFFTFTFTFTT
Every other entry in the file is the student ID, followed by a blank, followed by the student's responses. For example, the entry...
ABC54301 TFTFTFTT TFTFTFFTTFT
indicates that the student ID is ABC54301 and the answer to question 1 is True, the answer to question 2 is False, and so on. This student did not answer question 9. The exam has 20 questions, and the class has more than 150 students. Each correct answer is awarded two points, each wrong answer gets one point deducted, and no answer gets zero points. Write a program that processes the test data. The output should be the student's ID, followed by the answers, followed by the test score, followed by the test grade.
Assume the following grade scale: 90%-100%, A; 80%-89.99%, B; 70%-79.99%, C; 60%-69.99%, D; 0%-59.99%, F."""
The question above is what I am dealing with.
I have copy and pasted my coding below.
I have the entire coding written up, and I have my text document with the answer key & student answers as well.
I am using xCode to write this program.
Can someone please tell me how to properly apply text documents, so the program can read it? I have dragged & dropped the text document into the left-side bar of xCode, hoping that would fix the case. But, I do not understand why my code is not operating correctly.
Any pointers? I will be refreshing this page frequently.
Thank you for the help!!!
please use code tags, it is very hard to read without them :)
EDIT: when i work on projects that require in and out files, making functions really helps organize the program and helps you find mistakes. Try splitting the program into main, Readfile and WriteFile. Once you put the code into tags id be glad to help you.
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
usingnamespace std;
int main()
{
string id;
char answers [20];
char studentID [9];
char response;
char fileName [25];
int testScore;
cout << "\nThis program will take the teacher's correct test answers" << endl;
cout << "As well as the 150 students answers, and grade the tests.\n" << endl;
cout << "Enter the input file name: ";
cin >> fileName;
ifstream inFile;
inFile.open ("grades.txt");
ofstream outFile;
outFile.open ("output.txt");
cout << "\n ID Answers % Grade";
for (int i=0; i<20; i++)
inFile >> answers [i];
while ( ( inFile >> id) != NULL)
{
cout << "\n" << id << " ";
inFile.get(response);
testScore = 0;
for (int i = 0; i < 20; i++)
{
inFile.get (response);
cout << " " << response;
if (response == ' ')
testScore += 0;
elseif (response == answers [i])
testScore += 2;
else
testScore += -1;
}
cout << " " << testScore << " ";
double p = testScore * 2.5;
if (p >= 90)
cout << 'A';
elseif (p >=80)
cout << 'B';
elseif (p >=70)
cout << 'C';
elseif (p >=60)
cout << 'D';
else
cout << 'F';
}
inFile.close();
outFile.close();
return 0;
}
Sry for the noob mistake. ^_^;
Please help me fix this application and get it running!!!
I'm eager to learn, and have a few hours until it is needed.
I will be by my computer all night with coffee.
Thank you! ill check it out right before i go to bed for the night and see what i can help you with
EDIT: as for the program you use, i am not familiar with xcode. To make a file available to a program simply drag and drop it into the project folder where you saved it(Ex- Desktop -> Newfoler -> Prjt2). Are you having problems other than getting the file to be read?
Im heading off to sleep in 15 minutes, id try and get all of your questions out now before i head off so i can help as much as i can. Hopefully someone else will be around at 2:00
EDIT: Here are some common functions i use for reading files, maybe this could be of any importance to you:
Function reads in a 1000 character array from a 1000 digit file:
1 2 3 4 5 6 7 8 9 10
void InitArr(char Ar[])
{
std::ifstream TextObj("ArrDigits.txt");
for(int i = 0; i < MAX_BOUND; i++)
{
Ar[i] = TextObj.get(); //Grab character and put it into the array
}
TextObj.close(); //Close file
}
Ill attempt to write that program in a short amount of time, no telling how far i'll get.
I am capable of getting the file to read, I believe?
I tried testing it with the...
1 2 3 4
if(inFile.is_open)
cout << "The input file is reading fine."
system("pause");
exit(1);
The "cout" appeared, and I believe that means that my file is open and/or readable.
...I believe my main issue is regarding...
How do I get the program to take the information in the "grades.txt" document,
and output them on the "output.txt" and on my Compiler, when I Run/Test the code?
The farthest line, that appears in my Test-Run is the...
cout << "\n ID Answers % Grade";
After this line appears. That's as far as my code/program will take me.
I need the lines under the...
cout << "\n ID Answers % Grade";
to display all 150 of the student IDs, their answers, %correct, and the corresponding letter grade.
Is that clear enough? or no?
Just let me know! ^_^
Function reads in a 1000 character array from a 1000 digit file:
1
2
3
4
5
6
7
8
9
10
void InitArr(char Ar[])
{
std::ifstream TextObj("ArrDigits.txt");
for(int i = 0; i < MAX_BOUND; i++)
{
Ar[i] = TextObj.get(); //Grab character and put it into the array
}
TextObj.close(); //Close file
}
..sorry, but this is a little too advanced for me to understand. :(
Your while statement does not need the != NULL, leaving that out means the same exact thing. Let me see what else i can find
EDIT2: Change line 24 to inFile.open (fileName);. I ran the code and the program worked fine, make sure you named your text file right(capslock counts) and placed it in the correct folder. Goodluck on your next assignment, as im logging off for the night. Im sure someone else will be able to help you sometime tonight though.
cout is just an object, it can be replaced by the file object you created. so you would create temporary variables you would store the information from the file, then print those variables out like so:
1 2 3 4 5
OutFile << ID_copy << Grade_Copy;
//this outputs the first string it encounters(until a white space) into ID_copy and the int following the whitespace.
cout << ID_copy << " " << Grade_Copy
//you have now displayed the information from the file, you simply stored the information in temporary variables! Remember the file reads strings and char arrays until it finds a white space and stops, skipping the white space and then reads in the next string into the next variable! The only way around this is using get_line functions or reading one at a time(you probably dont need to know this), good luck!
After the... Student IDs | Answers | % | Grades
--------------------------------------------
...there is no more output.
I still need it to display (below the line) the student IDs, their Test Answers, % of Correct Answers, and the Letter Grade associated with the % given.
That last statement there will print a bunch of junk (or perhaps it could crash your program, which might've happened in your case), since neither studentID nor answers has been initialized at this point, so there might not be a null character at the end of the string, so the program might read past the end of the array (which is memory it's not supposed to be touching).
Basically, remove that line and you should be fine.
This is the output I get:
This program will take the teacher's correct test answers
As well as the 150 students answers, and grade the tests.
Enter the input file name: grades.txt
Student IDs | Answers | % | Grades
--------------------------------------------”\ÃwP(Áwÿÿÿÿ)NÃwBNÃw¤ŸÌoXÿ"
ABC54301 T F T F T F T T T F T F T F F T T F T 11 F
ABC59310 F T T F T F T T F T T F T T T F T F T T 16 F
ABC39583 F T T F T F T T F T F F F T F T T F T F 7 F
ABC38493 F T F T T F T F T F T T F T F F T T T 8 F
ABC10394 F T T F T F T F F T F T F T T F T T F T 4 F
ABC10006 F T F T F F F T F F T F T F T F F T F T 13 F
ABC10007 F T F T F F T F T F T T T T F F T T F T 10 F
ABC38558 F T F T T F T F F T F F F T F T F T F F 1 F
ABC10439 F T T T F T F T F T F T T F T F T F F T 16 F
ABC10010 F T T F T F F F F T F T T T F T F T F T -5 F
(and so on and so forth)