Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8858793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:54:33+00:00 2026-06-14T14:54:33+00:00

I want to get the user input for variable L , but the scanf

  • 0

I want to get the user input for variable L, but the scanf function is not working, and the program will jump and print the next cost statement and exit if I try to input anything.
I am new to C, and hope can get some help here. Thanks. Code below:

 #include <stdio.h>
 #include <conio.h>

 int main()
 {
    float L = 0; // L is litre
    float gallon;
    gallon = 3.785 * L;
    char x[2] = {'u', 'd'}; // u is unleaded and d is diesel
    float cost;
    printf("Hello, welcome to PetrolUpHere!!\n");
    printf("Would u like unleaded or diesel fuel?");
    scanf("%s", &x[2]);
    printf("Enter the litre you want to fuel:");
    scanf("%.2f", &L); //SCANF NOT WORKING
    switch (x[2]) {
        case 'u':
            cost = 1.98 * gallon;
            printf("The cost is :%.2f ", cost);
            break;
        case 'd':
            cost = 1.29*gallon;
            printf("The cost is :%.2f ",cost);
            break;
    }
    getch();
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:54:34+00:00Added an answer on June 14, 2026 at 2:54 pm

    There are a number of problems here:

    scanf("%s", &x[2]);
    

    I imagine you wanted to read a string into the variable x. Instead, you’re saying “read a string into memory 2 positions past where x points”. In this case that memory will be out of bounds. You should do this, since you only care about one character:

    char input;
    scanf("%c", &input);
    

    Your switch statement is similarly broken; x[2] is again out of bounds. Use input from the above code instead.

    As others have pointed out, using %.2f is not what you want to do when reading in L. Use %f instead. Generally you should only do something like that with format specifiers when printing out variables, rather than reading them in. Eventually you won’t be using scanf anyway, since it’s not a particularly safe way of getting input.

    Finally: it seems like your understanding of how C strings work is shaky at best. This is understandable, since this is a fairly confusing topic for anyone who hasn’t worked in C before, and especially for novice programmers. Here’s one explanation; I’m sure you can find many more, probably better ones if you look.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get user input in one page, store that in a php
I want to get the date today, and set the hour to a user-input
I want to get the user mail address, as shown in this thread :
i want to get the user who login now .. how to do this
I want to get the user ID of a given process ID using C
If I want to get a user that has the email address of 'me@example.com',
I'm trying to get to grips with web2py/python. I want to get the user
I want to get the following information of a user that is captured using
i want to get the full address of user programatically using c# how can
I want to get the home directory of a user of a unix system.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.