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);
There are multiple differences. Two crucial ones are: fgets() can read from any open file, but scanf() only reads standard input. fgets() reads ‘a line of text’ from a file; scanf() can be used for that but also handles conversions from string to built in numeric types. Many people will use fgets() to read a … Read more
The short answer is that it has no impact on printf, and denotes use of float or double in scanf. For printf, arguments of type float are promoted to double so both %f and %lf are used for double. For scanf, you should use %f for float and %lf for double. More detail for the … Read more
My advice, go for fgets(). Read the whole line from the input Tokenize using space [] [or your preferred delimiter] [using strtok()] Allocate memory to store the integer Convert the string input to integer [maybe strtol()] and store each integer. Optionally, you may want to add some validation and error checking. Read more about fgets() … Read more
%f is looking for a float, not a double. If you want to use a double, use the format %lf. As a somewhat interesting aside, clang warns about this without any extra flags, gcc 4.6 won’t warn about it even with -Wall -Wextra -pedantic.
Two points to consider. getchar_unlocked is deprecated in Windows because it is thread unsafe version of getchar(). Unless speed factor is too much necessary, try to avoid getchar_unlocked. Now, as far as speed is concerned. getchar_unlocked > getchar because there is no input stream lock check in getchar_unlocked which makes it unsafe. getchar > scanf … Read more
You are looking for std::getline(). For example: #include <string> std::string str; std::getline(std::cin, str); I’ve little idea what you mean when you say I must also be able to store the value through a pointer. Update: Looking at your updated question, I can imagine what is happening. The code that reads the choice, i.e. the number … Read more
Try: while(scanf(“%15s”, words) != EOF) You need to compare scanf output with EOF Since you are specifying a width of 15 in the format string, you’ll read at most 15 char. So the words char array should be of size 16 ( 15 +1 for null char). So declare it as: char words[16];
A simple solution using fscanf: void read_ints (const char* file_name) { FILE* file = fopen (file_name, “r”); int i = 0; fscanf (file, “%d”, &i); while (!feof (file)) { printf (“%d “, i); fscanf (file, “%d”, &i); } fclose (file); }
Let’s take a look at the problem one by one: newline Remains in stdin after No. or Persons Read printf(“\n\nEnter name: “); safer_gets(person[i].full_name, 35); /* This is the step being skipped */ It is skipped because your safer_gets() reads only to the first ‘\n’ (newline character — not a carriage-return, that is ‘\r’). However, the … Read more