it looks right...

Started by
8 comments, last by redneckCoder 21 years, 11 months ago
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
C:DOSC:DOSRUNRUNDOSRUN-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
Advertisement
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
Forever trusting who we areAnd nothing else matters - Metallica
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
C:DOSC:DOSRUNRUNDOSRUN-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
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
Forever trusting who we areAnd nothing else matters - Metallica
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]
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]
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
C:DOSC:DOSRUNRUNDOSRUN-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
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! ]
Ok, same program, new problem. What''s wrong with this line:

char *fileRead;

while(fscanf(someFile[0], "%c", &fileRead) != EOF)

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
C:DOSC:DOSRUNRUNDOSRUN-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
== is for std::string
for Char try using ... strcmp();

This topic is closed to new replies.

Advertisement