Program not outputting

I wrote a program to grade T or F test. It is running, but not outputting the information.
[code]
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>


using namespace std;

double grading(char*answer, char* stuResponse, double graded);
void info(ofstream&, string student, char *stuResponse, double total);

int main()
{

ifstream inFile("input.txt");
ofstream outFile("input.txt");

double points;
string stuID;
char *teachAnswer = new char[29];
char *stuAnswer = new char[29];
char ch;

inFile.get(teachAnswer, 29);

inFile >> stuID;
while (inFile)
{
points = 0;
inFile.get(ch);
inFile.get(stuAnswer, 29);
inFile.get(ch);
info(outFile, stuID, stuAnswer, grading(teachAnswer, stuAnswer, points));
inFile >> stuID;
}

inFile.close();
outFile.close();

system("pause");
return 0;
}

void info(ofstream& out, string student, char *stuResponse, double total)
{
double score = total / 40 * 100;
char grade;
if (total >= 90)
grade = 'A';
else if (total >= 80)
grade = 'B';
else if (total >= 70)
grade = 'C';
else if (total >= 60)
grade = 'D';
else
grade = 'F';



cout << student << " " << score << " " << grade << endl;

}

double grading(char *answer, char *stuResponse, double graded)
{
for (int i = 0; i < 20; i++)
{
if (stuResponse[i] == answer[i])
graded = graded + 2;
else if (stuResponse[i] != answer[i] && stuResponse[i] != ' ')
graded = graded - 1;
}
return graded;
}





text doc:

TFFTFFTTTTFFTFTFTFTT
ABC5403 TFTFTFTT TFTFTFFTTFT
ABC5404 TFTFFTTFFTFFFTTTFTFT
Last edited on
Edit: my bad I realized why it's not outputting. remove ofstream from your program. what it does is erase the file as soon as you open it in a stream. your program has nothing to read so it just skips over the loop.

The final program without ofstream should look like;
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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <windows.h>


using namespace std;

double grading(char*answer, char* stuResponse, double graded);
void info(string student, char *stuResponse, double total);

int main()
{

    ifstream inFile("input.txt");
    //ofstream outFile("input.txt");

    double points;
    string stuID;
    char *teachAnswer = new char[29];
    char *stuAnswer = new char[29];
    char ch;

    inFile.get(teachAnswer, 29);

    inFile >> stuID;
    while (!inFile.eof()) //or just "inFile" which ever you prefer
    {
        points = 0;
        inFile.get(ch);
        inFile.get(stuAnswer, 29);
        inFile.get(ch);
        info(stuID, stuAnswer, grading(teachAnswer, stuAnswer, points));
        inFile >> stuID;
    }

    inFile.close();

    system("pause");
    return 0;
}

void info(string student, char *stuResponse, double total)
{
    double score = total / 40 * 100;
    char grade;
    if (total >= 90)
        grade = 'A';
    else if (total >= 80)
        grade = 'B';
    else if (total >= 70)
        grade = 'C';
    else if (total >= 60)
        grade = 'D';
    else
        grade = 'F';



    cout << student << " " << score << " " << grade << endl;

}

double grading(char *answer, char *stuResponse, double graded)
{
    for (int i = 0; i < 20; i++)
    {
        if (stuResponse[i] == answer[i])
            graded = graded + 2;
        else if (stuResponse[i] != answer[i] && stuResponse[i] != ' ')
            graded = graded - 1;
    }
    return graded;
}
Last edited on
While that may in fact work (I didn't check), there was room for improvement. So I improved it.

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
#include <iostream>
#include <string>
#include <fstream>


void info ( const std::string student, const std::string stuResponse, int score );

int grading ( const std::string answer, const std::string stuResponse );


int main ( )
{
	std::ifstream inFile ( "input.txt" );
	std::string stuID;
	std::string teachAnswer;
	std::string stuAnswer;


	inFile >> teachAnswer;


	while ( inFile >> stuID ) // get sudent id
	{
		inFile.ignore ( ); // skip space

		getline ( inFile, stuAnswer ); // get student answer

		info ( stuID, stuAnswer, grading ( teachAnswer, stuAnswer ) );
	}
}

void info ( std::string student, std::string stuResponse, int score )
{
	score *= 5;


	std::cout << student << ' ' << score << "% ";


	if ( score >= 90 ) 
		std::cout << "A\n";
	else if ( score >= 80 ) 
		std::cout << "B\n";
	else if ( score >= 70 ) 
		std::cout << "C\n";
	else if ( score >= 60 ) 
		std::cout << "D\n";
	else
		std::cout << "F\n";
}

int grading ( std::string answer, std::string stuResponse )
{
	int graded = 0;

	for ( int i = 0; i < 20; ++i ) 
	{
		if ( stuResponse[i] == answer[i] ) 
			graded += 2; // get two points for correct answer
		else if ( stuResponse[i] != ' ' ) 
			--graded; // lose one point for wrong answer

		// lose nothing for no answer?
	}

	return graded;
}
Last edited on
Now that you mention it, the displayed results were wrong lol.

1
2
3
ABC5403 27.5 F
ABC5404 17.5 F
Press any key to continue . . .

Not sure what happened.

Very nice and clean improvement by the way.
Thanks I appreciate the help. I got it working now.
I just realized mine was off too. I needed to multiply the % by 100. I've fixed it in my previous post.
Topic archived. No new replies allowed.