Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't my Node tempNode show the right data?

I have a bit of an issue with my program. I have a function void loadData() which will load the data from a text file customers.txt and store each line of data into a Linked List. My concern is, specifically with how I/O works. I managed to get the data from the text file into and stored into a linked list data member variable. When i call that variable i get the answer i want printed onto the console. std::cout << "Group Name: " << tempCustomer->groupName << std::endl;

However, i decided to run a console output command later in the function to test if all the variables had the right data, i realize that it was all over the place. I'm not sure why its not working.

Here is the loadData() function

void Groups::loadData(){
  fin.open("customers.txt"); 
  char holder[MAX_SIZE];

  if(!fin.is_open())
    std::cerr << "Could not access file" << std::endl;
  else{
    while(!fin.eof()){
        Customers *tempCustomer = new Customers;

        fin.getline(holder,MAX_SIZE,';');
        tempCustomer->groupName = holder;

        std::cout << "Group Name: " << tempCustomer->groupName << std::endl;
        fin.getline(holder,MAX_SIZE,';');
        tempCustomer->name = holder;

        fin.getline(holder,MAX_SIZE,';');
        tempCustomer->email = holder;


        fin >> tempCustomer->choice;
        fin.get(); //gets the last character, which is '\n'
        fin.ignore(); //ignores the next character which is the '\n'

        tempCustomer->next = NULL;

        std::cout << "What does the temp Node Store?" << std::endl;
        std::cout << "Group Name: " << tempCustomer->groupName << std::endl;
        std::cout << "Name: " << tempCustomer->name << std::endl;
        std::cout << "Email: " << tempCustomer->email << std::endl;
        std::cout << "Choice: " << tempCustomer->choice << std::endl;

        //addCustomerToLL(tempCustomer);
        tempCustomer = NULL;
        delete tempCustomer;

    }    
   }
   fin.close();
  }

Here is the Console out put:

Group Name: Jonathan Group
What does the temp Node Store?
Group Name: [email protected]
Name: [email protected]
Email: [email protected]
Choice: 2

Here is the text file customers.txt

Jonathan Group;Jonathan;[email protected];2

This is a school assignment, i'm to store all the customers from the text file into a linked list. I'm also to use c strings as strings rather than c++ version of strings. Let me know if the other files are necessary, i didnt include them since well nothing in this function utilize anything else outside the func besides the ifstream fin; private variable i have in the class and the const int MAX_SIZE = 256; global variable.

like image 628
Jonathan Vazquez Avatar asked May 04 '26 00:05

Jonathan Vazquez


1 Answers

Assuming you're not allowed to use std::string, you need to allocate memory for each string.

So replace this:

fin.getline(holder,MAX_SIZE,';');
tempCustomer->groupName = holder;

with:

fin.getline(holder, MAX_SIZE, ';');
char *s = new char[strlen(holder) + 1];
strcpy(s, holder);
tempCustomer->groupName = s;

You should release the memory you allocate when you no longer need it, so create a destructor for your Customers class:

Customers::~Customers()
{
    delete[] groupName;
}
like image 183
Sid S Avatar answered May 07 '26 03:05

Sid S