-1

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.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • 1
    Welcome to SO. You should always check return value of `scanf` and friends. As `"ogan"` is not a valid number, it is not consumed from the buffer and `scanf` returns 0 on second call – Gerhardh Sep 21 '22 at 20:18
  • 2
    Also, you use `%d` on a `float` in `scanf("%d", &numOfVisits);`. Your compiler should emit a warning on this. – Chris Sep 21 '22 at 20:19
  • 2
    You should also enable compiler warnings. It should tell you that you are passing wrong type of argument for `printf`. You tell the function that you pass an integer but pass a `float` what is converted to `double`. Use `%f` instead of `%d`. – Gerhardh Sep 21 '22 at 20:20
  • What about the second attempt using only L? It lets me input the numbers as expect but the math doesn't come out correctly? – Logan Swinney Sep 21 '22 at 20:20
  • 2
    That is because you are cheating. You promise you provide an `int` but you sneak in a `double` as I mentioned above. The input is correct. Your output is broken. Running your code in a debugger would show that immediately. And this should always be your first action to search for issues. – Gerhardh Sep 21 '22 at 20:21
  • You should put a space before `%c` to consume any leading whitespace. `scanf(" %c", &firstLetterofName);` Not your issue right now but move that statement after another `scanf` or put it in a loop and it will be. Read [scanf() leaves the newline character in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-newline-character-in-the-buffer) for more details. – Retired Ninja Sep 21 '22 at 20:21
  • Oh I see. How exactly do I enable compiler warning on visual studio? – Logan Swinney Sep 21 '22 at 20:22
  • You must modify your `tasks.json` file to provide additional options. For GCC use `-Wall -Wextra -pedantic`. – Gerhardh Sep 21 '22 at 20:23
  • Have you been coerced into writing comments at every line? If not, I think function comments would explain your code better. – Neil Sep 21 '22 at 20:25
  • 1
    If you put comments in the code that only describe the obvious, they should at least be correct. You are *NOT* asking to enter first letter of the name. You ask for the name but only accept the first letter. Why would any sane user not enter the full first name if you ask for it? – Gerhardh Sep 21 '22 at 20:33
  • The comments are due to the class. You are correct on the comment being wrong obviously I've fixed that. I've also changed the %d to a %f yet the final output is still saying the total is 0. I apologize I'm not familiar with Visual studio how do I edit the tasks.json? and again not used to visual studio code I'm unfamiliar with its debugger – Logan Swinney Sep 21 '22 at 20:48
  • I apologize I've fixed the final output. I would still like to know how to edit the tasks.json – Logan Swinney Sep 21 '22 at 20:51
  • There's no such thing as an "empty variable", only "uninitialised variables"... Initialise all variables when they first appear. – Fe2O3 Sep 21 '22 at 21:24

2 Answers2

0

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.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
-1
#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

KITISH
  • 18
  • 3