0
printf("%c.\n", (getText(precrmo_menu[3]))[0]);  
char input[3];  
input[0] = (getText(precrmo_menu[3]))[0];  
printf("we lived");  
if (strcmp(input, "") != 0 && !isblank(input) && isdigit(input))  
{  
    clearMenu(precrmo_menu, precrmo_menu_length);  
    menu = 2;

}

This code prints out the first character (followed by a period and a line break) but then skips the printf("we lived"); command and hits the if statement and segfaults. When I change the condition to just

if (strcmp(input, ""))

then it again skips the printf("we lived");, hits the condition, finds it true, clears the menu, and then stalls out. If I close the window, the terminal THEN prints out "we lived".

I suppose it's helpful to indicate that this code is within an intentional infinite loop that checks for MOUSE_EVENT and KEY_EVENT. If a click is within a certain zone, it will change menus by making pieces visible/not-visible and changing an int menu that holds a different numerical value based on which menu I'm on. Also, clearMenu() is used elsewhere with no glitches (all it does is loop through the menu array and calling isVisible on them to turn them not-visible).

Furthermore, it's interesting to note that within the loop, I've got an if-else ladder to check which menu number I'm on, including an escape mechanism:

else if(getEventType(event) == KEY_PRESSED)  
{  
    menu = -1;  
}

THIS code also still executes after the program seems to stall out, and if I do press a key and get out, it also waits until the window is closed to print out "we lived"

I don't understand how that command could be held in reserve but the other printf didn't... Anyone know what's going on?

2
  • I don't understand this: if (strcmp(input, "") != 0 && !isblank(input) && isdigit(input)) are you checking whether the first char in input isn't null? Can't you logically simplify that to if(isdigit(input[0]) ? (I'm not sure how that line even compiles, given that you are passing a string to isblank() and isdigit() when both of those take an int (or a char). – curiouskiwi Jan 26 '15 at 10:00
  • @curiouskiwi I have banged my head against the wall for days trying to figure out why when passing a string instead of an int to isalpha and the alike everything compiles perfectly without even a warning since pointers can't be implicitly casted to ints until I found the answer. – kzidane Jan 26 '15 at 10:21
0

It doesn't really "skip" the printf statement. Instead, it just "buffers" the output (in this case, the string "we lived"), then "flushes" it at some point (e.g., when you print a newline). For more info, you may check 12.20.2 Flushing Buffers!

It's possible that you're getting a segfault because the functions isblank and isdigit, each take int as an argument and not a char array or a string. Also notice that the string input might be still missing the null character (i.e., '\0') at the end of it which might cause problems.

3
  • This has been incredibly helpful. I'm going to try changing that condition and seeing what I can do about it. A new question arises though: I looked through the string.h library and couldn't find an equivalent to isblank/isdigit. Is there no prebuilt function to handle that sort of thing on a string? To make sure a user inputs only numbers, would I have to run the string through a loop checking each character with these functions? – Michael F Jan 27 '15 at 7:55
  • @MichaelF well, that's one of the easiest solutions that are out there. If you are doing that multiple times throughout your program or having multiple programs that do that sort of thing, maybe you should define your own functions and use them! – kzidane Jan 27 '15 at 10:19
  • A string is really just an array of chars. An array of chars is really just a pointer to a single char that can be used with pointer arithmetic to access the additional chars. A char is really just an 8-bit integer (an int). so you can use isblank() and isdigit() on each element of those arrays. for(int i = 0; i < strlen(aString); i++) If(!isdigit(aString[i])){printf("Not a string of only digits");} – Nick Young Jan 21 '16 at 21:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .