Why the 5 random characters?

Can anyone tell me why my output for this program looks like this: (The 5 characters underlined are the extra ones at the top.)

8
85
3
361235
85321
3862663
6t426 
2222222222222222222222222
7777777777777777777777777
99999999999999999999999993
9999999999999999999999999
4444444444444444444444444
999999999999999


Instead of like this:


8
3
36123
85321
3862663
67426
2222222222222222222222222
7777777777777777777777777
9999999999999999999999999
9999999999999999999999999
4444444444444444444444444
999999999999999


It's so close, but I can't figure out where I've gone wrong in my code. Here's the code:
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
//***********************************************************************************
//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;

//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
    }
    char 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
    cout << ch << endl;    //Test value of ch
    outfile << ch << endl; //Echo

    //While read-in has not reached the end of
    while (!infile.eof())
    {
        //To read in each whole number per line
        while ( ch != '\n')    //While read-in value is not a new line character
        {
            chary[i] = ch;     //Assign value of ch to current pos in array
            cout << chary[i];  //Display current value in array to test
            outfile << chary[i];//Echo
            i = i + 1;         //Increment counter
            infile.get(ch);    //Read in next character
        }
        i = i + 1;          //Increment counter
        infile.get(ch);     //Read in next character
        cout << chary[i] << endl;   //Display current value to test
        outfile << chary[i] << endl;//Echo
    }
    return 0;
}

Last edited on
Lol. Thanks for all the help. I've figured out how to fix everything except that funny character that changes every time....Please someone help me figure this out, so I can move on...
I changed my code:

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
    }
    char 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 numbers
    infile.get(ch);        //Read in first character
    //While read-in has not reached the end of file
    while (!infile.eof())
    {

        //To read in each whole number per line
        while ( ch != '\n')    //While read-in value is not a new line character
        {
            chary[i] = ch;     //Assign value of ch to current pos in array
            cout << chary[i];  //Display current value in array to test
            outfile << chary[i];//Echo
            i = i + 1;         //Increment counter
            infile.get(ch);    //Read in next character
        }
        //infile.get(ch);     //Read in next character
        i = i + 1;
        cout << endl;
        outfile << endl;
        infile.get(ch);
    }
return 0;
}


And now my output looks like this:
8
3
36123
85321
3862663
6z426                              <---------This pesky z still changes every time program runs.               
2222222222222222222222222              And it just needs to be a 7, and then I'm golden. 
7777777777777777777777777
9999999999999999999999999
9999999999999999999999999
4444444444444444444444444
999999999999999


Topic archived. No new replies allowed.