Problems with scanf() in C

Hey folks, haven't been through in awhile. Hope all's well. This is sloppy student C code for a school project, written in bash shell using gcc to compile. The project is on interprocess communication using pipes, but I'm embarrassingly having problems getting keyboard console input. I'm trying to use scanf() and, I guess, have no idea what I'm doing. For type specifiers I've tried using %c, %s, and now %ls per my instructor's example. With %ls, I get a compile warning indicating %ls expects type wchar_t and here I am giving it a char*. But weirdly his example code declares and uses the char the exact same way and compiles with no warnings. Also, with all specifiers (not just %ls), the program spits random garbage to the screen after I enter my selection at line 19. The first character of the garbage is the character I just entered, and this happens before it hits the diagnostic "Checkpoint 1" printf at line 21.

Any ideas what's going on or, alternatively, a better method for accepting single character input in this situation? The single character input can either be a letter or number. Any advice appreciated, thanks so much.



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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  // child process
void child(int READ, int WRITE) {
	char input[2];
	int num = 0;  
	int g, r, c;
	Game response;
	Cell move;

	initGame();

	system("clear");
  
	printf("[This is where the name of the");
  	printf("game will go.]\n\n");
  	printf("\t1 - One Player\n");
  	printf("\t2 - Two Players\n");
  	printf("\tQ - Quit\n");
	printf("\tEnter selection: ");
  	scanf("%ls", input);

        printf("Checkpoint 1");       //Diagnostic

  	while ( input[0] != '1' && input[0] != '2' && input[0] != 'q' && input[0] != 'Q'  ) {
		system("clear");

		printf("[This is where the name of the");
		printf("game will go.]\n");
		printf("\nInvalid selection!\n");
		printf("\t1 - One Player");
	        printf("\t2 - Two Players");
		printf("\tQ - Quit\n");
		printf("\tEnter selection: ");
		scanf("%ls", input);
	}

	printf("Checkpoint 2");        //Diagnostic

   	if ( input[0] == 'q' || input[0] == 'Q' ) game.quit = 1;
	else {
		game.players = atoi(input[0]);
		populateVectorList();
	}


	while (!game.quit) {
		displayBoard();

		if (game.turn > 2) {
			printf("Player 2 moved to Grid %i, Row %i, Column %i.", game.lastMove.grid, game.lastMove.row, game.lastMove.col);
		}

		printf("Player 1, it's your turn!");
		printf("Enter grid # (1-4), 'Q' to quit: ");
		scanf("%ls", input);
		while (input[0] != 'q' || input[0] != 'Q' || input[0] != '1' || input[0] != '2' || input[0] != '3' || input[0] != '4') {
			system("clear");
			displayBoard();

			printf("Player 1, it's your turn!");
			printf("Enter grid # (1-4), 'Q' to quit: ");
			scanf("%ls", input);
		}
		
		if (input[0] == 'q' || input[0] == 'Q') {
			game.quit = 1;
			continue;
		} else g = atoi(input);

		printf("Enter row #: ");
		scanf("%ls", input);
		while (!(0 < atoi(input) < 5)) {
			system("clear");
			displayBoard();

			printf("Player 1, it's your turn!");
			printf("Enter grid # (1-4), 'Q' to quit: %d/n", g);
			printf("Enter row #: ");
			scanf("%ls", input);
		}

		r = atoi(input);

		printf("Enter column #: ");
		scanf("%ls", input);
		while (!(0 < atoi(input) < 5)) {
			system("clear");
			displayBoard();

			printf("Player 1, it's your turn!");
			printf("Enter grid # (1-4), 'Q' to quit: %i/n", g);
			printf("Enter row #: %i/n", r);
			printf("Enter column #: ");
			scanf("%ls", input);
		}

		c = atoi(input);

                game.board[g][r][c] = 1;
		game.lastMove.grid = g;
		game.lastMove.row = r;
		game.lastMove.col = c;

		game.turn++;

		write(WRITE, &game, sizeof(game));
		read(READ, &response, sizeof(response));

		game = response;
	}

	printf("CHILD SEZ GOODBYE!!");

  	close(READ);
  	close(WRITE);
}
> I get a compile warning indicating %ls expects type wchar_t and here I am giving it a char*.
> But weirdly his example code declares and uses the char the exact same way and compiles with no warnings.
maybe he didn't compile with warnings enabled.
you shouldn't lie in the format specifier.

> printf("Checkpoint 1"); //Diagnostic
Get a debugger.


http://www.cplusplus.com/forum/general/112111/
No, it's me compiling his code with the same settings, thanks.
Topic archived. No new replies allowed.