0

I'm having a bit of trouble passing multiple variables to scanf:

#include<stdio.h>

int main(void) {

    char *name;
    float weight;

    printf("Please enter your name, then weight separated by a comma: ");
    scanf("%s,%f", name, &weight);

    printf("%s, your weight is %.2f!\n", name, weight);

    return 0;

}

Please enter your name, then weight separated by a comma: Tommy,184
Tommy,184, your weight is 0.00!

What would be the proper way to do this, and why doesn't scanf detect the comma and pull the necessary values in their variables?

samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
  • 1
    1. Where is `name` going to be stored? 2? Does if get to the %f bit? 3. Why not check the return value from `scanf` – Ed Heal Jan 08 '21 at 02:33
  • 2
    `char *name;` reading into this without valid memory will be UB. – IrAM Jan 08 '21 at 02:35
  • @EdHeal it worked, I think the issue is `%s` needs to be separated with a space. -- `%s -- String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab). ` – samuelbrody1249 Jan 08 '21 at 02:37
  • @IrAM why would that be unexpected behavior? It seems to work fine for me. – samuelbrody1249 Jan 08 '21 at 02:38
  • @EdHeal return value is `2` for the two items stored (as soon as I add the space) – samuelbrody1249 Jan 08 '21 at 02:40
  • @samuelbrody1249, refer [1](https://stackoverflow.com/questions/10668504/segmentation-fault-c) [2](https://stackoverflow.com/questions/46858214/segmentation-fault-in-passing-a-char-pointer) – IrAM Jan 08 '21 at 02:48
  • 2
    @IrAM I see, thanks for those links. So instead do something like `char name[10];`...`scanf("%9s", name)` ? – samuelbrody1249 Jan 08 '21 at 02:55
  • @samuelbrody1249 - Why are you not checking for this. BTW - See comment above – Ed Heal Jan 08 '21 at 03:18
  • 1
    Maybe `scanf(%9[^,], %f"...)`] – Ed Heal Jan 08 '21 at 03:28
  • @samuelbrody1249, if using uninitialized pointers works fine for you, then that behavior is very much unexpected.. – HAL9000 Jan 08 '21 at 04:21
  • 1
    `%s` as format specifier would read everything up to next whitespace. Including name, comma and float. – HAL9000 Jan 08 '21 at 04:25

3 Answers3

0

Can you give this a try?

int main()
{
    int num; 
    char str[50];
    scanf("%49[^,],%d", str, &num); 
    printf("%s %d",str, num); 
    return 0; 
}

EDIT

Explanation: We can read input up to a specific character/characters using %[^<Your character(s)>]. The number 49 here simply refers to the length of string you'd receive and the last character is a null. Adding specific length is up to you ;) This is just one example of getting input the way you asked.

Deepak
  • 2,660
  • 2
  • 8
  • 23
  • 2
    `if( scanf(...) == 2 ) { printf(...);}` You must always check the value returned by scanf – William Pursell Jan 08 '21 at 03:05
  • 1
    Programming is not trial and error. Why do you believe this would work? It is not meant as a rhetorical question. You should explain your code. – HAL9000 Jan 08 '21 at 03:08
  • @WilliamPursell Thanks and I agree. I am not performing any logic here to deal with the inputs, as the OP had issue getting the input. – Deepak Jan 08 '21 at 03:55
  • @Deepak, I see that you changed `char *name;` to `char name[50];`. Is there a specific reason to do that? Or are they equivalent statements? (Seeing my other rants should answer the last question :-) – HAL9000 Jan 08 '21 at 04:06
  • @Deepak checking the value returned by `scanf` is not about logic dealing with the inputs, it is validating that the input was received. Since the issue is getting input, it is best to include the check in the minimal solution. – William Pursell Jan 08 '21 at 13:23
0

There are two mistakes in your code:

  1. Wild pointer

char *name; is not a safe code. *name* is a wild pointer and it points to some arbitrary memory location and may cause a program to crash or behave badly. You should use an array like char name[10]. You can refer to this link.

  1. Comma in scanf

You can check this thread for more details

The comma is not considered a whitespace character so the format specifier "%s" will consume the , and everything else on the line writing beyond the bounds of the array sem causing undefined behaviour

I've just tried to modify your code, you should consider removing the comma, and replace it with a space

printf("Please enter your name, then weight separated by a space: ");
scanf("%s %f", name, &weight); // your code should work properly
dustin2022
  • 182
  • 2
  • 18
  • Defining a `char *` variable should not be described as "not a safe code". Attempting to dereference the uninitialized variable is unsafe, but defining it is perfectly valid and common. – William Pursell Jan 08 '21 at 13:25
0

I think you should be using a char array for the name variable and if you want to store its adress you would assign it to a pointer, doesn't make sense the way you wrote it

float weight;
char[20] name;
scanf(" %s, %f", &name, &weight); 
Ferdo99
  • 14
  • 4