Thread: Need help with rudimentary program in C

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    2

    Question Need help with rudimentary program in C

    So, I have a homework assignment with a part with instructions as follows:
    Part 1: Write a function called get_value() that prompts the user toenter a value of type double between 0.0 and 10,000.0. If the user correctlyenters a value in this range, then the value is returned to the calling function.Otherwise, a value of -1 is returned.

    So, I wrote the following program:

    Code:
    #include <stdio.h>
    
    
    
    
    double get_value()
    {
        double value = 0.0;
    
    
        printf("Enter a value of type double between 0.0 and 10,000.0: ");
        scanf("%d", &value);
    
    
        if ((value > 0) && (value < 10000))
            printf("%d\n", value);
        else
            value = -1;
            printf("%d", value);
    }
    
    
    int main()
    {
        get_value();
    
    
        return 0;
    }
    I'm not entirely sure what I'm doing wrong here. Whatever value I enter, it simply outputs it to the screen like the if-else statement doesn't even exist. Any help is greatly appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > scanf("%d", &value);
    %d is used for integers, not doubles.
    Read your manual page on printf / scanf format strings.

    Your get_value function lacks a return statement.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking for rudimentary syntax
    By deathslice in forum C Programming
    Replies: 3
    Last Post: 10-28-2015, 12:59 AM
  2. Replies: 2
    Last Post: 09-09-2014, 02:36 PM
  3. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  4. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  5. Rudimentary, but not obvious! (About .h files)
    By Axpen in forum C Programming
    Replies: 4
    Last Post: 08-12-2004, 11:18 AM

Tags for this Thread