how to make ignorance in sequence scanf input

hi guys,
here in this code, it's working if I execute scanf functions one by one (independently, as I take others in comment line). However, I tried different variations but it's not working and regardless of how I search, I couldn't have figured out why it doesn't work on purpose. can you help, please?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <stdlib.h>

#define SIZE 20

int main(){


    char a[SIZE]={0};
    char b[SIZE]={0};
    char c[SIZE]={0};

    printf("Enter the string suzy as you see: ");
    scanf("%s\"%[^\"]\'%[^\']",a,b,c);

 /*   printf("Enter the string suzy as you see: ");
    scanf("%s",a);

    printf("Enter the string \"suzy\" as you see: ");
    scanf("\"%[^\"]",b);

    printf("Enter the string \'suzy\' as you see: ");
    scanf("\'%[^\']",c);
*/
    printf("\n\na The string suzy is %s",a);
    printf("\n\nb The string \"suzy\" is %s",b);
    printf("\n\nc The string \'suzy\' is %s\n\n\n\n\n",c);

    return 0;
}
Enter the string suzy as you see: suzy "suzy" 'suzy'


a The string suzy is suzy

b The string "suzy" is

c The string 'suzy' is

here is the code in sequential form
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <stdlib.h>

#define SIZE 20

int main(){


    char a[SIZE]={0};
    char b[SIZE]={0};
    char c[SIZE]={0};

  //  printf("Enter the string suzy as you see: ");
  //  scanf("%s\"%[^\"]\'%[^\']",a,b,c);

    printf("Enter the string suzy as you see: ");
    scanf("%s",a);

    printf("Enter the string \"suzy\" as you see: ");
    scanf("\"%[^\"]",b);

    printf("Enter the string \'suzy\' as you see: ");
    scanf("\'%[^\']",c);

    printf("\n\na The string suzy is %s",a);
    printf("\n\nb The string \"suzy\" is %s",b);
    printf("\n\nc The string \'suzy\' is %s\n\n\n\n\n",c);

    return 0;
}
Enter the string suzy as you see: suzy
Enter the string "suzy" as you see: Enter the string 'suzy' as you see:

a The string suzy is suzy

b The string "suzy" is

c The string 'suzy' is
The following seems to work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <stdlib.h>

#define SIZE 20

int main()
{
    char a[SIZE]={0};
    char b[SIZE]={0};
    char c[SIZE]={0};

    printf("Enter the string suzy as you see: ");
    scanf(" %s",a);

    printf("Enter the string \"suzy\" as you see: ");
    scanf(" \"%[^\"]\"",b);

    printf("Enter the string \'suzy\' as you see: ");
    scanf(" \'%[^\']\'",c);

    printf("\n\na The string suzy is %s",a);
    printf("\n\nb The string \"suzy\" is %s",b);
    printf("\n\nc The string \'suzy\' is %s\n\n\n\n\n",c);

    return 0;
}

I made two changes to the format strings:
1) Added space in front which will make scanf ignore any leading whitespace before trying to read the rest.
2) Added missing quotes (\" and \') at the end.
Last edited on
it doesn't make any sense to add \" or \' at the end, in [^\"] or [^\'] it will be ignored I don't get the point??
Last edited on
in origin of your code I made this change and it worked but still I am not sure why that happens.
first scanf
scanf("%s",a);
second scanf
scanf(" \"%[^\"]",b);
third scanf
scanf("\" \'%[^\']",c);
Last edited on
The extra \" is necessary to consume the ending double quote character from the user input.
1
2
"suzy"
     ^

If you don't do that it will be left in the input buffer and cause the next scanf to fail because it expects a single quote ' but finds a double quote "
Last edited on
What if user enters suzyismybestfriendandiwillalwaysloveher?
Topic archived. No new replies allowed.