Jump to content
  • Advertisement

Archived

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

zippo

Switch Statements w/ Strings

This topic is 6926 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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Õ

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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Õ

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!