Multidimensional string problem

I have a switch...

In case 1 and 2 the multidimensional array of string works fine
But when it do the case 3, the string value in stringWeight[1] was erased.

BTW, the stringWeight[1] value for case 3 is the same as the value in stringWeight[1] in case 2.

Basically, I have a for loop that counts, from 1 to 3.

It will do first case 1, then case 2, then case 3.

For case 1 it will store a value to stringWeight[0]
case 2 will store a value to stringWeight[1] with same value for stringWeight[0] from case 1
case 3 will store a value to stringWeight[2], with stringWeight[0] from case 1 and stringWeight[1] from case 2

I can store the value to another string like
string2 = stringWeight[1];

but I what I want is the explanation why stringWeight[1] has no value at case 3.

EDIT:
Now if I add case 4, the stringWeight[1] will now have a value but stringWeight[2] has no value. :(

Here is my code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
switch(count)
{
    case 1:
     sprintf (display, "Weights:\n%s", stringWeight[0]);
     //actually I can do this
     //string1 = stringWeight[0]; 
     CreateInstructionsWindow (display, "Please load second weight"); 
     break;
    case 2:
     //sprintf (display, "Weights:\n%s \n%s", string1, stringWeight[1]); 
     sprintf (display, "Weights:\n%s \n%s", stringWeight[0], stringWeight[1]); 
     //string2 = stringWeight[1];
     CreateInstructionsWindow (display, "Please load third weight"); 
     break;
    case 3: 
     //sprintf (display, "Weights:\n%s \n%s \n%s", string1,      string2, stringWeight[2]);
     sprintf (display, "Weights:\n%s \n%s \n%s", stringWeight[0],      stringWeight[1], stringWeight[2]); 
     CreateInstructionsWindow (display, "All weights obtained."); 
     break; 
}


Thanks for the help in advance
Last edited on
Nothing there is changing the strings. You'll need to post more code.

1) Are these strings we're talking about (as in std::string) or are they char arrays? Judging by how you pass them to sprintf I'm assuming char arrays, but you know what they say about assuming.

2) is 'display' large enough to hold this whole string? maybe you have some memory corruption going on (always a possibility)

3) are you passing these strings to any functions other than sprintf?


Anyway, yeah.... more code. I'll need to see the for loop and how all these variables are defined at least. Maybe even the whole function depending on how small it is.
1) uhm. its a char arrays

2) actually there are several files (200+ files) and it is large enough.

3) no.

hmm. actually I cannot show you the codes, it is very long, about 4000+ lines :(


Oops sorry. actually it is not my code..
BTW thanks Disch for the help. The problem is already solved.
The declaration of her string is stringWeight[2] instead of stringWeight[3] for the null character. XD
Last edited on
Topic archived. No new replies allowed.