Switch Statements w/ Strings

Started by
2 comments, last by zippo 24 years ago
In a program I am writing up, I am using a switch statement to change a character array, which is holding a file name. As it goes through a loop, the variable being looped will change the file name, as illustrated below: switch (i) { case 0: FileName = "images/side2.bmp"; break; case 1: FileName = "images/side3.bmp"; break; case 2: FileName = "images/side4.bmp"; break; case 3: FileName = "images/side5.bmp"; break; case 4: FileName = "images/side6.bmp"; break; default: FileName = "images/side6.bmp"; break; } FileName, was declared as such: char FileName[] = "images/side1.bmp"; When I try this switch statement, my compiler tells me: error C2106: ''='' : left operand must be l-value I''ve tried a few other ways of representing what I want to do (for example, use FileName[] instead of FileName in my switch statement), but all to no avail. Can this not be done, or am I simply not looking at it correctly? Any advice or replies are welcome. Thanks! Z¡PPÕ
Advertisement
Hmm.. not much to go on to figure out your error. Maybe you could post a little more source. Make sure that FileName exists in the same scope as the switch statement is the only problem I can think of.

Also, you don''t want to use FileName = "images/side2.bmp";

You should include string.h and use this instead:
strcpy(FileName, "images/side2.bmp");

When you declare FileName, make sure you give it enough space for anything you might put in it later. Like so:

char File[32]="images/side1.bmp";

If you can''t figure it out, feel free to email me the code, I''ll try to help.

Jesse Chounard
stinkygoop@crosswinds.net
Kick ass. I used strcpy and it works fine now. I also noticed that I didn''t have string.h included to begin with. Perhaps this was also causing a dillema? Anyways, it works great now. I didn''t think to use strcpy, as I''m used to seeing it with two variables, as opposed to one variable, and a string that was written out. Thanks a lot for your help!

Not there, but getting there,

Z¡PPÕ
quote:Original post by zippo
switch (i)
{
case 0: FileName = "images/side2.bmp";
break;
case 1: FileName = "images/side3.bmp";
break;
case 2: FileName = "images/side4.bmp";
break;
case 3: FileName = "images/side5.bmp";
break;
case 4: FileName = "images/side6.bmp";
break;
default: FileName = "images/side6.bmp";
break;
}

FileName, was declared as such:

char FileName[] = "images/side1.bmp";

When I try this switch statement, my compiler tells me:

error C2106: ''='' : left operand must be l-value



Your problem is with your declaration of FileName. You''ve declared FileName as a character array, not as a character pointer. What that means is that you cannot change the value of FileName (in other words, you cannot change the address of the storage for that character array). My explanation is not very technical, hopefully you understand.

There are two ways to fix this. First is like this:

char *FileName = "images/side1.bmp";


Now FileName is a pointer and not the name of storage. (Sorry, I''m not making the distinction very well.)


The other way is to leave your declaration of FileName alone, but change your case statements to something like this:

case 0: strcpy(FileName, "images/side2.bmp");break;


However, this will be slower. And if you want to be safe when using the strcpy, you''ll probably want to change the declaration anyway to make sure there will be enough room:

char FileName[1024] = "images/side1.bmp";


I would also suggest taking a look at some C/C++ books to better understand the difference between all these things.



---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!

This topic is closed to new replies.

Advertisement