0

What is wrong with the scanf() to get in the string on the second time, I can't input my string on the second time. I am not sure with the error that occurs, I can't get this program function well

#include <stdio.h>
#include <stdlib.h>

int main()
{
    //variables decleration
    char staff_name1[31];
    char staff_name2[31];
    float sales1, sales2;

    //input
    printf("Enter staff name\t> ");
    scanf("%[^\n]s", staff_name1);

    printf("Enter sales amount\t> ");
    scanf("%f", &sales1);

    printf("\nEnter staff name \t> ");//ERROR,CAN'T INPUT MY STRING
    fflush(stdin);
    scanf("%[^\n]s", staff_name2);

    printf("\nEnter sales amount\t> ");
    scanf("%f", &sales2);

    printf("\n");

    //output
    printf("Staff Name\t\t\t\tSales Amount\n");
    printf("===================\t\t=============\n");
    printf("%-20s \t%12.2f\n", staff_name1, sales1);
    printf("%-20s \t%12.2f\n", staff_name2, sales2);


}

my output of this code is as below:

warning: this program uses gets(), which is unsafe.

Enter staff name   > kh s
Enter sales amount > 134.14

Enter staff name   > 
Enter sales amount > 141243.14

Staff Name              Sales Amount
===================     =============
kh s                          134.14
                           141243.14

I can't input the second staff name. Can anyone please help me solve this??

WedaPashi
  • 3,561
  • 26
  • 42
HSKang
  • 25
  • 4

2 Answers2

1
fflush(stdin);

is undefined behaviour in standard C. To flush the newline character, you could simply use getchar() instead.

printf("\nEnter staff name \t> ");
getchar();
scanf("%[^\n]s", staff_name2);

I would also use fgets() instead of scanf to read a line and trim the newline if necessary, which offers better control over invalid inputs being entered by user and against buffer overflows.

P.P
  • 117,907
  • 20
  • 175
  • 238
0

You have three problems.

  1. I see that you use %[^\n]s. It is wrong. The s isn't part of the %[ specifier. So use %[^\n] instead of %[^\n]s
  2. After you enter the value for sales1, you press Enter. This character stays in the stdin(standard input stream). And when the next character for %[^\n] is \n, it will fail. Fix this problem by adding a space before %[^\n].
  3. Using fflush on stdin invokes Undefined Behavior as per the C11 standard, although the behavior is well defined in some implementations. It is better to remove it so that your code will be more portable.

Additional notes:

  • You can limit the amount of characters to be scanned so that you can avoid buffer overflows.
  • You can check the return value of scanf to make sure it is successful. All the scanf in your program will return 1 on success.


Fixed Program:
#include <stdio.h>
#include <stdlib.h> //Unused header

int main()
{
    char staff_name1[31];
    char staff_name2[31];
    float sales1, sales2;


    printf("Enter staff name\t> ");
    if(scanf(" %30[^\n]", staff_name1) != 1)
    {
        printf("Could not scan staff_name1");
        return -1;   //Exit main with a return value of -1
    }

    printf("Enter sales amount\t> ");
    if(scanf("%f", &sales1) != 1)
    {
        printf("Could not scan sales1");
        return -1;   //Exit main with a return value of -1
    }

    printf("\nEnter staff name \t> ");
    //fflush(stdin); UB!
    if(scanf(" %30[^\n]", staff_name2) != 1)
    {
        printf("Could not scan staff_name2");
        return -1;   //Exit main with a return value of -1
    }

    printf("\nEnter sales amount\t> ");
    if(scanf("%f", &sales2) != 1)
    {
        printf("Could not scan sales2");
        return -1;   //Exit main with a return value of -1
    }

    printf("\n");

    //output
    printf("Staff Name\t\t\t\tSales Amount\n");
    printf("===================\t\t=============\n");
    printf("%-20s \t%12.2f\n", staff_name1, sales1);
    printf("%-20s \t%12.2f\n", staff_name2, sales2);


}
Spikatrix
  • 20,225
  • 7
  • 37
  • 83