1

I've a problem and I couldn't find answer.

I need to get 2 strings from user using scanf(must) and print them.

I have 2 problems 1. when I type the first string it's somehow skip the second one. 2. if my string includes spaces, I takes only the chars till the space, from what I think the 2nd word goes to the second "scanf"

this is my code

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
void main()
{
    char format[30];
    char numbers[20];

    scanf("%s", format);

    scanf("%s", numbers);

    printf("\n %s %s", format, numbers);
}

is it possible to get 2 strings from user 1 after another including spaces? Thanks in advance

Brec
  • 87
  • 1
  • 1
  • 8

2 Answers2

0

I solved it this way

I added another scanf which I don't need and now skipping the second scanf but it stops on the third. not the best way but it helps.

scanf("%[^\n]s", format);
scanf("%c", &temp);
scanf("%[^\n]s", numbers);
Brec
  • 87
  • 1
  • 1
  • 8
-1

This is a very tricky request if a character class is used because %[^n] leaves a \n in the input buffer. You can solve the problem by reading a character in a second scanf:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char end;
    char format[30];
    char numbers[20];

    scanf("%[^\n]", format);
    scanf("%c", &end);
    scanf("%[^\n]", numbers);
    scanf("%c", &end);
    printf("\n");
    printf("format = %s\n", format);
    printf("numbers = %s\n", numbers);

    return 0;
}

Output

A string with spaces
Another string with spaces

format = A string with spaces
numbers = Another string with spaces
David Cullen
  • 13,143
  • 4
  • 35
  • 60
  • Try putting `(` in your test strings. or start them with `'\n'`. – chux - Reinstate Monica Nov 18 '16 at 04:19
  • These changes handle `(` and `\n`. Do you see any other holes? – David Cullen Nov 30 '16 at 18:37
  • If the user enters `'\n'`, `format` is not initialized nor set, `printf("format = %s\n", format);` is is UB. Good code checks the return vaulue of input functions like `scanf()`. `scanf("%[^\n]", format);` is subject to buffer overflow too. – chux - Reinstate Monica Dec 01 '16 at 04:18
  • Maybe you should answer the question instead of just pointing out the flaws in my answer. – David Cullen Dec 02 '16 at 13:26
  • My [comment](http://stackoverflow.com/questions/40663179/c-prog-how-to-get-2-string-from-user-1-after-another-using-scanf/40663652?noredirect=1#comment69018459_40663652) is in response to your [question](http://stackoverflow.com/questions/40663179/c-prog-how-to-get-2-string-from-user-1-after-another-using-scanf/40663652?noredirect=1#comment69005338_40663652) and did not just point out flaws as it also presented improvement ideas. Checking the result of user input functions is so useful in ID'ing input issues. – chux - Reinstate Monica Dec 02 '16 at 15:36
  • I am suggesting that it would be more efficient if you answered the question. I could add return value checks and other safety mechanisms, but if I was going to write the perfect solution, I wouldn't use `scanf`. However, the OP indicated that `scanf` was a requirement. This is as good as my answer is going to get. If you have a better one, you should provide it. – David Cullen Dec 02 '16 at 16:14
  • The answer was closed before my first comment. Unless the post is re-opened, I cannot provide an answer as [suggested](http://stackoverflow.com/questions/40663179/c-prog-how-to-get-2-string-from-user-1-after-another-using-scanf/40663652?noredirect=1#comment69084408_40663652). Commenting on the weakness of the best candidate answered seemed like a good idea, at the time, to bring it up to a good answer. – chux - Reinstate Monica Dec 02 '16 at 16:22