August 4th, 2013, 08:26 PM
Character strings not printing correctly
I'm not sure why I'm having this issue, because theoretically it shouldn't be a problem. But I have a long code, with many "if" statements and loops. And at the beginning of the code I initialize a lot of character strings and assign values to all of them. After lots of code, when i try to print those character strings, they sometimes don't work. I try to print them, like this:
strcpy(string1, "not words");
printf("words words words %s words words", string1);
but with lots of steps in between those ones. And I'll run the same program multiple times, but sometimes they don't work. It seems like different combinations of "if" statements leading up to the "print" statement result in either success or failure. If it runs correctly, the string gets printed when it's supposed to. But sometimes it prints nothing, leaving me with two consecutive spaces. I have no idea why this happens, and I would think it shouldn't be possible.
Can anyone shed some light on this freaky occurrance? I'd be really grateful.
August 4th, 2013, 08:39 PM
Is string1 initiated ? What does it evaluate to ?
Ok, nvm, i copied what your wrote, and it ran fine when compiled alone.
Means that it's not the issue, probably something else.
Try commenting parts of the code to see which part causes the problem, and then you can get closer.
Do you have functions in the program or just lots of loops ?
August 4th, 2013, 08:58 PM
The code is almost 2500 lines, so I don't want to copy it in. It's just a network of "if" statements. But they're irrelevant to the ultimate "print" statements that are causing problems. They affect other things along the way (like printing hardcoded text).
It just seems weird because if the character string is left untouched throughout the program until the "print" statement where I use it, it should print fine regardless of everything before that line.
Is it possible that I have too many strings? Because there are almost 100 variables declared and initialized at the top (a mixture of INT and CHAR). I was thinking that maybe I have too many variables for the program/computer to handle?
August 4th, 2013, 09:08 PM
:eek: 2500 lol
Anyway, what does the program do ??
It's really hard to tell without the code. But as in any program, your print will probably be affected by whatever came before that, since your'e passing data from one point to another.
If you suspect the other strings- comment them out and see how it compiles without.
When you use functions, you combine various commands into a single block, which has its own local variables and is independent from main (it's very comfortable). So when you want to cancel out all the code lines that belong to the function, all you have to do is cancel out the single line, inside main, in which the function is being called. Otherwise, you will have to look through all your lines and comment them one by one....with 2500 lines i suppose it's nearly impossible.
You can also try using switch, it's much more handy than if if if if ....
Comment each function at a time. When you see that your prog runs well without a certain function, compile it alone and pass data to it, and then print what it returns.
BTW, large programs are divided into files. Not that it changes anything, you can still write the code in a single file. It's just that with so many lines, finding a single bug is almost impossible.
I've seen smaller programs than 2500 lines being divided into files, LET ALONE into functions...
August 4th, 2013, 09:29 PM
You've been very helpful. Thank you! :)
I figured out what it is (I think). In my code, the initialization of the strings is dependent on "if" statements that I had written incorrectly. For instance, I had:
else if (x%2==1)
and I needed:
else if ((x%2)==1)
I don't know why that originally made it work (the times that it did), but I think now it works every time. Thanks again!
August 4th, 2013, 09:35 PM