Wrong output being shown on screen.

I have written a code here that gets data from the input file from the user and returns and output on the output file.I have a character id that stores in up to ten characters of user id. I have char answer that reads an answer consisting of true and false. The isze of the array would be ten.My problem arrises here when on my input file I have this
helowro tfftfffff
fahmankha ttttttttt
sadafadfa ttttttttt


and I get this as my output file
ID: helowro		Answers: tfftfffff Points: 11
ID: fahmankha		Answers: ttttttttt Points: 18
ID: sadafadfa		Answers: ttttttttt Points: 18
ID: 		Answers:  Points: 16


basically, on the last column. I'm getting this random 16 points. There is suppose to be nothing there. Anyone please willing to help me out on this.





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
  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)
{
	while (infile)
	{
	//Read data into infile 
	studentid[idsize];
	answers[answersize];
	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);

}
}
Anyone willing to help of why I have 16 points showing up towards the end, when it's not suppose to be :)
You should change the function ReadStudentid_TestAnswers() like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void ReadStudentid_TestAnswers(ifstream &infile, ofstream& outfile,char studentid[], int idsize,char answers[],int answersize)
{
    studentid[idsize];
    answers[answersize];

    while (infile >> studentid >>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);
    }
}


Your previous version using while (infile) does not work because it is checking the status of the file before reading from it. But that isn't useful. What you really need is to check the status after reading from the file.

But I already explained that in a previous post:
http://www.cplusplus.com/forum/beginner/118053/#msg644200
Chervil. I have tried this too. When I do your way, I get the following results as my output.

this is my input
raymarkst tttttttt
pomengrat tttttttt


and that's the output I get from using this code written below.
ID: pomengrat		Answers: tttttttt Points: 16


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
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 
	studentid[idsize];
	answers[answersize];
	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);

}
}


Contrastly, when I just have while (infile) I have this as my input
raymarkst tttttttt
pomengrat tttttttt
and I get an output of this here

ID: raymarkst		Answers: tttttttt Points: 16
ID: pomengrat		Answers: tttttttt Points: 16
ID: 		Answers:  Points: 14
I don't understand why you didn't just do it the way I showed you.
Here it is in simplified form:
1
2
3
4
5
6
7
8
9
10
11
12
void ReadStudentid_TestAnswers(ifstream &infile, ofstream& outfile,char studentid[], int idsize,char answers[],int answersize)
{
    studentid[idsize];                      // define some storage for the variables
    answers[answersize];

    while (infile >> studentid >>answers)   // try to read the two values from the file
    {
        // we only enter the body of the loop when
        // both values were read successfully.
        // now do something with those two values.
    }
}


But this is the version you gave shown above (again, simplified):
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
void ReadStudentid_TestAnswers(ifstream &infile, ofstream& outfile,char studentid[], int idsize,char answers[],int answersize)
{
    studentid[idsize];                  // define some storage for the variables
    answers[answersize];
    
    while (infile>>studentid>>answers)  // try to read the two values from the file
    {
        // we only enter the body of the loop when
        // both values were read successfully.
        // now ignore  those two values.
        
        studentid[idsize];              // define some more storage which  will
        answers[answersize];            // hide our proper variables
        infile>>studentid;              // try to read the two more values from the file
        infile>>answers;                // but don't bother checking whether the operation was successful
        
         // By now we have discarded the valid data, 
         // and tried to read two more.
         // But we have no idea 
         // whether the file was read successfully or not.
         // and will go ahead and try to use those variables
         // even though they might be invalid
         
    }
}

Input:
helowro tfftfffff
fahmankha ttttttttt
sadafadfa ttttttttt

Output:

ID: helowro        Answers: Points: 11
ID: fahmankha        Answers: Points: 18
ID: sadafadfa        Answers: Points: 18

Complete, working program:
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
#include <iostream>
#include <fstream>

    using namespace std;

void ReadStudentid_TestAnswers(ifstream &infile, ofstream& outfile,char studentid[], int idsize,char answers[],int answersize);
void answerchecker(char answer[],int size, ofstream &out);


void idandanswerreader(char * answers,int n, ofstream & outfile)
{

}

int main()
{
    ifstream fin("input.txt");
    ofstream fout("output.txt");

    char studentid[100];
    char answers[100];

    ReadStudentid_TestAnswers(fin, fout, studentid, 20, answers, 20);

    return 0;
}

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)
    {
        //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);
    }
}

Chervil, unless i'm not seeing something your seeing, I'm copying the exact code you have asked me to do so. I'm still getting this as my output. I'm going to post my entire code in here hoping you can detect something. Once again, don't want to bother you too much, this is going to be my final try and then I guess I will work on another assignment.Also, I noticed you using a pointer in this last post of yours. I haven't studied pointers yet :(

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;
}
My input again was
raymarkst tttttttt
pomengrat tttttttt


output I got was
ID: pomengrat		Answers: tttttttt Points: 16
@ fahmankhan75 please look here at the full working program I posted:
http://www.cplusplus.com/forum/beginner/118265/#msg645391

and then look at line 57 function ReadStudentid_TestAnswers()
Notice it has 21 lines of code. Now look at your own versioin of what should be identical code, in this post: http://www.cplusplus.com/forum/beginner/118265/#msg645278
Notice it is 27 lines long.

Hmmm - one code has 21 lines, the other 27 - do you think maybe they are not the same after all?

I don't think there is any point continuing this discussion as you seem determined to ignore all the help which is offered to you.
If I were to ignore all the help I'm getting here, then i'm not gaining anything posting questions that are non-assigned by the teacher. Just looking for help and to gain some insight on c++. I know this discussion has been carried way far then it was suppose too and I do apologize for that :) meanwhile I do appreciate for your help. Also for that specific function you told me to fix I actually did it your way :) unless i'm not seeing something :D

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
//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);

}
}
Last edited on
Topic archived. No new replies allowed.