scanf

C: scanf input single character and validation

这一生的挚爱 提交于 2021-02-20 04:53:11
问题 I've encountered a problem when validating a single-char scanf input in C and I cannot find an existing solution that works... The scenario is: a method is taking a single letter 'char' type input and then validating this input, if the criteria is not met, then pops an error message and re-enter, otherwise return this character value. my code is: char GetStuff(void) { char c; scanf("%c", &c); while(c != 'A' || c != 'P') { printf("invalid input, enter again (A for AM or P for PM): "); scanf ("

C: scanf input single character and validation

江枫思渺然 提交于 2021-02-20 04:52:02
问题 I've encountered a problem when validating a single-char scanf input in C and I cannot find an existing solution that works... The scenario is: a method is taking a single letter 'char' type input and then validating this input, if the criteria is not met, then pops an error message and re-enter, otherwise return this character value. my code is: char GetStuff(void) { char c; scanf("%c", &c); while(c != 'A' || c != 'P') { printf("invalid input, enter again (A for AM or P for PM): "); scanf ("

sscanf usage on matrix of unknown size?

此生再无相见时 提交于 2021-02-19 05:26:16
问题 So, I have a file that contains a matrix of NxM size. For example: P2 3 3 1 1 0 0 0 1 0 0 0 1 The 'P2' is just an useless indicator, the first '3' indicates how many columns are and the second '3' indicates how many lines, '1' indicates the maximum value among the matrix's numbers. This matrix was stored in a data structure like this: typedef struct { int c; // columns int l; // lines unsigned char max; // max value unsigned char** data // variable to store matrix's numbers } Matrix; To store

When do we need to clear the scanf buffer?

你离开我真会死。 提交于 2021-02-18 12:44:18
问题 I always thought that the "'\n' in buffer" issue only occurs when we are reading characters, however, I stumbled upon this issue with the following code: int main(int argc, char** argv){ int height = 0, width = 0; while(height < 1 || height > 10|| width < 1 || width > 15){ printf("Please insert the height(1~10) and width(1~15) of the parallelogram (integers): "); if(scanf("%d %d", &height, &width) != 2){ height = width = 0; } } return 0; } As above, I'm only reading integers with scanf, but

When do we need to clear the scanf buffer?

喜你入骨 提交于 2021-02-18 12:44:08
问题 I always thought that the "'\n' in buffer" issue only occurs when we are reading characters, however, I stumbled upon this issue with the following code: int main(int argc, char** argv){ int height = 0, width = 0; while(height < 1 || height > 10|| width < 1 || width > 15){ printf("Please insert the height(1~10) and width(1~15) of the parallelogram (integers): "); if(scanf("%d %d", &height, &width) != 2){ height = width = 0; } } return 0; } As above, I'm only reading integers with scanf, but

scanf is using an uninitialized variable; C [duplicate]

霸气de小男生 提交于 2021-02-17 03:29:51
问题 This question already has answers here : why scanf scans a null value (2 answers) Closed 6 years ago . I'm sure there just a silly mistake here, however, I can't figure it out. This is part of my code: char *moving; scanf("%s", moving); When I compile it with gcc, it says the following: newmatrix.c:38:7: warning: ‘moving’ is used uninitialized in this function [-Wuninitialized] Line 38 is the scanf How do I fix this? Thanks 回答1: Allocate memory for moving before using it. Use malloc() .

scanf(“%c”) automatically reads 10

隐身守侯 提交于 2021-02-16 21:13:10
问题 void main() { int cnt=1; char i; while(cnt<4) { printf("\nenter the character"); scanf("%c",&i); if(i>64 && i<91) printf("\ncharacter is entered"); else printf("\nnumber is entered"); cnt++; } } In the above program, during the second iteration, i automatically takes 10. So the control goes to else part. Can anyone help me find what is the issue? 回答1: In the first iteration, you type a character and press Enter . scanf consumes the character you entered, leaving the \n in the standard input

Using scanf into global or local variables (on the stack), 32-bit calling convention

好久不见. 提交于 2021-02-16 20:11:49
问题 Given the following code : .section .rodata str: .string "Hello World!\n" input: .long 2 ######## .text .globl main .type main, @function main: pushl %ebp movl %esp, %ebp pushl $str call printf #return from printf: movl $0, %eax movl %ebp,%esp popl %ebp ret The output would be "Hello World!". Now I try to get a number from the user , and then print it out on the screen , but it doesn't work (code compile,but I did something wrong) . Where is my mistake ? .section .rodata input: .long 2 ######

Using scanf into global or local variables (on the stack), 32-bit calling convention

邮差的信 提交于 2021-02-16 20:11:12
问题 Given the following code : .section .rodata str: .string "Hello World!\n" input: .long 2 ######## .text .globl main .type main, @function main: pushl %ebp movl %esp, %ebp pushl $str call printf #return from printf: movl $0, %eax movl %ebp,%esp popl %ebp ret The output would be "Hello World!". Now I try to get a number from the user , and then print it out on the screen , but it doesn't work (code compile,but I did something wrong) . Where is my mistake ? .section .rodata input: .long 2 ######

address passing in scanf but not in printf [duplicate]

China☆狼群 提交于 2021-02-16 09:28:12
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Why scanf must take the address of operator Why do we pass the variable in the case of printf(), whereas the address of the variable in the case of scanf()? why to pass address in scanf 回答1: why to use '&' in scanf( ) but not in printf( ) 'printf'()' only need the values in order to output them. 'scanf()' stores values, so it needs a place to store them. This is done by providing the addresses (in pointers) of