1 Replies - 220 Views - Last Post: 13 December 2018 - 06:53 AM

#1 adamchalkley   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 17-July 15

Bug which doesn't make sense

Posted 13 December 2018 - 06:46 AM

hey guys,

I've got a bug that just doesn't make sense to me so I'm printing out the Tokens types in the main function,when I don't have the debugging "cout << "here cons" << endl;" line it will not print the type instead just prints a blank character but when I include this line it prints the correct type.This is kind of baffling.

*edit come to think of it only Token one and three are displaying the correct type,Token Two is displaying a random character and it all depends on how long my debugging cout in the constructors with two arguments is,very strange


#include <iostream>
#include <vector>

using namespace std;


class Token
{

public:
    int number;
    char type;

    Token()
    {
        type = 'f';
    }

    Token(int number,char type)
        : number(number),type(type)
    {
        cout << "here cons" << endl; // when I keep this line of code it seems to work but when I remove it it stops working

    }
    Token(char type)
    : type(type)
    {

        number = 0;
    }
};


class TokenStream
{

public:
    Token buffer;
    bool bufferFull;

    TokenStream()
    {
        buffer = NULL;
        bufferFull = false;

    }

    Token& getToken()
    {

        if(bufferFull)
        {
            cout << "return buffer" << endl;
            bufferFull = false;
            return buffer;
        }

        char value;

        cin >> value;

        switch(value)
        {


        case '+':
        {
            cout << "here plus" << endl;
            Token t(value);
            return t;
        }

        case '5':
        {
            cout << "here" << endl;
            cin.putback(value);
            int num;
            cin >> num;
            cout << "num" << num << endl;
            Token t(num,'8');
            return t;
        }

        }
    }

    void putBack(Token& t){

       if(bufferFull){

          return;
       }
       bufferFull = true;
       buffer = t;

    }


};

vector<Token> tokens;

void populateTokens()
{

    cout << "enter expression";

}


int main()
{

    cout << "enter expression" << endl;

    TokenStream ts;

    Token one = ts.getToken();
    Token two = ts.getToken();
    Token three = ts.getToken();

    cout << "one type :" << one.type << endl;
    cout << "two type :" << two.type << endl;
    cout << "three type :" << three.type << endl;

}

This post has been edited by adamchalkley: 13 December 2018 - 06:50 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Bug which doesn't make sense

#2 adamchalkley   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 17-July 15

Re: Bug which doesn't make sense

Posted 13 December 2018 - 06:53 AM

Stupid me,I think I know the error,the reason I was getting undefined behaviour was because I was returning a reference to an object that went out of scope so one two and three were pretty much invalid,thats why they were printing garbage right?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1