//***********************************************************************************
//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>
usingnamespace 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;
}
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:
#include <iostream> //Header files
#include <fstream>
#include <cmath>
usingnamespace 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