Computing.Net > Forums > Programming > C++ major probs

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ major probs

Reply to Message Icon

Name: kimib
Date: November 12, 2004 at 13:58:10 Pacific
OS: XP
CPU/Ram: 2.8GIG 512ram
Comment:

I have just started learning C++ and for my first assignment i'm using a function of a switch statement which includes strings... i read somewhere on the internet that this can create a bug?!?!?

i just want to be able to use my program.

I'm using dev- c++ bloodshed

char func(int month)
{char output_month[4];
switch(month)
{
case 1:
strcpy(output_month,"Jan");
break;
case 2:
strcpy(output_month,"Feb");
break;
......etc....

default:

/* Do nothing */;

}
return output_month[4];
}

i recall it by using

cout << func(month1)
it compiles ok, but doesn't display the string of characters.. only weird symbols?!?!


anyone that could suggest a way to beat it, or a SIMPLE alternative would receive my greatest thanx! Kim




Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: November 12, 2004 at 15:35:14 Pacific
Reply:

You have four problems.

First two problems are in the line:

return output_month[4];

1-You declared a 4 element array, but are telling it to access the 5th element of the array. Remember that the first element is at output_month[0]. So you are accessing memory that is outside the array (C & C++ will let you do that).

2-By putting the [ ] on output_month, you are telling it to return only one character, the character at array position 4 (in your code). You want to return the whole string, string, so don't tell it to return a single character, but tell it to return the whole string by ...

3-Since you want to return the whole string (which is a char*) rather than a single character, you want to change your function definition to return a char* rather than a char. "char func(blah)" should be "char *func(blah)"

4-The final problem and the trickiest. Put the word "static" before your definition of output_month.

static char output_month[4];

There is a good chance that your program will appear to run fine without this, but that would just be luck. This explanation may be a little advanced for the level you are at if you just started.

But by doing "return output_month", you are telling it to return the address in memory of the array output_month. So the cout function uses that address to get a string to output.

No problem. The problem is that output_month is a local variable and local variables disappear after the function exits. The function "func" will exit before the cout executes, so the variable output_month doesn't exist when the cout is printing the string. So it is possible that you could get garbage. Adding "static" changes how the array is stored, so that it won't disappear when the function exits.

But, there is a good chance that you won't have this problem, because the program is simple enough that the memory where the output_month array was stored probably won't get overwritten. Even tho the variable doesn't exist anymore, if the memory that it used to use doesn't get reused, then you won't notice a problem.

But things like this are things that you'll need to learn to know about and notice if you program in C/C++.


0

Response Number 2
Name: kimib
Date: November 12, 2004 at 16:07:08 Pacific
Reply:

you're a star... working 'perfectly'.

Thank You so so much,



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ major probs

noobi c++ WHILE prob www.computing.net/answers/programming/noobi-c-while-prob/14020.html

C Programming prob. www.computing.net/answers/programming/c-programming-prob/1275.html

C++ code probs www.computing.net/answers/programming/c-code-probs/12098.html