0

I am trying to print my vector in the display function, but it fails. I'm about 80% sure my syntax is correct. I have tried passing it from class to class. I am sure the file is being read in correctly because when I put my for loop in ClientProcessing() it works fine but I want to separate them.

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;

// struct to hold the initial information of each client
struct Clients
{
  string client_name = " "; // first and last as one line 
  char account_type = ' '; // C for Checking - S for savings;
  double initial_deposit = 0.0; // Amount for initial deposit
};
// Prototype for Account Manager
class AccManager
{
public:
    vector<Clients> client_info;
    string client_name = ""; // first and last as one line 
    char account_type = ' '; // C for Checking / S for savings;
    double initial_deposit = 0.0; // Amount for initial deposit
    AccManager(){};
    void set_name(string n) {client_name = n;};
    string get_name(){return client_name;};
    void set_type(char t){account_type = t;};
    char get_type(){return account_type;};
    void set_initial_deposit(double d){initial_deposit = d;};
    double get_initial_deposit(){return initial_deposit;};
};
// Prototype for the UI - User interface
class UI
{
public:
    void ClientProcessing();
    void ShowClientInfo();
};

int main() 
{
  UI ui;
  ui.ClientProcessing();
  ui.ShowClientInfo();
  
}
// A module that reads in the the client data file
void UI::ClientProcessing()
{ 
  ifstream infile; 
  AccManager a;
  int i = 0;
  cout << "Processing client information...\n";
  infile.open("client_data.txt");
  if(infile.is_open()){
    cout << "Opened client file sucessfully! \n";
    while(!infile.eof()){
      while (i < 1){
        getline(infile, a.client_name, '\t');
        a.set_name(a.client_name);
        a.client_info.push_back(Clients());
        a.client_info[i].client_name = a.get_name();
        i++;
        
      } 
    }
  }
  else{
    cout << "Error opening file! \n";
  }
  infile.close();
  
}
// A module to display the client info as a table
void UI::ShowClientInfo()
{
  AccManager a;
  cout << "Name                       D\n";
  for(int i = 0; i < a.client_info.size(); i++){
   
    cout << a.client_info[i].client_name; // Will not print here

    
  }
   
}

James Risner
  • 5,451
  • 11
  • 25
  • 47
  • `AccManager a;` is a default constructed instance different from the one you created in the other method – 463035818_is_not_an_ai Dec 15 '22 at 19:10
  • `ShowClientInfo()` creates an empty `AccManager`, so there is nothing to display. Perhaps you should have it as a member variable, or pass it as a parameter to each function? – BoP Dec 15 '22 at 19:10
  • I suggest to try something simpler first. its like you have `int x = 42;` in one method and then `int x; std::cout << x;` in a different method. Much simpler but same issue – 463035818_is_not_an_ai Dec 15 '22 at 19:12
  • You seem to have the beginner misunderstanding that if you declare two variables in different places with the same name, they are the same variable. This is not true. So `AccManager a;` in `UI::ShowClientInfo()` is not the same variable as `AccManager a;` in `UI::ClientProcessing()`. This explains the error, you are setting up one variable correctly, but then printing a different variable. – john Dec 15 '22 at 19:21

1 Answers1

0

Converted my code to the following and passed in the vector. It worked!

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;

// struct to hold the initial information of each client
struct Clients
{
  string client_name = " "; // first and last as one line 
  char account_type = ' '; // C for Checking - S for savings;
  double initial_deposit = 0.0; // Amount for initial deposit
};
// Prototype for Account Manager
class AccManager
{
public:
    vector<Clients> client_info;
    string client_name = ""; // first and last as one line 
    char account_type = ' '; // C for Checking / S for savings;
    double initial_deposit = 0.0; // Amount for initial deposit
    AccManager(){};
    void set_name(string n) {client_name = n;};
    string get_name(){return client_name;};
    void set_type(char t){account_type = t;};
    char get_type(){return account_type;};
    void set_initial_deposit(double d){initial_deposit = d;};
    double get_initial_deposit(){return initial_deposit;};
};
// Prototype for the UI - User interface
class UI
{
public:
    void ClientProcessing(vector<Clients> &client_info);
    void ShowClientInfo(vector<Clients> client_info);
};

int main() 
{
  UI ui;
  AccManager a;
  ui.ClientProcessing(a.client_info);
  ui.ShowClientInfo(a.client_info);
  
}
// A module that reads in the the client data file
void UI::ClientProcessing(vector<Clients> &client_info)
{ AccManager a;
  ifstream infile; 
  int i = 0;
  cout << "Processing client information...\n";
  infile.open("client_data.txt");
  if(infile.is_open()){
    cout << "Opened client file sucessfully! \n";
    while(!infile.eof()){
      while (i < 1){
        getline(infile, a.client_name, '\t');
        a.set_name(a.client_name);
        client_info.push_back(Clients());
        client_info[i].client_name = a.get_name();
        i++;
        
      } 
    }
  }
  else{
    cout << "Error opening file! \n";
  }
  infile.close();
  
}
// A module to display the client info as a table
void UI::ShowClientInfo(vector<Clients> client_info)
{
 
  cout << "Name                       D\n";
  for(int i = 0; i < client_info.size(); i++){
   
    cout << client_info[i].client_name;
    
  }
   
}
  • 4
    `while(!infile.eof())` [is always wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). – n. m. could be an AI Dec 15 '22 at 19:50