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 8362687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:59:52+00:00 2026-06-09T11:59:52+00:00

Here is some part of my main: int main() { Inventory Master; bool flag;

  • 0

Here is some part of my main:

int main() {
  Inventory Master;
  bool flag;

  Customer Bob("Bob", "CreditCard.txt");
  Customer Chris("Chris", "CreditCard.txt" ); 
}

Here is my method:

Customer::Customer( string n, string fileName ) {
  name = n;
  ifstream Credit;

  Credit.open(fileName.c_str(), ios::in);

  while( Credit.good() && !Credit.eof() ) {
    Credit >> card >> balance >> ws;
    cout << card <<"\t" << balance << endl;

  }


 CreditCard _CC( int card, double balance);
}

Here is my “CreditCard.txt file:

12345  15.00
32564  20.00

The way I wanted the info to display is have line 1 “12345 15.00” assigned to Bob and line 2 assigned to Chris and do that so on and so forth if i make new instances or objects of a customer. However the way I currently implemented it is it keeps assigning “12345 15.00 and 32564 20.00” to both Bob and Chris. I could appreciate the help if someone could SHOW me how to somehow point to certain lines of the text file so Bob is assigned to line 1, Chris to line 2, and more customers to other lines when i add them in the text file.

  • 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-06-09T11:59:53+00:00Added an answer on June 9, 2026 at 11:59 am

    Everything you’re doing to Bob and Chris happens inside the constructor. So, as written, your code says: while the stream is in good condition and it’s not the end of the file(key point), write to here.

    Well, if you think about it, this will read until the end of the file is reached for each instance of Customer. That’s not what you want. I might suggest adding the name as the first field in the data file for each record. You could then search the file for the correct record, assuming you ensure the names are all uniquely defined, then pull the data out string by string. That way it’s not reading from the beginning to the end each time. I added “Bob” as the first field on line 1, and “Chris” to line 2 and made string name = "Chris";. So…

    #include <string>
    #include <iostream>
    #include <fstream>
    using namespace std;
    int main()
    {
      string tempStr;
      string name = "Chris";
      ifstream Credit;
    
      Credit.open("Info.txt", ios::in);
    
      while( Credit.good() && !Credit.eof() ) 
      {
          getline(Credit, tempStr, ' ');//Reads the first records name field
          cout << tempStr << endl;
          if(name.compare(tempStr) == 0)//Compares the "name" to the field.
          {                            //If true they are the same
              //Proceed to do reading and assignments with additional getline statements
              cout << "Chris was matched the second time around!";
              Credit.setstate(ios::eofbit, true);//***Sets eof to true
          }
          else 
          {
              Credit.ignore(50, '\n');
              //That should put Credit in the proper position to read the next name
          }
      }
    

    }

    The way you’re doing it will cause problems. The only way that it would work for sure is if you knew where the record was at in the file. What if you had five records? By the time you got to the third one you would have to ignore, or similar, all the fields prior to the one you’re working on. Also, it could be handy for a human to read a print out of the data file. Another reason to provide a label(name) to each record. Also, you’re apparently using namespace std;, so I did too, but it’s frowned upon.

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

Sidebar

Related Questions

Just putting part of some code here where I am writing two values to
I have some accent issues in a shopping cart script. Here's a part of
JAVA Code Here is some part of my code that I have written in
Why does the titlebar overlap the pixels of JPanel. Here some code: protected void
Here is some sample Python code: import re some_regex = re.compile(r\s+1\s+) result = some_regex.search(
Here are some examples of strings (mainly addresses): 12 20 43-B 43-C 123 2500
Here are some outputs: Date.today => Mon, 25 Jun 2012 Date.today.to_formatted_s(:long_ordinal) => June 25th,
Here's some LINQ to select all order details. It creates a join with the
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
Here is some dprofpp -I output, for almost the same program on different input

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.