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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:20:09+00:00 2026-06-15T10:20:09+00:00

I have a read function, which reads external files and makes each name a

  • 0

I have a read function, which reads external files and makes each name a new node, and I’d like to build on the list that I’ve created, but somehow there’s like a gap in the list

example:
I enter in dave, jack, dog, cat. 

result:
name: dave
name: jack
name: dog
name: cat

it prints it out, and that’s the list right now. but If I close the program, and run it again, the read function reads back those names and creates the linked list again. But if I choose to add a another name “bob”. It puts it in the next node over

example:
I enter in bob

result:
name: dave
name: jack
name: dog
name: cat
name: [BLANK LINE]
name: bob

6.cpp

#include "6.h"

int main()
{
    petAdoption adopt;
    char response;
    adopt.read();
    do {

    adopt.enroll();
    adopt.display();
    cout << "Another? (y/n): ";
    cin >> response;
    cin.ignore(100,'\n');
    } while (toupper(response) == 'Y');

}

petAdoption::petAdoption()
{
head = NULL;
}

void petAdoption::enroll()
{
    char temp[TEMP_SIZE];
    cout << "Name: ";
    cin.getline(temp, TEMP_SIZE);
    pet.name = new char[strlen(temp)+1];
    strcpy(pet.name, temp);

    newNode = new animal;
    newNode->name = pet.name;
    newNode->next = NULL;

    if (NULL == head)
    {
        head = newNode;
        current = newNode;
    }
    else 
    {               
        current = head;
        while (current->next != NULL){
        current = current->next;
        }
        cout << "adding node to the end of the list" << endl;
        current->next = newNode;
        current = current->next;
    }
        ofstream write;
    write.open("pets.txt", ios::app);
        write << current->name << '\n'; 
    write.close();
}

void petAdoption::display()
{
    current = head;
    while (NULL != current)
    {
        cout << "Pet's name: " << current->name << endl;
        current = current->next;
    }
}

void petAdoption::read()
{
    ifstream read;  
    char temp[TEMP_SIZE];
    read.open("pets.txt");
        if(!read)
        {
            cout << "/#S/ There are no pets" << endl;
        }
        else{
            while(!read.eof())
            {
                read.getline(temp, TEMP_SIZE);
                pet.name = new char[strlen(temp)+1];
                strcpy(pet.name, temp);

                newNode = new animal;
                newNode->name = pet.name;
                newNode->next = NULL;

                if (NULL == head)
                {
                    head = newNode;
                    current = newNode;
                }
                else 
                {               
                    current = head;
                    while (current->next != NULL){
                        current = current->next;
                    }
                    current->next = newNode;
                    current = current->next;
                }
            }
        }
    read.close();
}

6.h

#include <iostream>
#include <cctype>
#include <cstring>
#include <fstream>
using namespace std;

const int TEMP_SIZE = 250;

struct animal
{
    char *name;
    animal *next;   
};

class petAdoption
{
    public:
    petAdoption();
    //~petAdoption();
    void enroll();
    void read();
    void display();
    private:
    animal *head, *current, *newNode;
    animal pet;
};
  • 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-15T10:20:11+00:00Added an answer on June 15, 2026 at 10:20 am
    while(!read.eof())
    {
        read.getline(temp, TEMP_SIZE);
        ...
    }
    

    is not the way to write a read file loop in C++. eof() only returns true after an attempt to read past the end of the file, so you execute the body of the loop one time too many.

    The correct way to write the loop is as follows:

    for (;;)
    {
        read.getline(temp, TEMP_SIZE);
        if (read.failed())
            break;
        ...
    }
    

    or more idiomatically:

    while (read.getline(temp, TEMP_SIZE))
    {
        ...
    }
    

    which means exactly the same thing.

    Note also that I have used failed() instead of eof() – end-of-file is not the only reason reading could fail.

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

Sidebar

Related Questions

I have read about dynamically loading your class files when needed in a function
Let's say I want to have a function which reads data from the SerialPort
I have an rss feed which i would like to read from in my
I am using IMoniker::BindToObject function, and I have read the article on MSDN. The
I need help with preg_match function in php. I have read a lot of
I have a simple file read and write function. private void WriteToFile(String filename, String
I just don't get it. I have a function to read stemmed strings from
I have a function that calls out a read or write request on a
i have this function pymssql.connect(host=my host,user=my user,password=my pass,database=mydb) I want to read the user
I read many articles about unsafe functions like strcpy, memcpy, etc. which may lead

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.