C++ 为每个字符串使用不同的索引变量的字符串到字符串构建函数

C++ 为每个字符串使用不同的索引变量的字符串到字符串构建函数,c++,string,parsing,C++,String,Parsing,下面有两个程序。在第一步中,我将所有的东西都剥离出来,但问题循环除外,当我这样做时,它就起作用了。在第二个程序中(仍然是从我正在处理的程序中剥离出来的),我使用相同的逻辑,但是为_loop_buffer调用的子字符串从未加载 基本上,我正在解析一个字符串,其中包含来自cdf文件的记录。for循环缓冲区是我构建每个字段并将其保存到类的地方。我已经在这上面转了好几天了,如果有任何帮助,我将不胜感激 编辑: 以下是程序正在读取的文件的内容:0,Nicole 0,Debbie– 计划1 /*******

下面有两个程序。在第一步中,我将所有的东西都剥离出来,但问题循环除外,当我这样做时,它就起作用了。在第二个程序中(仍然是从我正在处理的程序中剥离出来的),我使用相同的逻辑,但是为_loop_buffer调用的子字符串从未加载

基本上,我正在解析一个字符串,其中包含来自cdf文件的记录。for循环缓冲区是我构建每个字段并将其保存到类的地方。我已经在这上面转了好几天了,如果有任何帮助,我将不胜感激

编辑:

以下是程序正在读取的文件的内容:0,Nicole 0,Debbie–

计划1

/*******************************************************\
* Debug Version 02-b-1 - string to string build         *
*                                                       *
* Saving a Person Object to file                        *
* This program will have a class that can save and      *
* retrieve itself to and from a flat file in cdf format *
*                                                       *
* The program will accept screen input for subsequent   *
* Object store call and will allow a print command that *
* will create a file of mailing labels.                 *
\*******************************************************/
#include <cstdlib>
#include <iostream>
#include <string>

std::string current_person = "Why does this not work?     ";         // CDF String version of person record from file
std::string for_loop_buffer = "                                        ";        // the substring buffer
int counter2 = 0;

int main( )
{
    for (int i = 0;i <= 23;++i){

/*****************************************************************************\
* This next line apears to be the problem                                     *
\*****************************************************************************/
          for_loop_buffer[counter2] = current_person[i];

  std::cout << "DEBUG - Current person " << current_person << std::endl;
  std::cout << "DEBUG - current Person element " << current_person[i] << std::endl;
  std::cout << "DEBUG - for Loop Buffer " << for_loop_buffer << std::endl;
  std::cout << "DEBUG - for Loop Buffer element " << for_loop_buffer[counter2] << std::endl;
  std::cout << "DEBUG - for Loop Buffer counter " << counter2 << std::endl;
      ++counter2;

    }  // close for


return (0);
}
/*******************************************************\
*调试版本02-b-1-字符串到字符串的生成*
*                                                       *
*将Person对象保存到文件*
*这个程序将有一个类,可以保存和*
*以cdf格式在平面文件中检索自身*
*                                                       *
*该程序将接受后续操作的屏幕输入*
*对象存储调用并将允许打印命令*
*将创建邮件标签文件*
\*******************************************************/
#包括
#包括
#包括
std::string current_person=“为什么这不起作用?”;//文件中人员记录的CDF字符串版本
std::_loop_buffer=“”;/)的字符串子字符串缓冲区
int计数器2=0;
int main()
{

for(int i=0;i很难看穿代码,但在您的“真实”代码中,您说
for_loop_buffer.clear();
,这与
for_loop_buffer=“”
,之后我认为您正在访问(现在为空)字符串的一个成员:


很难看穿代码,但在“真实”代码中,您说的是
for_loop_buffer.clear();
,这与
for_loop_buffer=“”;
,之后我认为您正在访问(现在为空)字符串的一个成员:


您正在循环字符串,用逗号将其拆分,但是有一些标准库函数可以为您简化。这听起来像是家庭作业,所以我不会发布任何代码答案,但这里有一些提示:

  • 将为您搜索字符串,并告诉您下一个逗号在哪里
  • 给定字符串中的位置和长度,将返回字符串的一部分
在循环中使用上述两个函数,您可以用更少的代码选择字段,这更容易理解


或者,可以直接从流中读取值,因为您使用的函数有一个额外的参数来指定终止行的字符(即逗号).

您正在循环字符串,用逗号将其拆分,但是有标准的库函数可以为您简化。这听起来像是家庭作业,所以我不会发布任何代码答案,但这里有一些提示:

  • 将为您搜索字符串,并告诉您下一个逗号在哪里
  • 给定字符串中的位置和长度,将返回字符串的一部分
在循环中使用上述两个函数,您可以用更少的代码选择字段,这更容易理解


或者,可以直接从流中读取值,因为您使用的函数有一个额外的参数来指定终止行的字符(即逗号).

Woops差点忘了这是程序正在读取的文件的内容:0,Nicole 0,Debbie Woops差点忘了这是程序正在读取的文件的内容:0,Nicole 0,Debbie
/*******************************************************\
* Debug Version 02 - Read data from a cdf file          *
*                                                       *
* Saving a Person Object to file                        *
* This program will have a class that can save and      *
* retrieve itself to and from a flat file in cdf format *
*                                                       *
* The program will accept screen input for subsequent   *
* Object store call and will allow a print command that *
* will create a file of mailing labels.                 *
\*******************************************************/

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
/***************************************************************\
* Class Definition - person                                     *
*                                                               *
* Member functions                                              *
* save_person -- Save the person object to a flat file in CDF   *
* get_person -- Retrives a persons data from a CDF flat file    *
\***************************************************************/

// Definition of the Class
class person_class {

  public:
    struct person_contact {
      int person_number; // System assigned not on ui  
      std::string first_name;
    };


  // Declarations for the method Prototypes
  public:            

    // A Function that retrieves 1 person from file and
    // provides it to the user
    void get_person();

} person;


/*********************************************************\
* person_class::get_person -- get a data record from file *
*                                                         *
\*********************************************************/
inline void person_class::get_person()
{
// Declaration of private variables for this method

std::string ui_first_name;     // User Input from Keyboard
person_contact target_person;  // Name of the opbect we are filling
std::string current_person;    // CDF String version of person record from file

  std::cout << "Enter the First Name of the person you are looking for: ";
  std::cin >> ui_first_name;

  std::cout << std::endl << "Matching Records: " << std::endl;
  // Open file
  std::ifstream input_file("ntc_db02test.ntc");

  std::cout << "DEBUG - I opened the file " << std::endl;

  current_person.clear();
  while (input_file.good()){

  std::cout << "DEBUG - I have entered the while loop " << std::endl;


    // Grab the next record from a comma delimited file
    getline(input_file,current_person);

  std::cout << "DEBUG - I have performed a getline " << std::endl;
  std::cout << current_person << std::endl;


    // I need a mechanism to build substrings

    // Substring Buffer stuff
    int buffer_pointer = 0;             // pointer to the next position in the substring buffer
    std::string for_loop_buffer ="                                        ";        // the substring buffer
      for_loop_buffer.clear();

    int field_index = 0;                // Which field is next to load

/*******************************************************************\
* If everything went as planned I have a single CDF person record   *
* in the current_person string and now all I have to do is parse    *
* it and place each field in the current instance.                  *
\*******************************************************************/
    // load the instance with the record data from the file
    for (int i = 0;i < current_person.length();++i){

  std::cout << "DEBUG - I am in the for loop " << std::endl;

      // look for the end of the CDF field
      if(current_person[i] == ','){
        // Write the buffer to the next field
  std::cout << "DEBUG - I found a comma " << std::endl;

        switch (field_index) {

          case 0:
             stringstream(for_loop_buffer) >> target_person.person_number;
  std::cout << "DEBUG - Field Index " << field_index << std::endl;
  std::cout << "DEBUG - For Loop Buffer " << for_loop_buffer << std::endl;
  std::cout << "DEBUG - Person Number " << target_person.person_number << std::endl;
            break;

          case 1:
             stringstream(for_loop_buffer) >> target_person.first_name;
  std::cout << "DEBUG - First Name " << target_person.first_name << std::endl;
            break;


          default:
            std::cout << "This should not happen, index not functioning " << '\n';
            break;
        }  // Close Switch

        // clear the buffer
          for_loop_buffer.clear();  //SE
          buffer_pointer = 0;
        // set up the next field load - increment the field index
        ++field_index;   // 
      }else{  // close if

  std::cout << "DEBUG - not a comma " << std::endl;

        // If the character is not a comma
        // add the character to the buffer

/*****************************************************************************\
* !!!!!!!!!!!!   This next line apears to be the problem  !!!!!!!!!!!!!!!     *
\*****************************************************************************/
          for_loop_buffer[buffer_pointer] = current_person[i];

  std::cout << "DEBUG - Primary Index i " << i << std::endl;
  std::cout << "DEBUG - current Person element " << current_person[i] << std::endl;
  std::cout << "DEBUG - Buffer Pointer " << buffer_pointer << std::endl;
  std::cout << "DEBUG - for Loop Buffer element " << for_loop_buffer[buffer_pointer] << std::endl;
  std::cout << "DEBUG - for Loop Buffer " << for_loop_buffer << std::endl;


        // Increment the buffer pointer
          ++buffer_pointer;
      } // close else
    }  // close for
    if (target_person.first_name == ui_first_name){

      // Code to print out full record

      std::cout << target_person.first_name << " "
                << std::endl;
    }  // Close if
  }  // Close While
  input_file.close();

}  // Close Class


/******************************************************************************\
* The idea this time is to do as little in the main as possible and as much    *
* in the class as is appropriate. So rather than read in data and pass it to   *
* the method, I will call the method and let it do its own UI reading.         *
\******************************************************************************/


int main( )
{

/**********************************************************\
* Ask the user if they want to Create a new person record  *
* n or find a record in the database f                     *
\**********************************************************/

  // Set up for user input
  char command = 'f';

  std::cout << " What do you want to do? New Person (n) or Find Person (f) ";
  std::cin >> command;

  while (command == 'n' || command == 'f'){
    if (command == 'n'){

      std::cout << "Do Nothing" << std::endl;

    } else if (command == 'f'){
      person_class current_person_object;
      current_person_object.get_person();  

    }
  command = 'x';
  std::cout << "I am Back in Main" << std::endl;
  }
std::cout << "You entered something other than a -n- or a -f-, program terminating now" << std::endl;

return (0);
}
for_loop_buffer[buffer_pointer] = current_person[i]; // Illegal access!