run time error

Sep 25, 2014 at 5:16pm
I'm getting an error and I don't understand why. I'm reading in a file of numbers that I need to convert from characters to a number that I store in an array. Then I need to fill the rest of the array with zeros.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void setupNums(int number[],int &size)
{
	ifstream inputFile;
	int i = 0;
	char num;

	inputFile.open("bignumbers.txt");

	inputFile >> num;
	
	while (num != '\n')
	{
		number[i] = num + '0';
		inputFile >> num;
		i++;
	}
	size = i;
	for (int i = size; i < 6; i++)
	{
		number[i] = 0;
	}


}
Sep 25, 2014 at 5:30pm
inputFile >> num; formatted input skips whitespace characters, so condition (num != '\n') will always be true. Use unformtted input, say, .get() member function.

Also: number[i] = num + '0'; I think, you meant , not +
Sep 25, 2014 at 6:01pm
Ok I did that and it works. I have another question though. If I wanted to call the function multiple times to read in different numbers, but not start at the beginning of the file every time how would I do that? or would I have to read in every number at once and pass all of the arrays?
Sep 25, 2014 at 6:16pm
Make your function to accept reference to istream as one of the parameters. You will open file in calling function and will just call function again on already existing file.
Topic archived. No new replies allowed.