i wrote this function readBig. It's purpose is to scan a number from the user as a string and converts each element of the string into an int and stores it in an int array then reverses the elements so the ones position is in n[0] tens position is in n[1] ect.. which is fine I've got that working fine. The only thing is, in my main program this function is called twice and i've got another function whose purpose is to add the two arrays and store the sum in a third array. I haven't gotten that far yet, I'm stuck just trying to print my two arrays, every time i try to print the first array it gives me the elements of the second array and it prints a bunch of nonsence for the second array... what do i need to do?
PS-not allowed to use pointers..

#include <iostream>
#include<string>
using namespace std;
//This program will test three functions capable of reading, adding, 
//and printing 100-digit numbers.

int readBig(int[]);
void printBig(int[]);
void addBig(int[], int[]/*, int[]*/);

const int MAX_DIGITS = 4;
int main()
    {
    // Declare the three numbers, the first, second and the sum:
    int numbr1[MAX_DIGITS], numbr2[MAX_DIGITS], sum[MAX_DIGITS];
    cout << "Please enter a number up to "<<MAX_DIGITS<< " digits: ";
    readBig(numbr1);
    cout << "Please enter a number up to "<<MAX_DIGITS<< " digits: ";
    readBig(numbr2);
    addBig(numbr1, numbr2, sum);

    //printBig(num1);
    //cout << "\n+\n";
    //printBig(num2);
    //cout << "\n=\n";
    //printBig(sum);*/
    system ("PAUSE");
    return 0;
    }

//////////Read Function

int readBig(int[])
{
    string num;
    int n[MAX_DIGITS],i;
    cin>>num;
    for(i=0;i<MAX_DIGITS;i++)
      {
        n[i]=num[MAX_DIGITS-1-i]-'0';
        cout<<"n["<<i<<"]"<<n[i]<<"\n";
      }

}

///////addBig
void addBig(int [],int [],int [])
{
     int i,sum[MAX_DIGITS],numbr1[MAX_DIGITS],numbr2[MAX_DIGITS];
     for(i=0;i<MAX_DIGITS;i++)
     {
       cout<<"num1["<<i<<"]"<<numbr1[i]<<"\n";
       cout<<"num2["<<i<<"]"<<numbr2[i]<<"\n";
       //sum[i]=num1[i]+num2[i];
       //cout<<"sum["<<i<<"]="<<sum[i]<<"\n";
     }
}

Recommended Answers

All 4 Replies

You sort of left off the printBig function which is what seems to be causing the problem.

Just a side note, though you can't use pointers when you pass an array you are technically passing a pointer anyway.

oooh. I haven't even written the printBig function yet.. I've just been trying to print straight from the addBig function i commented it out of my main program so it wouldn't call on it.

A couple problems with your functions:

int readBig(int[])    // what's the variable name being passed in?
{                     // same with addBig()
    string num;
    int n[MAX_DIGITS],i;
    cin>>num;
    for(i=0;i<MAX_DIGITS;i++) // what if you enter less than MAX_DIGITS digits?
      {
        n[i]=num[MAX_DIGITS-1-i]-'0';
        cout<<"n["<<i<<"]"<<n[i]<<"\n";
      }
// This is an INT function -- you must return an int
}

I think i may have figured it out to an extent... no more crazy output anyway

#include <iostream>
#include<string>
using namespace std;
//This program will test three functions capable of reading, adding, 
//and printing 100-digit numbers.


void readBig(int[]);
void addBig(int[], int[], int[]);

// This constant should be 100 when the program is finished.
const int MAX_DIGITS = 4;


int main()
    {
    // Declare the three numbers, the first, second and the sum:
    int numbr1[MAX_DIGITS], numbr2[MAX_DIGITS], sum[MAX_DIGITS];
    cout << "Please enter a number up to "<<MAX_DIGITS<< " digits: ";
    readBig(numbr1);
    cout << "Please enter a number up to "<<MAX_DIGITS<< " digits: ";
    readBig(numbr2);
    addBig(numbr1, numbr2, sum);
    system ("PAUSE");
    return 0;
    }

//Read Function

void readBig(int n[])
{
    string num;
    int i;
    cin>>num;
    for(i=0;i<MAX_DIGITS;i++)
      {
        n[i]=num[MAX_DIGITS-1-i]-'0';
        cout<<"n["<<i<<"]"<<n[i]<<"\n";
      }

}

//addBig
void addBig(int numbr1[],int numbr2[],int sum[])
{
     int i;
     for(i=0;i<MAX_DIGITS;i++)
     {
       //cout<<"num1["<<i<<"]"<<numbr1[i]<<"\n";
       //cout<<"num2["<<i<<"]"<<numbr2[i]<<"\n";
       sum[i]=numbr1[i]+numbr2[i];
       cout<<"sum["<<i<<"]="<<sum[i]<<"\n";
     }
}
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.