将逗号添加到数组C ++中的数字

时间:2018-02-18 21:26:46

标签: c++

我正在尝试将逗号添加到数组中的一组数字。

我有一个程序,它将采用随机数,其长度由用户的输入决定。这些数字存储在指针数组中。我创建了另一个数组来存储从int到string的转换数字。现在我正在开发一个向它们添加逗号的函数。我遇到了这个功能的问题。 infoArrayString是从int到string的用户输入的转换数。问题出在addCommas函数

#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);


}

1 个答案:

答案 0 :(得分:0)

如果没有特定原因在C ++中使用原始数组,则可能需要使用std::vector。它们更容易操纵。

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));
}

然后,您可以将vector个字符串传递给修改每个条目以使用逗号的函数。

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

您可能只想要逗号进行格式化。在这种情况下,您不必改变vector的值。

如果您使用的是csv样式格式,那么您可能会遇到以下情况:

// 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;
    }
  }
}

现在,如果真的希望用户指定列表的长度,可以尝试调用类似的内容:

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

以上可以这样运行:

#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;
}