Confused with C-String size requirements

Hey guys,
I have a really simple question that has been bugging me for a long time! This is some code I wrote back when I was studying c-strings. If you notice in the 'getanswers' function I use an array to read in 20 characters into the c-string 'answers' which is allocated a space of 20. Now if you look at the 'getstudentanswers' function, I use the get function to read in all my data from a text file. The text file looks like this:

1
2
3
TFFTFTTFTFTTFFTFTFTT
ABC44343 TFTFTFTT TFTFTFFTTFT
CCD23934 TF FTF TFTFTFT FTFTT


So 'getanswers' reads the top line of the text file and completely fills up the char array 'answers' but my 'getstudentanswers' function isn't reading in the arrays the way I'd hoped. It doesn't 'fill' the array as such like the loop did, and I have to add an extra space onto the arrays studentid and studentanswers where the actual length of the strings in these arrays are 8 and 20 respectively, I've had to declare them as 9 and 21.

In short my questions is this, is there a difference between filling a char array using a loop and using the get function? If not, is there anything you can see in my 'getstudentanswers' function that maybe causing this problem?

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


using namespace std;

void getanswers(ifstream& testscore, char answers[]);
void getstudentanswers(ifstream& testscore, char studentid[][9], char studentanswers[][21]);
void printanswers(char studentid[][9], char studentanswers[][21], int studentscore[]);
void comparescores(char answers[], char studentanswers[][21], int studentscore[]);


int main()
{
	char answers[20]; //correct answers used to calculate score.
	char studentid[150][9]; // 
	char studentanswers[150][21];
	int studentgrades[150] = {0};

	ifstream testscore;
	ofstream outfile;

	testscore.open("testscores.txt");
	outfile.open("output.txt");

	getanswers(testscore, answers);

	for (int i = 0; i < 20; i++)
	{
		cout << answers[i];
	}
	
	cout << endl;

	testscore.ignore(256, '\n');

	getstudentanswers(testscore, studentid, studentanswers);
	comparescores(answers, studentanswers, studentgrades);
	printanswers(studentid, studentanswers, studentgrades);


	testscore.close();
	outfile.close();

	system("PAUSE");
	return 0;
}

void getanswers(ifstream& testscore, char answers[])
{
	for (int i = 0; i < 20; i++)
	{
		testscore.get(answers[i]);
	}
}

void getstudentanswers(ifstream& testscore, char studentid[][9], char studentanswers[][21])
{
	int i = 0;
	while(testscore)
	{
	testscore.get(studentid[i], 9, ' ');
	testscore.ignore(256, ' ');
	testscore.get(studentanswers[i], 21, '\n');
	testscore.ignore(256, '\n');
	i++;
	}
}

void printanswers(char studentid[][9], char studentanswers[][21], int studentscore[])
{
	for (int i = 0; i < 2; i++)
	{
		cout << studentid[i] << " " << studentanswers[i] << " " << studentscore[i] <<endl;
	}
}

void comparescores(char answers[], char studentanswers[][21], int studentscore[])
{
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 20; j++)
		{
			if (studentanswers[i][j] == answers[j])
			{
				studentscore[i] += 2;
			}
			else if (studentanswers[i][j] != answers[j])
			{
				studentscore[i] -= 1;
			}
			else if (studentanswers[i][j] == ' ')
			{
				studentscore[i] = studentscore[i];
			}
		}
	}
}


Thanks guys!
If you notice in the 'getanswers' function I use an array to read in 20 characters into the c-string 'answers' which is allocated a space of 20.

No you are reading 20 characters into an array of char, not a C-string. A C-string is an array of char terminated with the end of string character '\0'. You are not terminating the array to make it a C-string, and if you did you would need a size of 21 to hold a string of 20 characters.


n short my questions is this, is there a difference between filling a char array using a loop and using the get function?

Yes. In your getanswers() function you are retrieving a series of characters. In your getstudentanswers() funciton you are retrieving a series of C-strings. A C-string contains a terminantion character as stated above which a plain array of char lacks. And if you're retieving C-strings you need to reserve space for that termination character.

By the way I really recommend you think about switching to C++ strings instead of the C-strings they are much less error prone.



Topic archived. No new replies allowed.