Home > database >  In C Scanf not grabbing inputs past the first instance of scanf
In C Scanf not grabbing inputs past the first instance of scanf

Time:09-22

Coding in visual studio code

#include<stdio.h>

int main(){
    char firstLetterofName;
    int numOfVisits;
    float priceOfDrink;   //Creating the variables leaving them empty for now
    float total;

    printf("Hello! What is your name?\n");
    scanf("%c", &firstLetterofName);            //Asking for an inputting the first letter of users name

    printf("How many times have you visited Starbucks in a month\n");
    scanf("%d", &numOfVisits);      //Asking and inputting number of visits 

    printf("What is the price of the drink you order?\n");
    scanf("%f", &priceOfDrink);     //Asking and inputting price of drink

    total = priceOfDrink * numOfVisits;

    printf("Wow %c! You have spent $%d on Starbuck!", firstLetterofName, total);


    return 0;

}

First attempt using full name Terminal outputs

PS C:\C Cpp> cd "c:\C Cpp\" ; if ($?) { gcc test.c -o test } ; if ($?) { .\test }
Hello! What is your name?
Logan
How many times have you visited Starbucks in a month
What is the price of the drink you order?
Wow L! You have spent $0 on Starbuck!

Second attempt using only first letter

PS C:\C Cpp> cd "c:\C Cpp\" ; if ($?) { gcc test.c -o test } ; if ($?) { .\test }
Hello! What is your name?
L
How many times have you visited Starbucks in a month
5
What is the price of the drink you order?
2.0
Wow L! You have spent $0 on Starbuck!
PS C:\C Cpp>

Expected output is Wow L! You have spent $10.0 on starbucks

For the name part it is suppose to only take the first letter.

CodePudding user response:

In C Scanf not grabbing inputs past the first instance of scanf

With input "Logan\n", code scans in the 'L' as the firstLetterofName with the "ogan\n" reamaining in stdin for the next input function.

scanf("%c", &firstLetterofName); 

With input "ogan\n", scanf() fails and returns 0. numOfVisits was not changed. Code unfortunately did not not check the return value of scanf() to test success.

scanf("%d", &numOfVisits); // FAILED

To quickly discern scanf() troubles, check scanf() return values for success.

Do not rely on the user to enter compliant text. Robust code watches out for too much data, not enough data, non-numeric data, etc. User input is evil.

Consider using fgets() to read a line of user input and then parse the resultant string.

CodePudding user response:

#include<stdio.h>

int main(){
    char firstLetterofName;
    int numOfVisits;
    float priceOfDrink;   //Creating the variables leaving them empty for now
    float total;

    printf("Hello! What is your name?\n");
    scanf("%c", &firstLetterofName);            //Asking for an inputting the first letter of users name

    //clear the input stream
    fflush(stdin);


    printf("How many times have you visited Starbucks in a month\n");
    scanf("%d", &numOfVisits);      //Asking and inputting number of visits 

    printf("What is the price of the drink you order?\n");
    scanf("%f", &priceOfDrink);     //Asking and inputting price of drink

    total = priceOfDrink * numOfVisits;

    printf("Wow %c! You have spent $%f on Starbuck!", firstLetterofName, total);


    return 0;

}

Use fflush(stdin) to flush the input stream. Availabe in gcc

  • Related