-1

So I want to write two variables in the same scanf_s. Maybe Im not even using the right names to describe what I want because im new to this but basically I want it to come out as this:

What is your last and first name: John Smith
Thank you now I know that your first name is John and your last name is Smith

And what I have written is this:

#include <iostream>

int main(void)
{
    char myFirstName[20];
    char myLastName[20];

    printf("\nWhat is your first and last name: ");
    scanf_s("???")
    printf("Thank you now I know that your first name is %s", myFirstName); printf(" and your last name 
    is %s\n",myLastName);

    getchar();
    return 0;
}

And I dont know how I should write the scanf_s part to include the two variables (myFirstName and myLastName) so it comes out as I want it.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
davzter
  • 1
  • 1
  • Can you read *one* string with `scanf_s`? There's no real limit to the number of format specifiers, for either `scanf` (and related functions like `scanf_s`) or `printf`. – Some programmer dude Oct 24 '19 at 15:43
  • 3
    There is no C header `iostream`. Do you compile with C or C++ compiler? Those are different languages. – Gerhardh Oct 24 '19 at 15:45
  • 1
    [Don't use `scanf` to read interactive input](https://stackoverflow.com/questions/58403537/what-can-i-use-for-input-conversion-instead-of-scanf); it cannot handle the user typing something other than what you expected. Use `fgets` to read an entire line of whatever the user typed, and then parse it using functions such as `strsep`. [Don't use `scanf_s`, or any of the other `_s` functions, at all](https://stackoverflow.com/questions/57915149/using-c11-standard-with-clang-for-use-of-strcpy-s/57915261); they are not portable and don't solve the problems they were intended to solve. – zwol Oct 24 '19 at 15:46

1 Answers1

1

If your two inputs won't contain space characters, you can write like this:

if (scanf_s("%19s%19s",
    myFirstName, (unsigned)sizeof(myFirstName),
    myLastName, (unsigned)sizeof(myLastName)) != 2) {
    puts("input failed");
    return 1;
}

points:

  • Yon can specify multiple conversion specifiers in the format string.
  • %19s is a format spacifier for reading string "%s" with a limit to save at most 19 characters plus one terminating NUL character.
  • When reading strings or characters via scanf_s, buffer size in type unsigned must be specified after each pointers to buffers.
  • scanf_s returns the number of successfully read data, so it should be checked to see if enough data are read.

reference:

scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l | Microsoft Docs

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • I'm sure you mean `sprintf(fmt, "%%%us%%%us", (unsigned)sizeof (myFirstName) - 1, (unsigned)sizeof (myLastName) - 1); if (scanf(fmt, ...) != 2) ...` – pmg Oct 24 '19 at 20:03
  • @pmg The question says "in same `scanf_s`", and unfortunately some compilers dislike `scanf`. [windows - error C4996: 'scanf': This function or variable may be unsafe in c programming - Stack Overflow](https://stackoverflow.com/questions/30577519/error-c4996-scanf-this-function-or-variable-may-be-unsafe-in-c-programming) – MikeCAT Oct 25 '19 at 13:10
  • I meant `scanf_s` (sorry). The point is your use of literal `19` versus `sizeof(myFirstName)` ... either use `19` and `20`, or always `sizeof` :) – pmg Oct 25 '19 at 13:16