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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:56:58+00:00 2026-05-13T23:56:58+00:00

Ok, after failing to read a polynomial, I’m trying first a basic approach to

  • 0

Ok, after failing to read a polynomial, I’m trying first a basic approach to this.

So i have class polinom with function read and print:

#ifndef _polinom_h
#define _polinom_h

#include <iostream>
#include <list>
#include <cstdlib>
#include <conio.h>

using namespace std;

class polinom 
{
    class term
    {
    public:
        double coef;
        int pow;

    term(){
        coef = 0;
        pow = 0;
    }  
    };

list<term> poly;
list<term>::iterator i;

public:

void read(int id) 
{ 
    term t;
    double coef = 1;
    int pow = 0;
    int nr_term = 1;

    cout << "P" << id << ":\n";
    while (coef != 0) {
        cout << "Term" << nr_term << ": ";
        cout << "coef = "; 
        cin >> coef;
        if (coef == 0) break;
        cout << " grade = ";
        cin >> pow;

        t.coef = coef;
        t.pow = pow;
        if (t.coef != 0) poly.push_back(t);
        nr_term++;
    } 
}



    void print(char var) 
    { 
        for (i=poly.begin() ; i != poly.end(); i++ ) { //going through the entire list to retrieve the terms and print them  

            if (poly.size() < 2) {
                  if (i->pow == 0) //if the last term's power is 0 we print only it's coefficient 
                     cout << i->coef;

                  else if (i->pow == 1) {
                      if (i->coef == 1)
                          cout << var;
                      else if (i->coef == -1)
                          cout << "-" << var;
                      else 
                          cout << i->coef << var;
                  }

                  else
                     cout << i->coef << var << "^" << i->pow; //otherwise we print both 
            }

            else {
                if (i == poly.end()) { // if we reached the last term  
                    if (i->pow == 0) //if the last term's power is 0 we print only it's coefficient 
                        cout << i->coef;
                    else if (i->pow == 1)
                        cout << i->coef << var;
                    else
                        cout << i->coef << var << "^" << i->pow; //otherwise we print both 
                } 

                else { 
                    if (i->coef > 0) {
                        if (i->pow == 1)//if the coef value is positive  
                            cout << i->coef << var << " + "; //we also add the '+' sign 
                        else 
                            cout << cout << i->coef << var << "^" << i->pow << " + ";
                    }

                    else {
                        if (i->pow == 1)//if the coef value is positive  
                            cout << i->coef << var << " + "; //we also add the '+' sign 
                        else 
                            cout << cout << i->coef << var << "^" << i->pow << " + ";
                    }
            }
        }
    }
}

};


#endif   

Well, it works when reading only one term but when reading more the printed coefficients are some random values and also after the last term it print ‘+’ or ‘-‘ when it shouldn’t.

So any idea what’s wrong?

Thanks!

FINAL UPDATE

Ok, i made it work perfectly by modifying Bill’s code so thanks a lot Bill and everyone else who commented or answered!

Here’s the final print function:

   void print(char var)  
{  
 list<term>::iterator endCheckIter;  
 for (i=poly.begin() ; i != poly.end(); i++ )
{ 
     //going through the entire list to retrieve the terms and print them  
     endCheckIter = i; 
     ++endCheckIter;  

     if (i->pow == 0)
         cout << i->coef;
     else if (i->pow == 1)
         cout << i->coef << var;
     else         
         cout << i->coef << var << "^" << i->pow;

     if (endCheckIter != poly.end()) { 
         if (endCheckIter->coef > 0) 
             cout << " + "; 
         else {  
             cout << " - "; 
             endCheckIter->coef *= -1;
        }
    }
} 

}

  • 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-05-13T23:56:58+00:00Added an answer on May 13, 2026 at 11:56 pm
    if (i == poly.end()) { // if we reached the last term   
    

    This comment shows your error. For any given collection of items, items.end() returns the entry after the last item.

    For instance, say I have a 5-item std::vector:

    [0] [1] [2] [3] [4]
    

    Then begin() points to:

    [0] [1] [2] [3] [4]
    /\
    

    And end() points to:

    [0] [1] [2] [3] [4] []
                        /\
    

    Your for loop, it looks like:

    for (i=poly.begin() ; i != poly.end(); i++ )
    

    Note that comparing i to poly.end() happens before iter is used. As soon as i == poly.end(), you’re done.

    Your code inside of if (i == poly.end()) { will never be executed because this can never be true.

    You can test for the end using the following:

    // get access to the advance function
    #include <iterator>
    
    ....
    
    std::list<term>::iterator endCheckIter = i;
    std::advance(endCheckIter, 1);
    
    if (endCheckIter == poly.end())
    {
      ...
    }
    

    But a simpler way might be:

    std::list<term>::iterator endCheckIter = i;
    ++endCheckIter;
    if (endCheckIter == poly.end())
    {
      ...
    }
    

    Edit:
    I’m not sure why you’re getting garbage. Add in your missing braces and handle the non-end case, and everything works here:

    void print(char var)  
    {  
        list<term>::iterator endCheckIter;  
        for (i=poly.begin() ; i != poly.end(); i++ )
        { // <- MISSING BRACE
             //going through the entire list to retrieve the terms and print them  
             endCheckIter = i; 
             ++endCheckIter;  
    
             cout << i->coef << var << "^" << i->pow; // <- MISSING OUTPUT
             if (endCheckIter != poly.end()) { 
                 if (i->coef > 0) 
                     cout << " + "; 
                 else   
                     cout << " - "; 
            }
        } // <- MISSING BRACE
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After freezing edge rails, all my controller examples are failing with MissingTemplate errors. e.g.,
After reading the Head First Design Patterns book and using a number of other
After reading this question , I was reminded of when I was taught Java
After lots of attempts and search I have never found a satisfactory way to
After reading a bit more about how Gnutella and other P2P networks function, I
After a pull, git status tells me that I have changed a 16Mb xml
After the suggestion to use a library for my ajax needs I am going
After upgrading to the latest version of TortoiseSVN (1.5.2.13595), it's context menu is no
After being told by at least 10 people on SO that version control was
After hours of debugging, it appears to me that in FireFox, the innerHTML of

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.