"if ....else...." compile error

1
2
3
4
5
6
7
8
9
10
11
12
        sfloat=strline.substr(18,8);
             
              if (scmd.compare("02") == 0) s+="读 ";
              else if (scmd.compare("00") == 0) s+="答 ";
              else    (scmd.compare("01") == 0) s+="写 ";
            
              s+=buffer;
              if      (stype.compare("00") == 0){s+="\n";continue;}
              else if (stype.compare("06") == 0) s+=" "+sint;
              else    (stype.compare("08") == 0) s+=" "+sfloat;
             
             s+="\n";


compile error:198 C:\Program Files\DEV-CPP\main.cpp expected primary-expression before "else"
compile error:198 C:\Program Files\DEV-CPP\main.cpp expected `;' before "else"

I don't know what wrong in the code.Can somebody show me the direction?
else statements don't have conditions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
sfloat=strline.substr(18,8);
             
              if (scmd.compare("02") == 0)
                  s+="读 ";
              else if (scmd.compare("00") == 0)
                  s+="答 ";
              else {
                  s+="写 "; }
            
              s+=buffer;
              if (stype.compare("00") == 0) {
                  s+="\n";
                  continue;
              }
              else if (stype.compare("06") == 0)
                  s+=" "+sint;
              else {
                  s+=" "+sfloat; }
             
             s+="\n";
Lines 5 and 10: It's illegal (and it doesn't make any sense) for elses to have conditions.
What is this code supposed to do anyway? I don't get it. Haha
thank you two.

Last edited on
What's the question?
Hang on. Let me go get my mind reading hat.
LOL @ helios
s="3301"
it is hexadecimal.it equal 0x0133 ;

I want to convert to decimal;

and get 307(0x133=307decimal)


but it has many error:
46 C:\Program Files\DEV-CPP\main.cpp `sout' was not declared in this scope
47 C:\Program Files\DEV-CPP\main.cpp expected constructor, destructor, or type conversion before '<<' token
47 C:\Program Files\DEV-CPP\main.cpp expected `,' or `;' before '<<' token
48 C:\Program Files\DEV-CPP\main.cpp expected constructor, destructor, or type conversion before '.' token
48C:\Program Files\DEV-CPP\main.cpp expected `,' or `;' before '.' token
49 C:\Program Files\DEV-CPP\main.cpp expected constructor, destructor, or type conversion before '.' token
50 C:\Program Files\DEV-CPP\main.cpp expected declaration before '}' token

do you think the information is right?
Last edited on
else;
This doesn't make any sense.
An else statement is written as such:
1
2
3
else {
    //code
}

I have delete "else;"

but it is still error;
Last edited on
Did you ever open the file so that you can close it?
void hex2dec(char *str,char* p)
If you want to modify p, pass it by reference or return the value.
1
2
void hex2dec(char *str,char* &p)
char * hex2dec(char *str)


1
2
3
 char buffer[6]={0};
//...
p=buffer;

buffer will be destroyed when you exit the function
Last edited on
Hi,ne555

I use void hex2dec(char *str,char* &p),it can be executed.

but when I cout<<buffer;cout<<s1;

they are different.why?

Last edited on
That's because buffer is destroyed at the end of the function, the memory is freeing, and now p point to garbage.
To fix that, either you allocate buffer dynamical, or use strcpy instead of assignment (make sure that s1 has enough space).
Wander wrote:
This doesn't make any sense.
An else statement is written as such:
Just so you know,
You can write them like that, if its one line

for example
1
2
3
int i = 0;
while(++i <= 10)
    cout << i;

would print i, 0 to 10 times
Last edited on
Hi,ne555

I replacep=buffer;with strcpy(p,buffer);but it still doesn't work.

I don't konw how to allocate dynamical array.so can you give me a easy way?
1
2
3
4
5
6
7
8
9
10
11
12
13
void hex2dec(char *str,char * const p){ //no changing the value of p
	//...
	strcpy(p, buffer);
	//...
}

int main() {
	char s1[10]; //Make sure you've got enough space
	//...
	hex2dec(s, s1);
	//...
	return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
char *hex2dec(char *str){
	char *buffer = new char[6]; //dynamic allocate space
	//...
	return buffer; 
}

int main(){
	char *s1;
	//...
	s1 = hex2dec(s);
	//...
	return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
char *hex2dec(char *str, char * &p){ //changing the value of p
	char *buffer = new char[6]; //dynamic allocate space
	//...
	p = buffer; 
}

int main(){
	char *s1;
	//...
	hex2dec(s, s1);
	//...
	return 0;
}
ne555,you will be a great man.thank you very much.
Topic archived. No new replies allowed.