Thread: Print problem

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    17

    Print problem

    I'm trying to figure what the problem might be with this. It is suppose to print a list of the tokens for what ever file is input but it won't.
    Code:
    #include <iostream>
     #include <fstream>
     #include <string>
     #include <ctype.h>
    
     using namespace std;
    
     typedef enum { ENDFILE, ERROR,IF, THEN, ELSE, END, REPEAT, UNTIL, READ, WRITE,ID, NUM,
          ASSIGN, EQ, LT, PLUS, MINUS, TIMES, OVER, LPAREN, RPAREN, SEMI
         } TokenType;
    
     ofstream wfile;
    
     void printToken( TokenType token, char arg[], ostream &outFile)
     { switch (token)
       { case IF:      outFile << "reserved word: " << arg << endl; break;
         case THEN:    outFile << "reserved word: " << arg << endl; break;
         case ELSE:    outFile << "reserved word: " << arg << endl; break;
         case END:     outFile << "reserved word: " << arg << endl; break;
         case REPEAT:  outFile << "reserved word: " << arg << endl; break;
         case UNTIL:   outFile << "reserved word: " << arg << endl; break;
         case READ:    outFile << "reserved word: " << arg << endl; break;
         case WRITE:   outFile << "reserved word: " << arg << endl; break;
         case ASSIGN:  outFile << ":=\n"; break;
         case LT:      outFile << "<\n"; break;
         case EQ:      outFile << "=\n"; break;
         case LPAREN:  outFile << "(\n"; break;
         case RPAREN:  outFile << ")\n"; break;
         case SEMI:    outFile << ";\n"; break;
         case PLUS:    outFile << "+\n"; break;
         case MINUS:   outFile << "-\n"; break;
         case TIMES:   outFile << "*\n"; break;
         case OVER:    outFile << "/\n"; break;
         case ENDFILE: outFile << "EOF\n"; break;
         case NUM:     outFile << "NUM, val= " << arg << endl; break;
         case ID:      outFile << "ID, name= " << arg << endl; break;
         case ERROR:   outFile << "ERROR: \n"; break;
         default:      outFile << "Unknown token: \n",token;
         arg = NULL;
       }
     }
    
    
     int main ( void )
     {
        char junk;
        char ch;
        char read[80];
        char write[80];
        char tokString[256];
        int lineno = 0;
        int tokenIndex = 0;
        
    cout<<"\nEnter source file name: " << endl;
    cin >> read;
    
        ifstream rfile ( read );
        if (! rfile.is_open())
           {
           cout << "Error opening file";
           exit (1);
           }
    
        cout<<"\nEnter destination file name: " << endl;
        cin >> write;
    
        ofstream wfile ( write );
        if (! wfile.is_open())
           {
           cout << "Error opening file";
           exit (1);
           }
    
       TokenType currentToken;
    
        while( !rfile.eof() )
           {
           ch = rfile.get();
    
           if(isdigit(ch))
              {
              rfile.unget();
    
              while(isdigit(ch))
                 {
                 ch = rfile.get();
                 tokString[tokenIndex++] = ch;
                 }
    
              currentToken = NUM;
              } // if(isdigit(ch))
           else if(isalpha(ch))
              {
              rfile.unget();
    
              while(isalpha(ch))
                 {
                 ch = rfile.get();
                 tokString[tokenIndex++] = ch;
                 }
    
              if(strcmp(tokString, "IF")==0 || strcmp(tokString,"if"))
                 {
                 currentToken = IF;
                 }
              if(strcmp(tokString, "THEN")==0 || strcmp(tokString,"then"))
                 {
                 currentToken = THEN;
                 }
              if(strcmp(tokString, "ELSE")==0 || strcmp(tokString,"else"))
                 {
                 currentToken = ELSE;
                 }
             if(strcmp(tokString, "END")==0 || strcmp(tokString,"end"))
                 {
                 currentToken = END;
                 }
              if(strcmp(tokString, "REPEAT")==0 || strcmp(tokString,"repeat"))
                 {
                 currentToken = REPEAT;
                 }
              if(strcmp(tokString, "UNTIL")==0 || strcmp(tokString,"until"))
                 {
                 currentToken = UNTIL;
                 }
              if(strcmp(tokString, "READ")==0 || strcmp(tokString,"read"))
                 {
                 currentToken = READ;
                 }
              if(strcmp(tokString, "WRITE")==0 || strcmp(tokString,"write"))
                 {
                 currentToken = WRITE;
                 }
             else
                 {
                 currentToken = ID;
                 }
              } // if(isalpha(ch))
           else if( ch == ':' )
              {
              ch = rfile.get();
              currentToken = ASSIGN;
              }
           else if( ch == '{' )
              {
              while(isalpha(ch))
                 {
                 ch = rfile.get();
                 tokString[tokenIndex++] = ch;
                 }
              } 
           else
              {
              switch (ch)
                 {
                 case '\n':
                    lineno++;
                    break;
                 case EOF:
                    currentToken = ENDFILE;
                    break;
                 case '=':
                    currentToken = EQ;
                    break;
                 case '<':
                    currentToken = LT;
                    break;
                 case '+':
                    currentToken = PLUS;
                    break;
                 case '-':
                    currentToken = MINUS;
                    break;
                 case '*':
                    currentToken = TIMES;
                    break;
                 case '/':
                    currentToken = OVER;
                    break;
                 case '(':
                    currentToken = LPAREN;
                    break;
                 case ')':
                    currentToken = RPAREN;
                    break;
                 case ';':
                    currentToken = SEMI;
                    break;
                 } // switch (ch)
              } // else
    
        //   cout << "token read" << endl;
           printToken( currentToken, tokString, wfile);
           tokenIndex = 0;
           } // while( !rfile.eof() )
    
        //cout << "\nScan is done Hit Enter" << endl;
        //cin >> junk;
    
        /* close files */
        rfile.close();
        wfile.close();
    
        return 0;
     }
    Mod edit:
    Next time, use the [code][/code] tags

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > if(strcmp(tokString, "IF")==0 || strcmp(tokString,"if"))
    Shouldn't this be:
    Code:
     if(strcmp(tokString, "IF")==0 || strcmp(tokString,"if")==0)
    You have several like this.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    BTW, you #include <string>, but you use C style strings and C style string functions like strcmp. The correct header for those would be <cstring> (or <string.h>). Of course, you should probably be using C++ strings instead, in which case you would leave <string>.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
         default:      outFile << "Unknown token: \n",token;
         arg = NULL;
       }
    guess you want
    Code:
         default:      outFile << "Unknown token: " << token << "\n";
         arg = NULL; break;
       }
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem I Can't solve...
    By ferniture in forum C Programming
    Replies: 3
    Last Post: 07-15-2008, 02:51 AM
  2. Array print problem
    By nixonbw in forum C Programming
    Replies: 2
    Last Post: 03-19-2008, 09:17 AM
  3. Print out first N lines
    By YoYayYo in forum C Programming
    Replies: 1
    Last Post: 02-21-2008, 12:58 AM
  4. Problem With search a file and printing to screen
    By sell682 in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2004, 05:55 AM
  5. print problem
    By maloy in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2002, 10:43 PM