GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Jun-2005, 17:39
rho rho is offline
New Member
 
Join Date: Jun 2005
Posts: 12
rho is on a distinguished road

Need help with enums and function program


hello. i am writing a program where it will read data from a file. and each token read will be determine to be a enum type of either IDENTIFIER, ARITHMETIC, ASSIGNMENT, or ILLEGAL. the program will then print out each token and the corresponding enum type for it. each type has logical rules and boundaries. the data being read is:

area = pi * r * r;
_perimeter = 2 *( l + w);
volume += 2pi r times r square_ ;
//Step 9.a
switch (transactionCode)
{
case 'd' : interest = princ_ipal * 1.06; + transactionAmount;
amountDeposited = amountDeposited + transactionAmount;

__numberOfDeposits ++;
break;
1default;
}


i have stored this project in a file called data.txt. and here is my program so far. it determines each token and the type of token correctly. however. after running, it only shows the token enum type and not the token.

here is my code:

CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

enum tokenType {Identifier , Arithmetic , Assignment , Illegal};
tokenType ReadAndDetermineTokens(ifstream & inFile);
void getTokenAndTokenType();

int main()
{
    getTokenAndTokenType();

    return 0;
}
//-------------------------------------------------------------------------------
tokenType ReadAndDetermineTokens(ifstream & inFile)
{
    string token;
    tokenType tType;

    inFile >> token;
    char word;
    char letter;
    int size = token.length();
    int counter = 0;
    int i;

	if (size == 1)
	{
		letter = token[0];
		
		if (letter == '*' || letter == '+' || letter == '-' || letter == '/')
		tType = Arithmetic;

                else if (letter == '=')
		tType = Assignment;

		else if (isalpha(letter))
		tType = Identifier;

		else 
		tType = Illegal;
	}
		
	else if (size > 1)
	{
    	for (i=0; i < size; i++)
        {
            word = token[i];
               
	    if (i == 0)
            {
                if (token[i]== '_' || isalpha(word))
	        counter++;
            }
                
            else
            {
                if (token[i]== '_' || isalnum(word))
	        counter++;
            }
	}
		    
	if (size == counter)
        tType = Identifier;
            
	else
        tType = Illegal;
	}
    return tType;
}
	
//-------------------------------------------------------------------------------
void getTokenAndTokenType()
{
	int identifierCount = 0;
	int arithmeticCount = 0;
	int assignmentCount = 0;
	int illegalCount = 0;
	string token;
	
	tokenType tType;
	ifstream inFile;
	inFile.open ("data.txt");

        cout << fixed << setfill(' ');
	cout << left << setw(30) << "Token";
	cout << left << setw(20) << "Type of Token" ; 
	cout << endl << '\n' << '\n';
    
    while (!inFile.eof())
    {
        tType = ReadAndDetermineTokens(inFile);
	
	    switch (tType)
	    {
	    case Identifier: cout << left << setw(30) << token 
				        << left << setw(20) << "identifier" 
					<< endl << endl;
				 identifierCount++;
				 break;
	
	    case Arithmetic: cout << left << setw(30) << token 
					  << left << setw(20) << "aritmetic" 
					  << endl << endl;
			           arithmeticCount++;
				   break;
	
	    case Assignment: cout << left << setw(30) << token 
				           << left << setw(20) << "assignment" 
				           << endl << endl;
			             assignmentCount++;
				     break;
	
	    case Illegal:	 cout << left << setw(30) << token 
					   << left << setw(20) << "illegal" 
				           << endl << endl;
				    illegalCount++;
				    break;
	    }
    }

	cout << '\n';
	cout << "There were " << identifierCount << " identifer(s)" << endl << endl;
	cout << "There were " << arithmeticCount << " aritmetic operator(s)" << endl << endl;
	cout << "There were " << assignmentCount << " assignment operator(s)" << endl << endl;
	cout << "There were " << illegalCount << " illegal operator(s)" << endl << endl;
}

the problem is the output which looks something like

Token Token type

area identifier

= assignment

pi identifier

* arithmetic

r identifier

and so on....

however it's outputting this:

Token Token type

identifier

assignment

identifier

arithmetic

identifier


where the type of token is being displayed but no token itself.
does anyone know what the error is?
thank you for all your time

by the way. this is intro c++. and i'm using visual studio
  #2  
Old 27-Jun-2005, 17:50
Kacyndra's Avatar
Kacyndra Kacyndra is offline
Member
 
Join Date: May 2005
Location: Maryland
Posts: 230
Kacyndra will become famous soon enough
i dont even get output when i run it, but then again im using a very very old C++ compiler right now


i get this:
Token Type of Token
__________________
Xrum!
  #3  
Old 27-Jun-2005, 18:35
rho rho is offline
New Member
 
Join Date: Jun 2005
Posts: 12
rho is on a distinguished road
hmm....well. i'm thinkin it's something to do with the "infile >> token" i have in my tokenType ReadAndDetermineTokens(ifstream & inFile) function. all i know right now is that it's definitely an error within my 2 functions. i mean, there's really nothing else. but i still don't understand where an error might be. do you see any errors going through it? i'm pretty sure it has something to do with reading the file or infile-ing the token. maybe i put the infile >> token in the wrong place?

thank you so much for helping :-)
  #4  
Old 27-Jun-2005, 18:50
Kacyndra's Avatar
Kacyndra Kacyndra is offline
Member
 
Join Date: May 2005
Location: Maryland
Posts: 230
Kacyndra will become famous soon enough
i think i know what it is,
You need to ReadData, and DetermineToken seporatly.

Basically you read data, and return a token(not the TokenType)
than you use that to DetermineToken(to see whcih one it is, and retuern that)... i think

does that make sense?
__________________
Xrum!
  #5  
Old 27-Jun-2005, 18:59
rho rho is offline
New Member
 
Join Date: Jun 2005
Posts: 12
rho is on a distinguished road
hmm... well the way i have it set up. is that the getTokenAndTokenType() function reads the file in a loop. in my getTokenAndTokenType(), i read the file as a loop, and call the function ReadAndDetermineTokens() within the loop, so that each token read can go through the conditions, and then back into getTokenAndTokenType() to increment each token type count as well as print to screen. i mean it reads the tokens, goes through the conditions in ReadAndDetermineTokens() correctly, goes back into getTokenAndTokenType() and correctly increments the token type counters. but it's just not printing the token. i guess it's a little misleading for the function name getTokenAndTokenType() because i made a change and had it indeed read and determing tokens separately instead of in the same function. now it reads in getTokenAndTokenType(), and determines in ReadAndDetermineTokens().
so what it comes down to is....i don't really understand what you're telling me to do means.
  #6  
Old 27-Jun-2005, 19:01
rho rho is offline
New Member
 
Join Date: Jun 2005
Posts: 12
rho is on a distinguished road
by the way. it is required i use enums. that's why i have to do it like this. otherwise i'd do everything in main. and i'd be done pretty fast haha.
again. i really appreciate the help. thank you so much. i'm struggling with this one as well as my other project in my other thread. it's the same problem as harleyrider has. he's in my class. haha. everything is due tomorrow and i'm panicking. plus final is tomorrow too!!! ??:
  #7  
Old 27-Jun-2005, 19:02
Kacyndra's Avatar
Kacyndra Kacyndra is offline
Member
 
Join Date: May 2005
Location: Maryland
Posts: 230
Kacyndra will become famous soon enough
if yur cout<< statments,
your showing the tokenType.. is left the token itself?
are you asking how to pass the symbol itself though, or why it's now showing?
__________________
Xrum!
  #8  
Old 27-Jun-2005, 19:04
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about
Quote:
Originally Posted by rho
where the type of token is being displayed but no token itself.
does anyone know what the error is?
thank you for all your time

by the way. this is intro c++. and i'm using visual studio
Well I see this in your function "getTokenAndTokenType",
CPP / C++ / C Code:
  int illegalCount = 0;
  string token;
Then I see this,
CPP / C++ / C Code:
      case Identifier: cout << left << setw(30) << token 
                << left << setw(20) << "identifier" 
          << endl << endl;
         identifierCount++;
         break;
You use "token" in the function "getTokenAndTokenType" but I do not see any code where you ever assign anything to it?

One way to fix this might be to pass the string "token" to your function,
CPP / C++ / C Code:
tokenType ReadAndDetermineTokens(ifstream & inFile, string &token);
then call it from "getTokenAndTokenType" like this ,
CPP / C++ / C Code:
tType = ReadAndDetermineTokens(inFile, token);
and comment this out in "ReadAndDetermineTokens",
CPP / C++ / C Code:
    //string token;
    tokenType tType;
  #9  
Old 27-Jun-2005, 19:13
rho rho is offline
New Member
 
Join Date: Jun 2005
Posts: 12
rho is on a distinguished road
THANK YOU GUYS SOO MUCH!!! YES!! thanks especially to sokar. suggestion worked perfectly!! of course thank you too kacyndra!! thanks guy for the time and help. nicee, i got one down , another to go now.... :-( haha. any help with that one too perhaps haha it's under need help with simple 1D array thread. lol, that's if you have nothing else to do of course, no obligation to help of course. again, thanks so much
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can enum have same name as class? crystalattice C++ Forum 3 08-Dec-2004 16:43

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:11.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.