Dev Shed Forums - C Programming http://forums.devshed.com/ C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems. You can also talk about C# implementation in our .NET section below, when applicable. en Tue, 16 Jan 2018 07:15:02 GMT vBulletin 60 http://forums.devshed.com/images/misc/rss.png Dev Shed Forums - C Programming http://forums.devshed.com/ C Programming - Incorrect value returned by float function http://forums.devshed.com/programming/979781-programming-incorrect-value-returned-float-function-new-post.html Sat, 06 Jan 2018 09:21:36 GMT Hello all, My first post here in the forum. I would like to thank you for an opportunity to learn from everyone in the forum. As a heads up, I will probably have a lot of questions as I am just starting to learn C. My first question: from the code below, I was supposed to get either a value of 2.0 or 3.0 for the carOneCharge variable upon executing the calculateCharges function, though I... Hello all,

My first post here in the forum. I would like to thank you for an opportunity to learn from everyone in the forum. As a heads up, I will probably have a lot of questions as I am just starting to learn C. My first question: from the code below, I was supposed to get either a value of 2.0 or 3.0 for the carOneCharge variable upon executing the calculateCharges function, though I instead I got -858993472? Is it a compiler error? I am using Microsoft Visual Studio Community 2013, running on Windows 10 with 64 bit OS.

Thanks,

Nobita

Code:

#include <stdio.h>
int main()
{
        int carOneHour=0;
        float carOneCharge = 10.0;

        printf("Enter hours for car 1:"); //get hour input
        scanf_s("%d", &carOneHour); //store hour
        carOneCharge = calculateCharges(carOneHour);

        system("pause"); //prevent console from closing
        return 0;
}
//Calculate charges
float calculateCharges(int x)
{
        float charge = 3.0;
        if (x <= 3)
        {
                charge = 2.0;
                return charge;
        }
        else
                return charge;
}

]]>
C Programming Nobita123 http://forums.devshed.com/programming-42/programming-incorrect-value-returned-float-function-979781.html