0

when the console displays the "StudentAns" I get weird characters instead of the actual answer.

/////////////////////////////////////////////////////////////

this is the files
CorrectAnswers.txt
A
D
B
C
A
A
D
B
D
C
A
D
B
A
C
C
D
B
C
A

StudentAnswers.txt
A
D
B
C
A
A
D
B
D
C
A
D
B
A
C
C
D
B
C
A

/*
Omar Perez
Homework Version 6.1
ExamGrader.CPP
August 8, 2016
This Program is meant to grada an exam using two text files.
*/

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main()
{
	const int SIZE = 20;
	char CorrectAns[SIZE];
	char StudentAns[SIZE];
	int count = 0;
	ifstream CorrectAnswers,
			 StudentAnswers;

	CorrectAnswers.open( "E:\\Visual Studio 2013\\Projects\\CorrectAnswers.txt");
	if (!CorrectAnswers)
		cout << "Error opening CorrectAnswers.txt\n";
	else
		while (count < SIZE && CorrectAnswers >> CorrectAns[count])
			count ++;
	
	CorrectAnswers.close();


	StudentAnswers.open( "E:\\Visual Studio 2013\\Projects\\StudentAnswers.txt");
	if (!StudentAnswers)
		cout << "Error opening CorrectAnswers.txt\n";
	else
		while 
			(count < SIZE && StudentAnswers >> StudentAns[count])
			count ++;
	
	StudentAnswers.close();
	
	
	for (int count = 0; count < SIZE; count++)
		cout << "CA " << CorrectAns[count] << "\n";
	
	for (int count = 0; count < SIZE; count++)
		cout << "SA " << StudentAns[count] << "\n";



	return 0;
}
2
Contributors
1
Reply
14
Views
1 Year
Discussion Span
Last Post by AssertNull
0

I'm going to chime in with a bug-detecting technique rather than point out the actual bug. One, initialize your arrays with some printable value that is not in either file, like 'X' (as opposed to A,B,C,D,F). You want to get a printout of printable characters. It's tough to debug when you get random non-printable characters in your printout. If your printout shows 'X' anywhere, it is likely because this initialization was not overwritten. Two, stick some debugging statements in your loops. You want to know how many times you are going through each loop, the loop counter variable, and the value read in. Add some cout statements to display this content. A quick way to do that is to add some #if statements, as below. Change the 0 in line 10 to 1 when debugging. Change it back to 0 when not debugging. When you turn the program in, delete those #if DEBUG_ON statements and the code within.

/*
Omar Perez
Homework Version 6.1
ExamGrader.CPP
August 8, 2016
This Program is meant to grada an exam using two text files.
*/

#ifndef DEBUG_ON
#define DEBUG_ON 0
#endif

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    const int SIZE = 20;
    char CorrectAns[SIZE];
    char StudentAns[SIZE];
    int count = 0;
    ifstream CorrectAnswers,
             StudentAnswers;

    #if DEBUG_ON
    for(int i = 0; i < SIZE; i++)
    {
        CorrectAns[i] = 'X';
        StudentAns[i] = 'Y';
    }
    #endif

    CorrectAnswers.open( "E:\\Visual Studio 2013\\Projects\\CorrectAnswers.txt");
    if (!CorrectAnswers)
        cout << "Error opening CorrectAnswers.txt\n";
    else
    {
        while (count < SIZE && CorrectAnswers >> CorrectAns[count])
        {
            #if DEBUG_ON
            cout << "CA count=" << count << " " << CorrectAns[count] << "\n";
            #endif
            count ++;
        }
    }

    CorrectAnswers.close();

    StudentAnswers.open( "E:\\Visual Studio 2013\\Projects\\StudentAnswers.txt");
    if (!StudentAnswers)
        cout << "Error opening CorrectAnswers.txt\n";
    else
    {
        while (count < SIZE && StudentAnswers >> StudentAns[count])
        {
            #if DEBUG_ON
            cout << "SA count=" << count << " " << StudentAns[count] << "\n";
            #endif
            count ++;
        }
    }

    StudentAnswers.close();

    for (int count = 0; count < SIZE; count++)
        cout << "CA " << CorrectAns[count] << "\n";

    for (int count = 0; count < SIZE; count++)
        cout << "SA " << StudentAns[count] << "\n";

    return 0;
}
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.