3 Replies - 221 Views - Last Post: 05 February 2013 - 06:20 AM Rate Topic: -----

#1 BeanieBots  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-February 13

C++ Token Class Code Problem

Posted 04 February 2013 - 05:48 PM

I am currently in my second year for Game Software Development and we have been working with code in just about ever semester. I have had some experience with programming in C++ but the code that we have been working with has given me some problems, main because I haven't worked with if before and the "terms" used can just complicate things.

I'm currently working on an assignment with some signifcant code, however, the manner in which it is to be arranged in confuses me. The commented out code is the "Token Class" code given to us but I don't believe that it is correct.
This is my first post and tips/adive is greatly appreciated :)

#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <fstream>
#include <string>
#define VARIABLE		0x1
#define COEF			0x2
#define MULTIPLYDIVIDE	0x4
#define ADDSUB			0x8
#define XOR				0x10
#define AND				0x20
#define OR				0x40
#define NOT				0x80
#define PARENOPEN		0x100
#define PARACLOSE		0x200
#define EQUALS			0x400
#define NOTEQUALS		0x800
#define LESSTHAN		0x1000
#define GREATERTHAN		0x2000
#define LESSTHANEQUAL	0x4000
#define GREATERTHANEQUAL	0x8000
#define STATEMENT		0X10000
#define ASSIGN			0x20000

using namespace std;

class cFile
{
private:
	ifstream myfile;
	string nextToken;

public:
	bool Open(const wchar_t* FileName)
	{
		myfile.open(FileName);
		return myfile.is_open();
	}

	void Close(const wchar_t* FileName)
	{
		myfile.close();
	}

	bool eof()
	{
		bool bReturn = myfile >> nextToken;
		return bReturn;
	}

	string GetLine()
	{
		 return nextToken;
	}
};

class cToken
{
public:
	float fValue;
	int iType;

	cToken(float Value, int Type)
	{
		fValue = Value;
		iType = Type;
	}

	cToken()
	{
		fValue = 0;
		iType = 0;
	}

	~cToken()
	{
	};

	int SizeOf()
	{
		return sizeof(cToken);
	}

	cToken & operator= (const cToken & other)
	{
		if (this != &other)
		{
			fValue = other.fValue;
			iType = other.iType;
		}
		return *this;
	}
};

cToken TokensIn[1000];
int iTokens = 0;


void main(void) //(01/25/2013)
{
	string line;
	cFile oFile;
	oFile.Open(L"list.txt");
	ifstream myfile ("list.txt");

	if (myfile.is_open())
	{
		while (myfile.good())
		{
			getline (myfile, line);
			cout << line << endl;
		}
		myfile.close();
	}

	char Line[] = "x= 3*467;" ;
	int i;
	
	for (i=0; Line[i]!=0;)
	{
			/*
                          cToken myToken, anotherToken;         // The sections commented out here
	                  myToken.fValue = 3;                   // are the "Token Code" given to us.
                          myToken.iType = VARIABLE;             // We are suppose to make them work with
                        */                                      // our code.

		if (Line[i] == ' ')
		{
			i++;
		}
		else if (Line[i] == 'x')
		{
			printf("Token x %i\n", i);
			i++;
		}
		else if (Line[i] == '=')
		{
			printf("Token =\n");
			i++;
		}
		else if (Line[i] >= '0' && Line[i] <= '9')
		{
			/*                                           //
                          anotherToken = myToken;                    //
	                  TokensIn[iTokens] = myToken;               //
			*/                                           //

			int Temp, Numb = 0;
			while (Line[i] >= '0' && Line[i] <= '9')
			{
				/*                                   //
                                  TokensIn[iTokens].fValue = 4;      //
	                          TokensIn[iTokens].iType = XOR;     //
                                */

				Temp = Line[i] - '0';
				Numb = 10*Numb + Temp;  // build the number one place at a time.
				i++;
				iTokens++;
			}
			printf("Token %i\n", Numb);
		}
		else
		{
			i++;
		}
	}
	system("pause");
}

/*

list.txt               // this section in comments here is a txt file that is included
{                      // and gets printed out where it is called

print 32
print 3 * 4
print 2 + 3 * 4
print 2 * ( 3 + 4 * 5 )
print 32/8
print 32 * 8 /  8 -30
print 3 * 4 * 5

}

*/



Is This A Good Question/Topic? 0
  • +

Replies To: C++ Token Class Code Problem

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3461
  • View blog
  • Posts: 10,669
  • Joined: 05-May 12

Re: C++ Token Class Code Problem

Posted 04 February 2013 - 05:59 PM

What problem are you running into? It seems pretty straightforward that the tokens are to be used in an attempt to parse a string.
Was This Post Helpful? 0
  • +
  • -

#3 BeanieBots  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-February 13

Re: C++ Token Class Code Problem

Posted 04 February 2013 - 07:08 PM

The commented out Token code. I feel like it isn't placed properly because we're suppose to get the program to print out each token from the strings but when I run it, it just's prints out the text ".print [integers]..." followed by "Token: "

View PostSkydiver, on 04 February 2013 - 05:59 PM, said:

What problem are you running into? It seems pretty straightforward that the tokens are to be used in an attempt to parse a string.

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3461
  • View blog
  • Posts: 10,669
  • Joined: 05-May 12

Re: C++ Token Class Code Problem

Posted 05 February 2013 - 06:20 AM

Can you post your updated code with stuff uncommented out? The code you post above never prints out "Token:". Where did that come from?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1