1

forum!

I have a project where we are supposed to add numbers that are length 14 or greater. I did some digging and realized that there is no current type that takes numbers this big. So, I have the user enter the numbers as a string and the numbers they would like to add are stored in a static string array.

I would like to add the numbers from the static array together. The issue is I have no idea how to deal with numbers this large. I am assuming you would have to convert the string values into int's and add them up one by one? I am having a big issue coming up with the logic for this. Any help would be appreciated.

If not, if you can provide some context which could help me come up with some logic.

The only library functions I can use is iostream and string.

Here is my code if you'll like to see my logic! I have some test cases I am trying to figure out so please ignore the comment outs. But, if you run the code you should get a better sense of what I am trying to get out. I am trying to sum up the numbers the user enters.

#include <iostream>

#include <string>

using namespace std;


void amountOfNumbers(string &userAmount, int MIN_AMOUNT, int MAX_AMOUNT){

  //string alpha = "abcdefghijklmnopqrstuvwxyz";

  cout << "How many numbers? -> ";

  cin >> userAmount;

  cout << endl;

  while(!userAmount.find("abcdefghijklmnopqrstuvwxyz")){

    cout << "ERROR: must be a number, try again ->";

    cout << userAmount;

    //cin.clear();

    //cin.ignore(1000, '\n');

    cin >> userAmount;

    cout << endl;

  }


  int temp = stoi(userAmount);

  while((temp < MIN_AMOUNT) or (temp > MAX_AMOUNT)){

    cout << "ERROR: Program can only take in  " << MIN_AMOUNT << " - "<< MAX_AMOUNT  << " numbers. Try again ->";

    cin >> userAmount;

    cout << endl;

    temp = stoi(userAmount);

  }

}


void takeNumbers(string &userAmount, string (&numberArray)[11]){

  int temp = stoi(userAmount);

  for (int i = 0; i < temp; i++){

    cout << "Input number #" << i+1 << " ->";

    cin >> numberArray[i];

    cout << endl;

}

}

void display(string &userAmount, string (&numberArray)[11]){

  int temp = stoi(userAmount);


  for (int i = 0; i < temp; i++){

    cout <<  numberArray[i];

    cout << endl;

  }

}

void addNumber(string &userAmount, string (&numberArray)[11]){


}

int main() {

  const int MIN_AMOUNT = 2, MAX_AMOUNT = 11, MAX_INPUT = 14;

  string userAmount = "0";

  string numberInput;

  // static array

string numberArray [MAX_AMOUNT];





  amountOfNumbers(userAmount, MIN_AMOUNT, MAX_AMOUNT);

 takeNumbers(userAmount, numberArray);

display(userAmount, numberArray);
}
Paul
  • 13,042
  • 3
  • 41
  • 59
Victor Nwadike
  • 101
  • 1
  • 8
  • 2
    `long long` supports numbers up to about 20 digits, so 14 is no problem. – melpomene Feb 27 '18 at 23:13
  • 1
    Possible duplicate of [C++ handling very large integers](https://stackoverflow.com/questions/124332/c-handling-very-large-integers) – SwiftMango Feb 27 '18 at 23:13
  • Check https://stackoverflow.com/questions/124332/c-handling-very-large-integers and https://stackoverflow.com/questions/12988099/big-numbers-library-in-c – SwiftMango Feb 27 '18 at 23:13
  • He is required not to use any library except `iostream` and `string`, the question states this – user9335240 Feb 27 '18 at 23:16
  • First, why are you filtering out alpha? Why not filtering in numbers, minus sign and decimal points (also you don't need to filter in decimal points)?? Second: I think you might want to try reading as a string, then reverse iterate the both, adding and putting what is more than ten in a carry variable, then add the next, until you find one of the strings end – user9335240 Feb 27 '18 at 23:19
  • Fun fact: Stack Overflow is not a forum. – user4581301 Feb 27 '18 at 23:39

0 Answers0