It's possible to read the text using scanf? [duplicate] - c

This question already has answers here:
How do you allow spaces to be entered using scanf?
(11 answers)
Closed 3 years ago.
I need to read the text using scanf, but what I know scanf is reading characters to the first space. I don't know that it's possible to read more words into the array by scanf and i have no any idea.

That's not correct; scanf reads according to the format string you give it.
Nothing keeps you from using a format that reads past spaces, returns, or whatever else, for example scanf("%50c",buffer); will read 50 characters, no matter what they are; or scanf("%[^|]", buffer); will read everything up the first |. Read up on the definition of the scanf family.

Related

How to recieve multiple inputs when using a switch statement? [duplicate]

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Closed 12 days ago.
I have do while menu setup and inside is the switch statement to select each menu option. However when you select option "1" it immediately loads two of my printf() statements without allowing the user to input for one of the statements (I'm using fgets()).
When I input 1 during the initial menu, it immediately prompts me with
"Enter Name of Artist:
Enter Name of song: "
And If input anything it accepts it, takes me to the next printf, "Enter song length: "
And that's it. I can't seem to understand why I don't get prompted the first two printf's one after the other.
Thanks

Do I'm missing something in code? The results are not right [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 1 year ago.
I'm new to programming, trying to learn C.
The program below is showing no signs of error still the output is not correct. I tried it without using '&' sign as "ms" and "sc" are characters but still that doesn't work. What am I doing wrong?
the problem is that you missed enclosing quotation marks in the scanf call
replace the following
line 5: scanf("%c",&ms)
line 7: scanf("%c",&sc)

Discrepancy betwenn scanf and get_int [duplicate]

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 2 years ago.
discrepency
Hi everyone,
Currently working on some Harvard's CS50 problems, I've noticed that substituting get_int by scanf makes my program behave erratically.
The problem at hand here consists in creating a plurality election mockup. And the simple fact that I replace get_int by scanf in order to register the number of votes during the election ends up signalling the first vote as 'invalid' (a function later in the code is charged of checking vote validity).
I've checked multiple times, used the debugger, but to no avail.
Any hints as to why that happens would be deeply appreciated.
scanf("%i", &vote_count) does not consume anything after the number. What comes after the number is most likely a newline character, which is what will be seen by get_string. That probably causes the first name to be an empty string.
You should see all that if you use a debugger.

Difference between scanf and scanf_s in C [duplicate]

This question already has answers here:
Difference between scanf and scanf_s
(3 answers)
Closed 8 years ago.
Although my program gives the required output. There are many warnings showing scanf() shouldn't be used and try using scanf_s() instead.
Possible cause of this warning??
This is happening to all the programs which are using the scanf function. Even simple addition of numbers.
With scanf, with some format arguments it is possible to crash your program if you use unlimited size inputs. scanf_s requires to provide size of output buffers, thus limiting possibility of buffer overflows (provided you specify output buffer sizes correctly).

Reading in to a character array of unknown size [duplicate]

This question already has answers here:
C dynamically growing array
(10 answers)
Closed 9 years ago.
How do I read in a c-string in to a character array without knowing the size of the string that the user will enter ?
Without any code or further description of your issue it's hard to know what you're trying to achieve, but, one of the following might be appropriate for your needs:
Use a preallocated array of some maximum size you know is greater than the number of characters that will be entered.
Create an empty std::string, and then use the string "+=" operator for each character entered. Then you can convert back to an array using the c_str() method.

Resources