My professor has working with very large number. We bring them into our project through a file stream (.txt). I get the MATH behind using 3 arrays to add, subtract, and store the sum of the numbers. I get that you can do a "x-'0'" to convert the char to an int. My arrays are to be able to handle up to 26 digits.

The portion of the code I am having a problem with is below. Right now I am just trying to see if my program brings in the number set for n1, and the number set for n2 correctly. This is what the number set looks like in the .txt file:


5
7
36
85
3878563
6426
2222222222222222222222222
7777777777777777777777777
9999999999999999999999999
9999999999999999999999999
4444444444444444444444444
999999999999999

So n1 would get 5
n2 would get 7.
then i would add them to get, divide and mod if i have to for carry over, all that I get how to do.

The first set comes in to my arrays just fine, padded with extra zeros as he wants it to be. But the second set that should be 3 6 and 8 5 is all messed up. It looks like its getting numbers and skipping them somewhere:

-------------------------------------------------------
this set is fine
5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

the second set is blah!
5 3 -38 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
7 3 7 5 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Press any key to continue . . .
---------------------------------------------------------------

I'm desperate. My code i have so far is below. I'm sure its flawed or has a more efficient way of going about it, but its at my skill level that I can understand, so please consider that in your reply.

crap..sorry..here is the code.

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

ifstream dataIn;
ofstream dataOut;

int n1[26], n2[26], sum[26];//arrays
int n1Count = 0, n2Count = 0;//keeps track of how many numbers digits added to array.

//basic array printer using an array and a lenth as params
void printarray (int arg[], int length) {
  for (int n=0; n<length; n++)
    cout << arg[n] << " ";
  cout << "\n";
}

//get numbers as strings from file, convert to int and add to array
void readInFirst(){


     char x;
     n1Count++;
     dataIn.get(x);

     x=x-'0';

     n1[n1Count-1] = x;



     while(dataIn.get() != '\n'){
         n1Count++;
     dataIn.get(x);

     x=x-'0';

     n1[n1Count-1] = x;
     }
     }
     /////////////////////////////////////
void readInSecond(){
     char x;//

     n2Count++;
     dataIn.get(x);

     x=x-'0';//convert

     n2[n2Count-1] = x;



     while(dataIn.get() != '\n'){
         n2Count++;
     dataIn.get(x);

     x=x-'0';

     n2[n2Count-1] = x;

         }
     }

int main(int argc, char *argv[])
{
    dataIn.open("BigNumbersV2.txt");

    //this set works fine
    readInFirst();
    readInSecond();
    printarray(n1,26);
    printarray(n2,26);

    cout << endl;

    //this set is missed up
    readInFirst();
    readInSecond();
    printarray(n1,26);
    printarray(n2,26);

    //pauses the console screen.
    system("PAUSE");
    return EXIT_SUCCESS;
}

Code tags and formatting please. Code is too hard to read otherwise.

[code]

// formatted code goes here. Convert all tabs to spaces please

[/code]

NetBeans and Visual Studio both have auto-formatters that will do the formatting/indenting/tabs-to-spaces formatting for you. Both are free.

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

ifstream dataIn;
ofstream dataOut;

int n1[26], n2[26], sum[26];//arrays
int n1Count = 0, n2Count = 0;//keeps track of how many numbers digits added to array.

//basic array printer using an array and a lenth as params
void printarray (int arg[], int length) {
  for (int n=0; n<length; n++)
    cout << arg[n] << " ";
  cout << "\n";
}

//get numbers as strings from file, convert to int and add to array
void readInFirst(){
    
    
     char x;
     n1Count++;
     dataIn.get(x);
    
     x=x-'0';
    
     n1[n1Count-1] = x;
    
    
    
     while(dataIn.get() != '\n'){
         n1Count++;
         dataIn.get(x);
         x=x-'0';
    
     n1[n1Count-1] = x;
     }
}
     /////////////////////////////////////
void readInSecond(){
     char x;
    
     n2Count++;
     dataIn.get(x);
    
     x=x-'0';//convert
    
     n2[n2Count-1] = x;
    
    
    
     while(dataIn.get() != '\n'){
         n2Count++;
         dataIn.get(x);
    
     x=x-'0';
    
     n2[n2Count-1] = x;
     }
}

int main(int argc, char *argv[])
{
    dataIn.open("BigNumbersV2.txt");
   
    //this set works fine
    readInFirst();
    readInSecond();
    printarray(n1,26);
    printarray(n2,26);
   
    cout << endl;
   
    //this set is missed up
    readInFirst();
    readInSecond();
    printarray(n1,26);
    printarray(n2,26);
   
    //pauses the console screen.
    system("PAUSE");
    return EXIT_SUCCESS;
}

When debugging this, I would check to make sure your assumptions are valid. For instance, lines like x=x-'0'; are going to be messed up if x is not a digit from '0' through '9', so check and make sure.

I am particular wondering whether there is a line feed (Ascii value 10) in there since you are displaying -38. '0' is Ascii 48, so 10 - 48 = -38. If you're using Windows, you likely have stuff like "\r\n" instead of just "\n" at the end of the line, so you want to check for all that.

A better way of handling all this IMO is to read a line at a time using getline, then parsing the string that you read in.

You should also try a file without the first two lines and see if the results you are getting for "36" and "85" are still bad.

When debugging this, I would check to make sure your assumptions are valid. For instance, lines like x=x-'0'; are going to be messed up if x is not a digit from '0' through '9', so check and make sure.

I am particular wondering whether there is a line feed (Ascii value 10) in there since you are displaying -38. '0' is Ascii 48, so 10 - 48 = -38. If you're using Windows, you likely have stuff like "\r\n" instead of just "\n" at the end of the line, so you want to check for all that.

A better way of handling all this IMO is to read a line at a time using getline, then parsing the string that you read in.

You should also try a file without the first two lines and see if the results you are getting for "36" and "85" are still bad.

Firstly thank you so much for your reply.

My professor requested that we read one char at a time using .get(), but I will keep you suggestion in mind as I suspect the next project will be dealing with strings.

After talking with a fellow classmate. I went back and redesigned my code, making it a little more elegant, at least to me, lol. The problem was in my original getNum() loop. I don't think it was actually ever getting to the while != \n, the way I had the primer set up. This is my new code, and is seems to be working properly now:

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

ifstream dataIn;
ofstream dataOut;

void printarry(int arg[]);
int zeroArray(int arg[]);
int getNum(int arg[]);
void spacer();

//basic array printer using an array and a lenth as params
void printarray (int arg[]) {
     for (int n=0; n<26; n++){
         cout << arg[n] << " ";
         }
         cout << "\n";
     
}

int zeroArray (int arg[]) {
    for (int n=0; n<26; n++){
        arg[n] = 0;
    }
}

int getNum(int arg[]){
    char x;
    int i=0;
    dataIn.get(x);
    
    
    while(x != '\n'){
         arg[i] = x-'0';
         i++;
         dataIn.get(x);
         //arg[i] = x-'0';
    }
         
}

void spacer(){
     cout << "---------------------------------------------------------------" << endl;
     }

int main(int argc, char *argv[])
{
    int n1[26], n2[26], sum[26];
    dataIn.open("BigNumbersV2.txt");
    
    
    zeroArray(n1);
    zeroArray(n2);
    zeroArray(sum);
    
    //show arrays have been zeroed
    printarray(n1);
    printarray(n2);
    spacer();
    
    //first set of numbers still rolling along good
    getNum(n1);
    getNum(n2);
    printarray(n1);
    printarray(n2);
    spacer();
    
    //second set of numbers is good now
    getNum(n1);
    getNum(n2);
    printarray(n1);
    printarray(n2);
    spacer();
    
    //third set
    getNum(n1);
    getNum(n2);
    printarray(n1);
    printarray(n2);
    spacer();
    
    //4th set
    getNum(n1);
    getNum(n2);
    printarray(n1);
    printarray(n2);
    spacer();

    //5th set
    getNum(n1);
    getNum(n2);
    printarray(n1);
    printarray(n2);
    spacer();
    
    //6th set
    getNum(n1);
    getNum(n2);
    printarray(n1);
    printarray(n2);
    spacer();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Once I get the shuffle to the right side of the array and the 'add' working, I will package them and the getNum and printarray into a loop that checks for the end of file. I also need to change the print so it ignores the 0's on the front, using two loops, if I'm picturing it correctly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.