Why isn't my infile.get working?

My program is supposed to read in numbers from a file, one character at a time.
When ran, the program correctly identifies that the first line only has one character, but it does not seem to have that it is an 8. For some reason, I get back the number 10, after I try to convert it from a char data type to an int data type. Teacher said the way to do this was to subtract a 0 from the character. What am I doing wrong?

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
//***********************************************************************************
//This is a program that will add two numbers and print out those numbers as well
//as their sum.  The program should handle up to 25 digit integer numbers.  The
//input is found in BigNumberV2.txt.  The format should be one number per line, and
//should be all right hand justified, and labeled, without showing any leading zeros.
//***********************************************************************************

#include <iostream>    //Header files
#include <fstream>
#include <cmath>

using namespace std;

//File descriptors
ifstream infile;
ofstream outfile;

int main()
{
    infile.open("BigNumbersV2.txt"); //Open text file to read from
    outfile.open("answers.out");    //Open textfile to write to
    if (!infile) //If input file did not open correctly
    {
        cout << "Error opening input file." << endl; //Display error message
        return 0; //Quit
    }
    if (!outfile) //If outfile does not open
    {
        cout << "Error opening output file." << endl; //Display error message
        return 0;  //Quit
    }
    int chary[25];   //Declare array for reading in numbers by character
    int num1ary[25]; //Declare array for storing number 1
    int num2ary[25]; //Declare array for storing number 2
    int sumary[25];  //Declare array for storing sum
    int i = 0;  //Declare and initialize an array position holder
    char ch;    //Declare variable for reading in each character from infile

    //Read-in the number
    infile.get(ch); //Read in first character
    while ( ch != '\n')  //While read-in value is not a new line character
    {
        chary[i] = ch; //Set read-in value to counter's position within array
        i = i + 1; //Increment your counter
        infile.get(ch); //Now get your next position within array
    } //Fall out of loop as soon as you hit a new line
    cout << "Length of first character array is: " << i << endl; //Display length to test
    cout << "Character read in is: " << ch << endl; //Display character to test
                                    //When I run it, this ch is blank

    //Convert the read-in character number to an integer number
    //Store the number in array: num1ary
    num1ary[i] = ch - 0;  //Convert position i in num1 array from char type to int type
    cout << "Number converted from char type to int type is: "
         <<num1ary[i]  << endl;  //Display num1 array position to test
                               //When I run it, numary1[i] is 10 
When you fall out of this loop, ch = '\n'.

1
2
3
4
5
6
    while ( ch != '\n')  //While read-in value is not a new line character
    {
        chary[i] = ch; //Set read-in value to counter's position within array
        i = i + 1; //Increment your counter
        infile.get(ch); //Now get your next position within array
    } //Fall out of loop as soon as you hit a new line 



In this next line instead of ch, try chary[0].

1
2
    cout << "Character read in is: " << ch << endl; //Display character to test
                                    //When I run it, this ch is blank 


Not sure what your teacher means by subtract a 0 but probably subtract the ASCII value for '0'.
Last edited on
I had a very similar problem recently. I think you need to do this:

num1ary[i] = ch - '0'; //Convert position i in num1 array from char type to int type
The teacher DID say to subtract the ASCII value from the number, but when I did that I got -38. So then I tried just subtracting the integer zero and that's what got me the answer 10. I'll go back and change a few lines and see what happens...Thanks for your input.
Still not working...strange, because it seems to be reading the first two numbers backwards. Why? How? Here's the new code, with the above mentioned change...and below it I placed the output....What the program SHOULD be telling me is that the first number is 1 character long, and that character is 8. The second number on the second line is 3, so I'm not sure how it's going there first...Please help!

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
#include <iostream>    //Header files
#include <fstream>
#include <cmath>

using namespace std;

//File descriptors
ifstream infile;
ofstream outfile;

//Prototypes

int main()
{
    infile.open("BigNumbersV2.txt"); //Open text file to read from
    outfile.open("answers.out");    //Open textfile to write to
    if (!infile) //If input file did not open correctly
    {
        cout << "Error opening input file." << endl; //Display error message
        return 0; //Quit
    }
    if (!outfile) //If outfile does not open
    {
        cout << "Error opening output file." << endl; //Display error message
        return 0;  //Quit
    }
    int chary[25];   //Declare array for reading in numbers by character
    int num1ary[25]; //Declare array for storing number 1
    int num2ary[25]; //Declare array for storing number 2
    int sumary[25];  //Declare array for storing sum
    int i = 0;  //Declare and initialize an array position holder
    char ch;    //Declare variable for reading in each character from infile

    //Read-in the number
    infile.get(ch) >> chary[i]; //Read in first character
    cout << chary[i] << endl;   //Display position in the array to test
    outfile << chary[i] << endl;//Echo
    while ( ch != '\n')  //While read-in value is not a new line character
    {
        cout << ch << endl; //Display number grabbed to test
        outfile << ch << endl; //Echo
        i = i + 1; //Increment your counter
        cout << "Length of first character array is: " << i << endl; //Display length to test
        cout << "Character read in is: " << num1ary[i] << endl; //Display character to test
        outfile << "Length of first character array is: " << i << endl; //Echo
        outfile << "Character read in is: " << num1ary[i] << endl; //Echo
        infile.get(ch) >> chary[i]; //Now get your next position within array
    } //Fall out of loop as soon as you hit a new line

    //Convert the read-in character number to an integer number
    //Store the number in array: num1ary
    num1ary[i] = ch - '0';  //Convert position i in num1 array from char type to int type
    cout << "Number converted from char type to int type is: "
         <<num1ary[i]  << endl;  //Display num1 array position to test
    outfile << "Number converted from char type to int type is: " //Echo
            <<num1ary[i]  << endl;  //Echo   



3
8
Length of first character array is: 1
Character read in is: -1
Number converted from char type to int type is: -38


Just for clarity, this is the infile I'm reading from...

1
2
3
4
5
6
7
8
9
10
11
12
8
3
36123
85321
3862663
67426
2222222222222222222222222
7777777777777777777777777
9999999999999999999999999
9999999999999999999999999
4444444444444444444444444
999999999999999

For future reference (for myself, really) it wasn't working because my array 'chary' was declared as an int, not a char array. Changing that fixed it. Go me. :)
Topic archived. No new replies allowed.