• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# it looks right...

## Recommended Posts

Ok, I have this if loop in my program:

extern int symbol2Int(char *symbol) {
char *character;
int charAsNum;
character = symbol;
for(int n=0;n<255;n++) {
if(symbolSet[n] == character) { //<<this line is bad

charAsNum = n;
}
}
return(n);
}

Now, when I compile, these are the only two errors I ever get: C:\programming\bored\main.cpp(17) : error C2446: ''=='' : no conversion from ''char *'' to ''int'' This conversion requires a reinterpret_cast, a C-style cast or function-style cast C:\programming\bored\main.cpp(17) : error C2040: ''=='' : ''int'' differs in levels of indirection from ''char *'' Error executing cl.exe. And has the little pointer that shows which line is erroneous on the line that is commented in the code. I know this is similar to the recent thread about changing ints to chars, but I couldn''t find what I was looking for on that thread, so here I am, lol. What''s wrong with that code? Thanks. -AJ C:\DOS C:\DOS\RUN RUN\DOS\RUN -Comic Book Store Guy''s t-shirt that I saw on the Simpsons, although it didn''t actually come from the Simpsons. http://vdsoft.netfirms.com/home.html

##### Share on other sites
I''m not sure what this function is supposed to do. If symbol points to one char (as opposed to a zero terminated string), then:

int i = (int)(*symbol);

does the job.

If symbol points to a string containing the "ASCII" representation of an integer, than

int i;
sscanf("%d", symbol, &i);

does the job.

Forever trusting who we are
And nothing else matters
- Metallica

##### Share on other sites
Well the problem is that, looking at it, there are no conflicts of data types.

Here''s the function again so you don''t have to scroll back up:

  char symbolSet[255]; //<<was not in originally post functionextern int symbol2Int(char *symbol) {	char *character;	int charAsNum;	character = symbol;	for(int n=0;n<255;n++) {		if(symbolSet[n] == character) {//<<erroneous line			charAsNum = n;		}	}	return(n);}

now, character and symbol are boths chars, nor problem. symbolSet[n] and character are both chars, again no problem. Same goes for charAsNum and n . There isn''t any apparent conflict of data type, so what''s wrong? Thanks.

-AJ

C:\DOS
C:\DOS\RUN
RUN\DOS\RUN

-Comic Book Store Guy''s t-shirt that I saw on the Simpsons, although it didn''t actually come from the Simpsons.

http://vdsoft.netfirms.com/home.html

##### Share on other sites
quote:
now, character and symbol are boths chars

Nope. character is a pointer to char . symbolSet[n] is, indeed a char.

Forever trusting who we are
And nothing else matters
- Metallica

##### Share on other sites
There is actually a type conflict, since symbolSet[n] is a char but character is a char*. But what do you actually want to do?

been to slow

[edited by - VolkerG on May 3, 2002 5:19:56 PM]

##### Share on other sites
You might want to just pass a char (by value) to the function. If for some reason you need to pass the pointer to a character, I think you could do the comparison as
(symbolSet[n] == *character)
(*character refers to the char value pointed to by the pointer.)

[edited by - Miserable on May 3, 2002 5:24:55 PM]

##### Share on other sites
Thanks for the replies guys, but I went ahead and tried something and it worked. I changed the char to an int first with atoi() and then did my comparison and it worked fine. Thanks.

-AJ

C:\DOS
C:\DOS\RUN
RUN\DOS\RUN

-Comic Book Store Guy''s t-shirt that I saw on the Simpsons, although it didn''t actually come from the Simpsons.

http://vdsoft.netfirms.com/home.html

##### Share on other sites
It''s not clear why you are doing this, but the algorithm you are implementing can be improved by making symbolSet an int rray, and using the value held in *character as in index into the array. Thus you can have:
char character = some_value;int charAsNum = symbolSet[character];

I''m a bit bamboozled by your use of char*, as I suspect you might be trying to use a c-string instead of a single char. If that''s the case, you need to restate your problem.

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]

##### Share on other sites
Ok, same program, new problem. What''s wrong with this line:

Thanks.

-AJ

C:\DOS
C:\DOS\RUN
RUN\DOS\RUN

-Comic Book Store Guy''s t-shirt that I saw on the Simpsons, although it didn''t actually come from the Simpsons.

http://vdsoft.netfirms.com/home.html

##### Share on other sites
== is for std::string
for Char try using ... strcmp();

• ### Forum Statistics

• Total Topics
627708
• Total Posts
2978732

• 21
• 14
• 12
• 22
• 35