Getting multiple values with scanf()
You can do this with a single call, like so: scanf( “%i %i %i %i”, &minx, &maxx, &miny, &maxy);
You can do this with a single call, like so: scanf( “%i %i %i %i”, &minx, &maxx, &miny, &maxy);
They are declared in <inttypes.h> as macros: SCNd8, SCNd16, SCNd32 and SCNd64. Example (for int32_t): sscanf (line, “Value of integer: %” SCNd32 “\n”, &my_integer); Their format is PRI (for printf)/SCN (for scan) then o, u, x, X d, i for the corresponding specifier then nothing, LEAST, FAST, MAX then the size (obviously there is no … Read more
If flushing std doesn’t work, then try reading in the extra characters and discarding, as suggested here. This will work: #include <string.h> #include <stdio.h> int main(){ char nombre[10]; char mensaje[80]; int c; printf(“Type your name:\n”); scanf(“%9s”, nombre); while((c= getchar()) != ‘\n’ && c != EOF) /* discard */ ; printf(“Now, type a message:\n”); gets(mensaje); printf(“%s:%s”,nombre,mensaje); … Read more
You need a space between scanf(” and the %c for it to work correctly: scanf(” %c”, &choice); And you also need to use &choice, not choice! EDIT: While you’re at it, you might want to look into do while() for that loop (unless the professor specifically said to use a break) – do while works … Read more
Use fgets to read console input: int res = 2; while (res == 2) { char buf[100]; fgets(buf, sizeof(buf), stdin); res = sscanf(buf, “%f %f”, &real, &img); if (res == 2) c[i++] = real + img * I; } c[i++] = 1 + 0*I; // most significant coefficient is assumed to be 1 return i;
Try using the “%h” modifier: scanf(“%hu”, &length); ^ ISO/IEC 9899:201x – 7.21.6.1-7 Specifies that a following d , i , o , u , x , X , or n conversion specifier applies to an argument with type pointer to short or unsigned short.
You must pass the address of the variable to scanf: scanf(“%c”, &answer);
scanf() does not have a simple method to meet OP’s goal. Recommend using fgets() for input and then parse the string. char buf[20]; fgets(buf, sizeof buf, stdin); // Could add check for NULL here To limit to 2 decimal places, code could read the number in parts: integer portion and fraction, then form the result: … Read more
It is consistent behavior, you’re just thinking about it wrong. 😉 scanf(“%c”, some_char); // reads a character from the key board. scanf(“%d”, some_int); // reads an integer from the key board. So if I do this: printf(“Insert a character: “); scanf(“%c”, &ch); // if I enter ‘f’. Really I entered ‘f’ + ‘\n’ // scanf … Read more
Since the files are “semi-structured” can’t you use a combination of ReadLine() and TryParse() methods, or the Regex class to parse your data?