-2

The for loop runs only once, after entering my answer with scanf, the for loop terminates. I have no idea why?

for(int i = 0; i < 10; i++){  
    char answer;  
    gotoxy(32,10);  //self-made function(using Dev C++) to go to a coordinate
    star_vowels(easy[i]);    
    gotoxy(30,18);  
    printf("ANSWER: ");  
    scanf("%s", &answer);  
}  
fluter
  • 13,238
  • 8
  • 62
  • 100

4 Answers4

2

The answer variable is defined as character so you should change the format string of the scanf function:

scanf("%c",&answer);
klyone
  • 94
  • 1
  • 10
  • I feel so embarrassed right now. This actually fixed it, tho my for loop doesn't count by 1. It's kind of skipping an array index after the other. – Ivan Ralph G. Vitto May 09 '16 at 15:45
  • 2
    @IvanRalphG.Vitto: Regarding the "skipping": do you see `ANSWER: ` printed multiple times even though you have entered only one number? [That may be because `scanf` consumes the newline.](http://stackoverflow.com/questions/19628381/repeat-program-function-failure-in-c-scanf-related). – DarkDust May 09 '16 at 15:49
  • @DarkDust I don't see it print multiple times because of the gotoxy(), however I tried printing out the value of the current array that's why I knew that my for loop is skipping one after the other. Any solutions to this? – Ivan Ralph G. Vitto May 09 '16 at 15:51
  • @IvanRalphG.Vitto: I don't really get that, sorry. Use something like `printf("%d\n", i);` to really check whether `i` is skipping values. Or better yet: use a debugger :-) – DarkDust May 09 '16 at 15:53
  • @DarkDust I've already tried that. It sure is skipping depending on the length of whatever I type plus 1 character for the new line. – Ivan Ralph G. Vitto May 09 '16 at 15:54
  • @IvanRalphG.Vitto: But for you **answer** variable must be a string or a char? – klyone May 09 '16 at 15:57
  • @klyone yes of course. It's solved by the way, thanks to you guys! Solved it by replacing scanf with gets();. Thanks again! – Ivan Ralph G. Vitto May 09 '16 at 15:59
  • @IvanRalphG.Vitto But be careful because the gets() function gets a string and you try to store in a char variable... I think you should have the same problem... – klyone May 09 '16 at 16:02
  • @klyone will do, thanks again! – Ivan Ralph G. Vitto May 09 '16 at 16:03
1

The problem is likely because of your buffer overflow.

You define a variable answer that can store just one char. Yet you pass it to scanf to scan a string. If you just enter one character and press enter, you already overflow answer and likely overwrite i in the process which then becomes larger than or equal to 10.

You need to pass a buffer (like char answer[100]; or dynamically allocated with malloc) to scarf. Using scanf to scan a string correctly is a bit tricky.

If you really only meant to read a single character instead, @klyone's answer is the correct one: use the %c format string.

Community
  • 1
  • 1
DarkDust
  • 90,870
  • 19
  • 190
  • 224
0

One issue would be that you are using scanf to input a string, however answer is a single character.

Even if you are only entering a single character string, it will be attempting to put that character in the string, followed by a null byte.

Assuming a 1 charcater answer, changing the 'char answer' to 'char answer[2];' and changing the scanf() to scanf("%1s", answer); may help

lostbard
  • 5,065
  • 1
  • 15
  • 17
0

Assuming you want to read in a string as opposed to a single character, I would highly recommend using fgets instead of scanf to read your input, as it will ensure that you never get a buffer overflow of any sort, as far as I recall.

Also, as others have said, you will first need to allocate more than one character for the answer, either by statically allocating it or doing it dynamically with malloc and free. Assuming you want to save the answers somewhere, doing dynamic allocations would be necessary. If you only need it one time, allocating it statically like char answer[100]; would suffice. Although I would recommend putting something like #define BUFFER 100 at the top of your file, and allocating it like char answer[BUFFER]; instead.

Random Davis
  • 6,662
  • 4
  • 14
  • 24