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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:51:21+00:00 2026-05-26T05:51:21+00:00

update: added LinkedList.h and SymbolTable.h So I’m having difficulty with a programming assignment from

  • 0

update: added LinkedList.h and SymbolTable.h

So I’m having difficulty with a programming assignment from school. This is my first C++ course, the previous ones were taught in Java.

I created a linked list of SymbolTable objects and I need to retrieve the id (std::string) and value (int) from each when traversing the list, but any values I try to get just return null.

I think I’m missing something fundamental here, but not sure what.

this is the output I get, notice the blank line is from calling
cout << temp->data->getID() << endl;

I’m really at a loss of how to fix/get around this.

output:

C:\Users\spud\Documents\Visual Studio 2010\Projects\EECS1580_assign_01\Debug>EEC
S1580_assign_01.exe
Reading file...
Parsing...id_1 = 40
id_1
40

Parsing...var_2 = 50
var_2
50

Parsing...id_1 += 30
Parsing...var_2-=20
Parsing...id_1*=5
Parsing...k_27=80
k_27
80

Parsing...k_27 /= 20
done reading..
done

Here’s the code

LinkedList.cpp

#include "StdAfx.h"
#include "LinkedList.h"
#include <iostream> 
#include <string>
#include "SymbolTable.h"

using namespace std;


// define node for LinkedList
typedef struct node 
{ 
    public:
        SymbolTable *data;       // data in node 
        node *next;             // link to next node 
        node() 
        {
            next = NULL;
            data = new SymbolTable();
        }
}; 


node *head = NULL;               // empty linkedList
node *tail = NULL;
node *curr = NULL;


LinkedList::LinkedList() 
{
    head = new node();           
} 

LinkedList::~LinkedList() 
{ 
   node *q; 
   if( head == NULL ) 
        return; 

   while( head != NULL ) 
   { 
        q = head->next; 
      delete head; 
      head = q; 
   }                     
} 


// insert node at front of LinkedList
void LinkedList::insert(string id, int value) 

{ 
    SymbolTable *s = new SymbolTable(id, value);

    node *temp;                   //create a temporary node 
    temp = new node;              //allocate space for node 

    temp->data = s;               // store data(first field)
    temp->next = head;            // store the address of the pointer head(second field)
    head = temp;                  // transfer the address of 'temp' to 'head'
    cout << temp->data->getID() << endl;
} 

void LinkedList::display() 
{ 
     node *q; 
     cout << endl; 

     for(q = head; q != NULL; q = q->next) 
           cout << q->data->toString() << endl;
} 


void LinkedList::traverse() 
{ 

    node *curr;
    curr = head;
    SymbolTable *temp;

    if(head == NULL)
    cout << "End of list.\n";
    else    
    {
        while(curr != NULL) {
        //cout << temp->data.toString() << " ";
            temp->setID(curr->data->getID());
            temp->setValue(curr->data->getValue());
            cout << temp->getID() << endl;
            curr = curr->next;
        }
    }
} 

/*void find(string tofind, char op, int value)
{
     node *temp1;                               // create tmp node 
     temp1 = head;                              // transfer address of p to temp1 
     //string t = "";
     while(temp1 != NULL) 
     { 
        // t = temp1->data.getID();
        // if(tofind.compare(t) == 0)
         {
             cout << "match!" << endl;
        //   temp1->data.performOp(op, value);  // perform op
         }
        // temp1 = temp1->next;                   // transfer address of temp->next to temp             
     }     
     cout << "no match!" << endl;
}*/

void LinkedList::sort() 
{ 
    node *temp, *temp1, *temp2;
    //temp = new node;
    //temp1 = new node;
    //temp2 = new node;

    for(temp1 = head; temp1 != NULL; temp1 = temp1->next)
    for(temp2 = head; temp2 != NULL; temp2 = temp2->next)
    {
        if(temp1->data->toString() < temp2->data->toString())
        {
            temp->data = temp2->data;
            temp2->data = temp1->data;
            temp1->data = temp->data;
        }
    }
} 

SymbolTable.cpp

#include "StdAfx.h"
#include "SymbolTable.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdlib> 

using namespace std;

    string id;
    int value;
    int final;


    void SymbolTable::setID(string s)
    {
        id = s;
    }

    void SymbolTable::setValue(int a) 
    {
        value = a;
    }

    // Will perform the requested operation on the node
    int SymbolTable::performOp(char op, int val)
{
    // final value to return
    int final = 0;
    // determine which op to use, then exec it
        switch (op) {
            case '+':
                value += val; break;
            case '-':
                value -= val; break;
            case '*':
                value *= val; break;
            case '/':
                value /= val; break;
            case '%':
                value %= val; break;
            default:
                // no operation performed
                cout << "noOp" << endl;
                break;
        }

        cout << id << " New Value: " << value << endl;

        // return the new value
        return value;
}

    // returns a string with info about the node
    string SymbolTable::toString(void)
    {
        // create new string to return
        string mString;
        // append id and = to the string
        mString.append(id).append("=");
        // append in to string
        ostringstream oss;
        oss << value;
        mString += oss.str();
        cout << mString << endl;
        // return the final string
        return mString;
    }

    // return Id 
    const string SymbolTable::getID(void)
    {
        return id;
    }
    const int SymbolTable::getValue(void)
    {
        return value;
    }

    // return the link that this obj points to
    //SymbolTable* SymbolTable::getLink()
    //{
    //  return link;
    //}

    // set link
    //void SymbolTable::setLink(SymbolTable *s)
    //{
    //  link = s;
    //}

    // Constructor
    SymbolTable::SymbolTable(string a, int v)
    {

        // initialize the id
        id = a;
        cout << getID() << endl;
        // initialize the value
        value = v;
        cout << getValue() << endl;
        final = 0;
    }
    SymbolTable::SymbolTable()
    {
        id = "";
        value = 0;
    }

    // deConstructor
    SymbolTable::~SymbolTable(void)
    {
        // clear the string
        id.clear();
        // set value to 0
        value=0;
    }

Main

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iostream> 
#include <algorithm>
#include "SymbolTable.h"
#include "LinkedList.h"

using namespace std;

//SymbolTable *root = NULL;

LinkedList mylist;

void parse(string line) 
{ 
     int value;
     string svalue;
     string id;
     char op;

     cout << "Parsing..." << line << endl;

     // remove all spaces                                             
     for(int i=0; i<line.length(); i++)
         if(line[i] == ' ')
             line.erase(i,1);

     int eqSpot = line.find('=');                             // find the = 
     svalue = line.substr(eqSpot+1, line.length()-1);         // set value 
     switch(line.at(eqSpot-1)){                               // set op 
                      case '+':  op = '+';  eqSpot--; break; 
                      case '-':  op = '-';  eqSpot--; break; 
                      case '*':  op = '*';  eqSpot--; break; 
                      case '/':  op = '/';  eqSpot--; break;
                      default:   op = '=';  break;          
     };

     id = line.substr(0, eqSpot);                           // set id

     stringstream ss(svalue);                               // convert std::string svalue to int value
     ss >> value;

     if(op == '=')                                          // if decleration.. 
     { 
         // create new node in the LinkedList      
                mylist.insert(id, value);                           // insert s into LL
     } 
     else                                                   // else is operation 
     { 
               // myList.find(id, op, value);
     }
} 

/*// sorts the nodes in the LL
void sort()
{
    SymbolTable *temp1;
    SymbolTable *temp2;
    SymbolTable *temp;

    cout << "Sorting Nodes..." << endl;
    for( temp1 = root ; temp1!=NULL ; temp1 = temp1->getLink() )
    {
          for( temp2 = temp1->getLink() ; temp2!=NULL ; temp2 = temp2->getLink() )
          {
                if( temp1->getID() > temp2->getID() )
                {
                      temp = temp1->getLink();
                      temp1->setLink(temp2->getLink());
                      temp2->setLink(temp);
                }
          }
    }
}
// should print out the toString for each node
void printNodes()
{
    SymbolTable *node = root;

    do
    {
        cout << node->toString() << endl;
    }while((node = node->getLink()) != NULL);
}
// Will traverse the string, comparing IDs and will return a pointer to that node or null if not found
SymbolTable* traverse(string id)
{
     SymbolTable *temp1;                          // create tmp node 
     temp1 = root;

     while(temp1 != NULL) 
     {  
        cout << temp1->toString() << endl;
        if(id.compare(temp1->getID()) == 0)
        {
            // Found!
            cout << "MATCH" << endl;
            return temp1;
        };
        cout << root->getLink() << endl;
        if(temp1 == temp1->getLink())
            temp1 = NULL;
        else
            temp1 = temp1->getLink();                // transfer address of temp->next to temp 
     };

     cout << "NO MATCH FOUND" << endl;
     return NULL;

}
*/

// some test values
int _tmain(int argc, _TCHAR* argv[])
{
    // Read input
    mylist = LinkedList();
    string line;
    ifstream myfile ("input.txt");              // input file
    if (myfile.is_open())
    {
        cout << "Reading file..." << endl;
        while ( myfile.good() )                 // read file line by line
        {
            getline(myfile,line);
            if(line.compare("</end/>") == 0)    // end of file
                break;
            else
                parse(line);                    // parse the line
        }
        myfile.close();
        cout << "done reading.." << endl;
    }
    else cout << "Unable to open file"; 

    //myList.sort();

    //myList.traverse();

    cout << "done";
    //return 0;

    return EXIT_SUCCESS; 
} 

LinkedList.h

#pragma once
#include "SymbolTable.h"
#include <string>

class LinkedList
{
private: 

public:
    LinkedList(void);
    ~LinkedList(void);
    void insert(std::string id, int value); 
    void display(); 
    void traverse();
    void sort();
    //void find(std::string, char, int);
};

SymbolTable.h

#pragma once
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdlib> 

class SymbolTable
{
public:
    int performOp(char, int);
    std::string toString(void);
    const std::string getID(void);
    void setValue(int);
    void setID(std::string);
    const int getValue();
    SymbolTable(std::string, int);
    SymbolTable();
    ~SymbolTable(void);
};
  • 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-26T05:51:22+00:00Added an answer on May 26, 2026 at 5:51 am

    One problem is removing spaces in parse function

     // remove all spaces                                             
     for(int i=0; i<line.length(); i++)
         if(line[i] == ' ')
             line.erase(i,1);
    

    When space is removed index should not be incremented, so that you can remove the possible next space.

    But you main problem is having global variables that should have been members of SymbolTable:

    string id;
    int value;
    int final;
    

    Instead of this above you should have the following (in SymbolTable.h):

    class SymbolTable
    {
        string id;
        int value;
        int final;
    
        // declaration of all the member functions
        ...
    };
    

    Also, in this code you don’t need destructor (the one you call deConstructor). All members will be automatically deleted when the object is deleted itself. You only have to worry about deleting objects of which you only have a pointer.

    Global variables that pertain to LinkedList should also be members of that class, instead of being global:

    class LinkedList
    {
        node *head;
        node *tail;
        node *curr;
    
        // declaration of all the member functions
        ...
    };
    

    And their initialization should be done in constructor:

    LinkedList::LinkedList()
    {
        head = NULL;               // empty linkedList
        tail = NULL;
        curr = NULL;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: Modified title and added update. UPDATE : We no longer believe this is
I added a counter cache but can't get it to update. But I can
NOTE: I added my new solution at the UPDATE answer below. I try to
I'm using an Ajax update panel and have recently added ASP.NET tracing code to
There're files/folders that were deleted and then re-added to the repository. svn update >
In my CMS I've added this code <div><?php include(my_contact_form.php) ?></div> which updates to a
Update: Added Schema to the bottom... I have a table of contracts: tbl_contract And
Update Added jsfiddle - see bottom of post I currently have a function that
I am having a really hard time trying to get this to work. All
UPDATE : added an example to clarify the format of the data. Considering a

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.