skip to Main Content

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 monthn");
    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.

2

Answers


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

    With input "Logann", code scans in the 'L' as the firstLetterofName with the "ogann" reamaining in stdin for the next input function.

    scanf("%c", &firstLetterofName); 
    

    With input "ogann", 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.

    Login or Signup to reply.
  2. #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 monthn");
        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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search