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

  • Home
  • SEARCH
  • 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 8588231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:45:31+00:00 2026-06-11T22:45:31+00:00

I am working on an assignment which to create a program that can read

  • 0

I am working on an assignment which to create a program that can read a polynomial from a text file using linked list. Which when i tried to return the starting pointer of the linked list “poly_pointer” from the read_poly function things being weird.

The expected output should be -12

But what i got is -10

And if i add one single line of code right before return in read_poly

cout << curr_ptr->coef;

the output would suddenly turns to 2-12
May anyone provide some explanation on why and how to fix this problem?

Polynomial.h

#ifndef _POLYNOMIAL_H_
#define _POLYNOMIAL_H_

using namespace std;

typedef struct poly_node *poly_pointer;

typedef struct poly_node {
  int coef;
  int expon;
  poly_pointer link;
};

poly_pointer addNode(int coef, int expon);

#endif

Polynomial.cpp

#include <iostream>
#include <fstream>
#include <string>
#include "Polynomial.h"
using namespace std;

poly_pointer addNode(int coef, int expon)
{
    poly_node a;
    poly_pointer ptr = &a;
    a.coef = coef;
    a.expon = expon;
    return ptr;
}

poly_pointer read_poly(const char* fileName)
{
    poly_pointer start_ptr, curr_ptr;
    start_ptr = curr_ptr = addNode(-1, 6);
    curr_ptr = curr_ptr->link = addNode(2, 3);
    return start_ptr;
}

main.cpp

#include <iostream>
#include "Polynomial.h"
using namespace std;

int main(void)
{
    poly_pointer a, b, d, e, f;
    a = read_poly("input1.txt");
    cout << a->coef;
    cout << a->link->coef;

    cout << "\n-eop-";
    cin.get();
    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-06-11T22:45:32+00:00Added an answer on June 11, 2026 at 10:45 pm

    addNode is returning a pointer to a locally allocated poly_node.

    The following is sloppy allocation of memory, but will work.

      poly_pointer a = new poly_pointer();
      a->coef = coef;
      a->expon = expon;
      return a;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an assignment that deals with reading data from a text
Currently I'm working on an assignment that requires that I create a program in
I'm working on Polynomial Transform for a homework assignment. I'm using a document from
I'm working on a homework assignment (a project), for which one criterion is that
Greetings I've been working on a homework in which I must create a linked
I'm working on assignment here, part of which requires me to parse integers from
As part of an assignment I need to create a shipping program that checks
I'm working on a homework assignment in which I'm required to use char arrays
I'm working on my assignment of PHP course. The problem that I don't understand
I am working on an assignment for networking where we are supposed to create

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.