-2

I am trying to add commas to a set of numbers in an array.

I have a program that will take in random numbers the length of which are determined by the user's input. These numbers are stored in a pointer array. I made another array to store the converted numbers from int to string. Now I am working on a function to add commas to them. I am having an issue with this function. infoArrayString is the converted numbers of user input from int to string. The issue is in the addCommas function

#include <iostream>
#include <string>

using namespace std;

void validNumber(int & x){

   while (cin.fail()){

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

    cin.clear();

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

    cin >> x;

  }

}

void validNumberPointer(int *& x){

   while (cin.fail()){

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

  cin.clear();

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

  cin >> *x;


cout << endl;

}

}




void amount(int & userAmount, const int & MIN_INPUT, const int & MAX_INPUT)

{

  /*
   * Asks how many number they want
   */ 


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

  cin >> userAmount;

  cout << endl;

  /*
   * check
   */ 

  validNumber(userAmount);


  while ((userAmount < MIN_INPUT) or (userAmount > MAX_INPUT)){

    cout << "ERROR: number out of range" << endl;

    cout << "Please enter numbers in range of " << MIN_INPUT << " to " << MAX_INPUT << " ->";

    cin >> userAmount;

  }

}

void getInfo(int *& infoArray, int & userAmount){

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

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

  cin >> *(infoArray+i);

  cout << endl;

  /*
   * check
   */ 

  validNumberPointer(infoArray);

   while (*(infoArray+i) < 0){

    cout << "ERROR: number out of range" << endl;

    cout << "Please enter numbers in range of range  -> ";

    cin >> *(infoArray+i);

    cout << endl;

  }

 }

}

void convertString(int *& infoArray, string *& infoArrayString, int & userAmount){


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

  *(infoArrayString +i) = to_string(*(infoArray+i));

  }


}

void addCommas(string *& infoArrayString){

  for(int i = 0; i < infoArrayString[i].length(); i++){

    if(i%3 == 0 and i != 0){

      infoArrayString[i] = infoArrayString[i] + ",";

    }

  }

}

void displayBoard(string *& infoArrayString, int & userAmount){

  cout << "The sum of: " << endl;


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

    cout << *(infoArrayString++) << endl;

  }

}


int main() {

  const int MIN_INPUT = 2, MAX_INPUT = 11;

  int userAmount = MIN_INPUT;

  int * infoArray = NULL;

  infoArray = new int [MAX_INPUT];


  string * infoArrayString = NULL;

  infoArrayString = new string [MAX_INPUT];


  amount(userAmount, MIN_INPUT, MAX_INPUT);

  getInfo(infoArray, userAmount);

  convertString(infoArray,infoArrayString,userAmount);

  addCommas(infoArrayString);

  displayBoard(infoArrayString, userAmount);


}
Xam
  • 263
  • 2
  • 7
  • 19
http
  • 27
  • 1
  • 7
  • There’s not enough information here. To get ints from the user, don’t you have to convert their string to an int? Or do you generate a csv file and the user just inputs a number to say how many value? What is the issue? – Pam Feb 18 '18 at 21:36
  • The posted code is not sufficient to understand what's going on in your program. Please post a [mcve]. – R Sahu Feb 18 '18 at 21:36
  • Is `infoArrayString[0]` the least significant digit or most significant digit? Edit: I guess that is what you were trying to handle with offset. – drescherjm Feb 18 '18 at 21:36
  • `i % 3 == i` and `infoArrayString[i] = infoArrayString[i] + ",";` look wrong to me. For the latter isn't infoArrayString an array of strings. And you want to insert an ',' inside one of the strings? – drescherjm Feb 18 '18 at 21:39
  • I think passing by pointer is a mistake? Did you mean to work on one string in this function? Currently you pass in an array of strings (apparently). – Galik Feb 18 '18 at 21:43
  • Weird condition `i < infoArrayString[i].length()`. You can't get the number of strings from that pointer. Use [`std::vector`](http://en.cppreference.com/w/cpp/container/vector). – O'Neil Feb 18 '18 at 21:52
  • I think that weird condition was trying to get the length of a single string. The OP probably should not be passing an array of strings to this function. – drescherjm Feb 18 '18 at 22:26
  • Also ```and``` in the last ```if``` block is not valid C++... – eric Feb 18 '18 at 23:17
  • @Pam, @"R sahu" So, I proved all my code! You can take a run through it and see where the issue might lay. – http Feb 18 '18 at 23:51
  • @everyone else ! I Cannot use other library functions. Unless I would have! – http Feb 18 '18 at 23:53

1 Answers1

0

If there is not specific reason you are using a raw array in C++ you may want to use a std::vector. They are easier to manipulate.

void int_to_string(std::vector<int>& integer_list, 
                   std::vector<std::string>& string_list) {

  // You can read integers/strings (with streams) into a 
  // container directly this function is for demo purposes.

  for (auto& element : integer_list)
    string_list.push_back(std::to_string(element));
}

You can then pass that vector of strings to a function that modifies each entry to have a comma.

void add_commas_to_strings(std::vector<std::string>& S) {
  for (auto& element : S)
    element += ',';
}

You may just want the commas for formatting. In that case you don't have to mutate the values of the vector.

If a csv style format is what you are after then you may be after something like this:

// Formats csv file style output.
void format_with_commas(std::vector<std::string>& string_list) {
  int line_break = 0;
  for (int i = 0; i < string_list.size(); ++i) {
    if (line_break == 3) {
      std::cout << "\n";
      line_break = 0;
    }
    int is_end = line_break + 1;
    if (is_end == 3) {
      std::cout << string_list[i];
      ++line_break;
    } else {
      std::cout << string_list[i] << ", ";
      ++line_break;
    }
  }
}

Now if you really want the user to specify the length of a list perhaps try calling something like:

std::vector<int> user_defined_vector(std::vector<int>::size_type sz) {
  return std::vector<int>(sz, 0);
}

The above can be ran like so:

#include <iostream>
#include <vector>

int main() {

  std::vector<int> A{1, 2, 3, 4, 5, 6, 7, 8, 9};
  std::vector<std::string> B;

  int_to_string(A, B);

  format_with_commas(B);

  auto ten_element_vector_of_ints = user_defined_vector(10);

  return 0;
}
eric
  • 1,029
  • 1
  • 8
  • 9
  • The issue is I can't use other libraries. Only iostream, string and using namespace std – http Feb 19 '18 at 00:24
  • @http Remove [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice?s=1|826.8226). And pass the number of strings as second parameter. – O'Neil Feb 19 '18 at 09:46