C++ 将逗号添加到数组C++;

C++ 将逗号添加到数组C++;,c++,C++,我试图在数组中的一组数字中添加逗号 我有一个程序,可以接收随机数,其长度由用户的输入决定。这些数字存储在指针数组中。我制作了另一个数组来存储从int到string的转换数字。现在,我正在开发一个函数来为它们添加逗号。我对此函数有问题。infoArrayString是用户输入从int到string的转换数。问题出在addCommas函数中 #include <iostream> #include <string> using namespace std; void va

我试图在数组中的一组数字中添加逗号

我有一个程序,可以接收随机数,其长度由用户的输入决定。这些数字存储在指针数组中。我制作了另一个数组来存储从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);


}
#包括
#包括
使用名称空间std;
无效有效编号(整数和x){
while(cin.fail()){
cout>x;
}
}
无效有效数字指针(int*&x){
while(cin.fail()){
cout>*x;
cout>userAmount;
cout(最大输入){

CUT< P>如果没有特定的原因,你在C++中使用原始数组,你可能想使用<代码> STD::vector < /C>。它们更容易操作。< /P>
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));
}
您可能只需要逗号来格式化。在这种情况下,您不必改变
向量的值

如果您所追求的是
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;
    }
  }
}
//格式化csv文件样式输出。
带逗号的无效格式(标准::向量和字符串列表){
int line_break=0;
对于(int i=0;istd::难道这里没有足够的信息吗?要从用户那里获取int,你不需要将它们的字符串转换为int吗?或者你是否生成了一个csv文件,用户只输入一个数字来表示有多少个值?问题是什么?发布的代码不足以理解程序中发生了什么。请发布一个.is
infoArraySt环[0]
最低有效位或最高有效位?编辑:我想这就是您试图处理偏移量的原因。
I%3==I
infoArrayString[I]=infoArrayString[I]+“,”;
我看错了。因为后者不是infoArrayString一个字符串数组。你想在其中一个字符串中插入一个“,”吗?我认为通过指针传递是一个错误?你是想在这个函数中处理一个字符串吗?目前你传递了一个字符串数组(显然)。问题是我不能使用其他库。只能使用iostream、string和命名空间std@http移除。并将字符串数作为第二个参数传递。
// 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;
}