Problems c / c ++ input string

POJ2236 Wireless Network
encountered while solving the problem a little trouble, declares a char variable ch, after circulating enter find this does not work, refer to the solution to a problem of others as well as access to the information after summarized some on c / c ++ input problem .
First posted code:

#include <stdio.h>
int main() {
	char ch;
	while(~scanf("%c", &ch)) {
		if(ch == 'O')
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
} 

Run Results:
O
YES
NO
. 1
NO
NO

Why is there such a result?
Is because the scanf to read characters, it will also read whitespace characters (carriage return, spaces, tabs). The procedure described above and clicks the Enter key input O, scanf reads characters from the buffer O, i.e., the enter key but remain in the buffer line breaks, then the output YES and line, but reads the immediately previous scanf input when left in the buffer and outputs the newline NO. Corresponds to the input to perform a cycle twice (Scanf respectively read and newline characters entered).

First, we can write:

#include <stdio.h>
int main() {
	char ch;
	while(~scanf("%c", &ch)) {
		if(ch == '\n')
			continue;
		if(ch == 'O')
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
} 

Can output normal.

Also this:

#include <stdio.h>
int main() {
	char ch[2];
	while(~scanf("%s", ch)) {
		if(ch[0] == 'O')
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
} 

getchar()和fflush()

#include <stdio.h>
int main() {
	char ch;
	while(~scanf("%c", &ch)) {
		//getchar();
		fflush(stdin);
		if(ch == 'O')
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
} 

getchar () within a carriage absorbing excess buffer.
action fflush () buffer is empty, fflush (stdin) and fflush (stdout) are clear of the standard input and output buffers.
Both functions may be such that normal operation of the program results.

" %c"

#include <stdio.h>
int main() {
	char ch;
	while(~scanf(" %c", &ch)) {
		if(ch == 'O')
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
} 

% C in front where there is a space, access to information later discovered the magic of usage, if the% c in front of a space that will directly ignore the buffer space, carriage return (pro-test no matter how many spaces and carriage returns inputs can output normal).

In addition also noted that a knowledge point, "% * c" can ignore a char type character, that does not assign values ​​to variables.

gets()

#include <stdio.h>
int main() {
	char ch[2];
	while(gets(ch)) {
		if(ch[0] == 'O')
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
} 

gets () function reads one line string and scanf ( "% s") except that the scanf ( "% s") read character string encounters a blank stopped, but gets () function reads the string can have spaces.
Further gets () function to read a string of characters in the transport buffer will be removed and discarded upon, i.e. it is not the last buffer of the transport superfluous.

Enter on the c ++

Before writing binary established when the code, a prelude to the establishment of binary tree, input is cin, console input space represents an empty tree, the results can not function properly. Later learned that cin is not read spaces, and cin.get () reads a character, that character can be a space.

Read his string, can be used getline (), the syntax is getline (cin, str), str which is a string type variables.

Published 44 original articles · won praise 0 · Views 844

Guess you like

Origin blog.csdn.net/Komatsu_1137/article/details/104055354