By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,483 Members | 1,614 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,483 IT Pros & Developers. It's quick & easy.

compile time problem

P: n/a
I have a small problem I am trying to extract charcters from one string
store it in another and then compare the new string and out a msg. The
code looks fine but it always runs into a compiler problem. please help
here is the code:

// LexicalScanner.cpp : Defines the entry point for the console
application.
//
#include <iostream>
#include "stdafx.h"

using namespace std;

typedef struct {
enum {addop, mulop, number, identifier, lparen, rparen,
assignop, compop, string, resword, label, delim,
comma, error, eof} tokentype;
std::string token;
int ivalue;
std::string svalue;
int slen;
enum {iswrite, isread, isif, isthen, iselse, isendif,
isgoto, isnone} whichword;
int tosprec, inprec;
} TOKEN;

TOKEN Get_Token(string, int);

int main(int argc, char* argv[])
{
TOKEN t;
std::ifstream ifs("filename.txt");
std::string line;
int CountLine=1;
while (getline(ifs, line))
{
t=Get_Token(line,CountLine);
/* while(t.tokentype !=EOF)
{
printf("\tToken= \"%s", "type=", t.token);
switch(t.tokentype)
{
case addop: printf("Addop"); break;
case mulop: printf("Mulop"); break;
case number: printf("Number value=%d", t.ivalue); break;
case identifier: printf("Identifier"); break;
case lparen: printf("Lparen"); break;
case rparen: printf("Rparen"); break;
case assignop: printf("Assignop"); break;
case compop: printf("Compop"); break;
case comma: printf("Addop"); break;
case string: printf("String value=\"%s\" length=%d",t.svalue,t.slen);
break;
case resword: printf("resword");
switch(t.whichword)
{
case isinteger:printf("(integer");break;
case iswrite:printf("(write)");break;
case isread:printf("(read)");break;
case isif:printf("(if)");break;:
case isthen:printf("(then)");break;
case iselse:printf("(else)");break;
case isrendif:printf("(endif)");break;
case isnone:printf("(none)");break;
}
break;
case label: printf("Label value=\"%s\"",t.svalue);break;
case delim: printf("Delim"); break;
case error: printf("Error"); break;
case eof: printf("EOF"); break;
};
printf("\n");
t=Get_Token();*/
CountLine++;
}

return 0;
}

TOKEN Get_Token(string ln, int CLine)
{
TOKEN word;
std::string integer("integer");
cout<<CLine<<". "<<ln<<endl;
int strcounter = ln.length()-1;
for(int i=0;i<=strcounter;i++)
{
word.token[i]=ln[i];
if(word.token=="integer")
{
cout<<"found it!"<<endl;
}
}
return word;
}

Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"idikoko" <ni*****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
I have a small problem I am trying to extract charcters from one string
store it in another and then compare the new string and out a msg. The
code looks fine but it always runs into a compiler problem.
"Compiler problem" doesn't help describe your difficulty.
If you get diagnostic messages from your compiler, you
need to tell us what they are.

However I tried compiling your code, and the following
changes gave me a successful compile:
please help
here is the code:

// LexicalScanner.cpp : Defines the entry point for the console
application.
//
#include <iostream> #include "stdafx.h"
// #include "stdafx.h"
// (Omitted.)

#include <fstream>
#include <string>

// More below

using namespace std;

typedef struct {
enum {addop, mulop, number, identifier, lparen, rparen,
assignop, compop, string, resword, label, delim,
comma, error, eof} tokentype;
std::string token;
int ivalue;
std::string svalue;
int slen;
enum {iswrite, isread, isif, isthen, iselse, isendif,
isgoto, isnone} whichword;
int tosprec, inprec;
} TOKEN;

TOKEN Get_Token(string, int);

int main(int argc, char* argv[])
{
TOKEN t;
std::ifstream ifs("filename.txt");
std::string line;
int CountLine=1;
while (getline(ifs, line))
{
t=Get_Token(line,CountLine);
/* while(t.tokentype !=EOF)
{
printf("\tToken= \"%s", "type=", t.token);
switch(t.tokentype)
{
case addop: printf("Addop"); break;
case mulop: printf("Mulop"); break;
case number: printf("Number value=%d", t.ivalue); break;
case identifier: printf("Identifier"); break;
case lparen: printf("Lparen"); break;
case rparen: printf("Rparen"); break;
case assignop: printf("Assignop"); break;
case compop: printf("Compop"); break;
case comma: printf("Addop"); break;
case string: printf("String value=\"%s\" length=%d",t.svalue,t.slen);
break;
case resword: printf("resword");
switch(t.whichword)
{
case isinteger:printf("(integer");break;
case iswrite:printf("(write)");break;
case isread:printf("(read)");break;
case isif:printf("(if)");break;:
case isthen:printf("(then)");break;
case iselse:printf("(else)");break;
case isrendif:printf("(endif)");break;
case isnone:printf("(none)");break;
}
break;
case label: printf("Label value=\"%s\"",t.svalue);break;
case delim: printf("Delim"); break;
case error: printf("Error"); break;
case eof: printf("EOF"); break;
};
printf("\n");
t=Get_Token();*/
CountLine++;
}

return 0;
}

TOKEN Get_Token(string ln, int CLine)
{
TOKEN word;
std::string integer("integer");
cout<<CLine<<". "<<ln<<endl;
int strcounter = ln.length()-1;
// int strcounter = ln.length()-1;
string::size_type strcounter = ln.length()-1;
for(int i=0;i<=strcounter;i++)
// for(int i=0;i<=strcounter;i++)
for(string::size_type i=0;i<=strcounter;i++)
{
word.token[i]=ln[i];
if(word.token=="integer")
{
cout<<"found it!"<<endl;
}
}
return word;
}


Now whether or not this code actually does what you
intend, that's another story. :-)

-Mike
Nov 22 '05 #2

P: n/a
Thats a compile time problem alright.. I can know that by just reading
the title!

Nov 22 '05 #3

P: n/a
idikoko wrote:
You want to give us a hint as to the line and the error you detected?

typedef struct { .... } TOKEN;
This is silly C++. struct TOKEN { ... };

TOKEN Get_Token(string, int);

int main(int argc, char* argv[])
{
TOKEN t;
std::ifstream ifs("filename.txt");
std::string line;
int CountLine=1;
while (getline(ifs, line))
{
t=Get_Token(line,CountLine);
/* while(t.tokentype !=EOF)
What is the comment doing at the beginning of this line?
I can't find the matching close. #if is much better
for doing conditional compilation.

Another problem is that I think you probably wanted your
enumerator "eof" rather than "EOF" which is a macro in
the C standard IO libraries.

TOKEN Get_Token(string ln, int CLine)
{
TOKEN word;
std::string integer("integer");
cout<<CLine<<". "<<ln<<endl;
int strcounter = ln.length()-1;
for(int i=0;i<=strcounter;i++)
{
word.token[i]=ln[i];
This is invalid. word.token has zero size and you're sticking
in characters after the end. You need to append the characters
to the string:
word.token += in[i];
or something similar.
if(word.token=="integer")


This isn't very efficient. Even if "integer" is found, you'll
test "i" against "integer", "in" against integer, etc...
Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.