Printf does not repeat in loop after scanf

Hello, I am a newb to C, been programming Basic/Visual Basic since 1984. Trying to teach myself, (as I learned Basic, HTML, etc) using books. I have searched here and google, but did not find the answer to this, so I hope someone can explain the issue to me. The code below works fine with the scanf line commented out, and prints a nice list of the test integers and the count. When I uncomment the scanf line, it prints one line, then stops for input as expected, but does not do the printf line in any subsequent iteration, just stopping for the scanf input, but not printing the count line. Why does it behave like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  // Training 1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"



int _tmain(int argc, _TCHAR* argv[])
{

	int test = 0, cnt = 0;
	char mykey ='K';

	while (test<= 1000000)
	{
		test += 100;
		cnt +=1;

		printf("Test = %d.  %d\n" , test, cnt) ;
		//scanf(" %c %c \n" , mykey);

	}
	

	return 0;
}
Your arguments to scanf is not correct. You need to pass a pointers to the variables. If you are reading two values you need to pass two pointers.

1
2
3
char mykey;
char mykey2;
scanf(" %c %c \n" , &mykey, &mykey2);
Last edited on
Thank you, you were correct, the problem was due to my using a variable instead of a pointer. I did not want 2 values, but that was an idea I found (as well as the leading space before the %c) somewhere else that seemed to solve an issue, but in actuality was suppressing the error message. Pointers are a new concept to me.

Here is the corrected code for the benefit of other rank noobs like me!

1
2
3
4
5
6
7
8
9
while (test<= 1000000)
	{
		test += 100;
		cnt +=1;

		printf("Test = %d.  %d\n" , test, cnt) ;
		
		scanf("%c" , &mykey);
	}
Topic archived. No new replies allowed.