Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6142093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:20:24+00:00 2026-05-23T18:20:24+00:00

There are 2 programs below. In the first I have striped out everything but

  • 0

There are 2 programs below. In the first I have striped out everything but the problem loop and when I do that it works. In the second program (still striped down from the one I am working on) I use the same logic but the sub string called for_loop_buffer never loads.

Basically I am parsing 1 string which holds a record from a cdf file. The for loop buffer is where I build each field and save it to my class. I have been spinning my wheels on this for days any help would be appreciated.

EDIT:

Here are the contents from the file the program is reading:0,Nicole 0,Debbie –

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

Program 2

/*******************************************************\
* 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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:20:25+00:00Added an answer on May 23, 2026 at 6:20 pm

    You are looping through the string, splitting it up on commas, however there are standard library functions that can simplify this for you. This sounds like homework, so I wont post any code answers, but here are some hints:

    • std::string::find_first_of will search through a string for you, and tell you where the next comma is.
    • std::string::substr given a position in the string and a length, will return a part of the string.

    Using the above two functions in a loop you could pick out your fields in less code, that’s easier to follow.

    Alternatively, it would be possible to read the values straight from the stream, as the std::getline function you are using has an extra parameter to specify what character terminates the line (i.e. the comma).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the program below, there are two things that I don't understand. How can
Just a curious question but is there any programs that can help/aid you when
The example program below compiles two in-memory assemblies. The first compilation works fine. The
I have a little problem. I have a program that will draw wave function
Say I have a Java program such: //case1 Long first = 1; Long second
Let me first describe the problem. I have this application that opens a word
Are there any programs that will allow you to follow a sql transaction through
First of all, to clarify my goal: There exist two programs written in C
There are a lot of programs, Visual Studio for instance, that can detect when
We have a fairly simple program that's used for creating backups. I'm attempting to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.