简体   繁体   中英

Why doesnt my float function work as intended?

My function priceDifference inside startShare's if statement at the end is not returning the calculated float numbers and displaying it in console. My other functions are working properly its just that function that is going bananas and I have no idea why.

For example, when I pass the numbers 2105.11 & 1999.55 it returns 1999.55???

#include <stdio.h>

void shareStart();
char askMe();
char askMe2();
char askMe3();
char askAgain();
int getShares();
int getMoney();
float getStartingInvestment();
float getSoldInvestment();
float getPrice();
float shareDivide(float, int);
float shareMultiply(float, int);
float priceDifference(float, float);

int main()
{
    shareStart();
    return 0;
}

void shareStart()
{
    do {
        if(askMe() == 'y') {
         printf("You could buy: %f shares.\n", shareDivide(getPrice(), getMoney()));
        } else if(askMe2() == 'y') {
          printf("Shares cost: %f\n", shareMultiply(getPrice(), getShares()));
        } else if(askMe3() == 'y') {
            printf("Profit/Loss is: %f\n", priceDifference(getStartingInvestment(), getSoldInvestment()));
        }
    } while(askAgain() == 'y');
}

char askMe()
{
    char ask;
    printf("See how many shares to buy? 'y/n'\n");
    scanf("%s", &ask);
    return ask;
}

char askMe2()
{
    char ask;
    printf("See total cost of shares? 'y/n'\n");
    scanf("%s", &ask);
    return ask;
}

char askMe3()
{
    char ask;
    printf("See profit/loss difference between trades? 'y/n'\n");
    scanf("%s", &ask);
    return ask;
}

char askAgain()
{
    char ask;
    printf("Would you like to run the program again? 'y/n'\n");
    scanf("%s", &ask);
    return ask;
}

int getShares()
{
    int ask;
    printf("How many shares are you using?\n");
    scanf("%d", &ask);
    return ask;
}

int getMoney()
{
    int money;
    printf("How much money are you using?\n");
    scanf("%d", &money);
    return money;
}

float getStartingInvestment() 
{
    float money;
    printf("How much money did your shares cost?\n");
    scanf("%f", &money);
    return money;
}

float getSoldInvestment()
{
    float money;
    printf("What did you sold your shares for?\n");
    scanf("%f", &money);
    return money;
}

float getPrice()
{
    float price;
    printf("Whats the price of a share?\n");
    scanf("%f", &price);
    return price;
}

float shareDivide(float price, int money) 
{
    return money / price;
}

float shareMultiply(float price, int shares) 
{
    return shares * price;
}

float priceDifference(float start, float sold)
{
    return sold - start;
}

I dont know exactly how it works, but the answer as stated in the comments is changing scanf function format specifier to " %c" in all ask functions.

Execution with "%s" specifier ~~ Execution with " %c" specifier


I ran the program with the specifier "%s" in my laptops command prompt and it works fine. Something tells me that the online compiler is causing it to work unexpectedly.

The %s specification to fscanf tells it to read a sequence of non-white-space characters and store them, along with a terminating null character, in the space for characters pointed to by the argument.

When ask is defined with char ask , passing &ask passes a pointer to space for a single character. This is not big enough to hold both a character scanned from input and a terminating null character. In consequence, your program nominally attempts to write a null character to memory not allocated for it. This can break your program in any number of ways.

Changing the fscanf string to %c tells it to read a single character and store it in the place pointed to by the argument, without a terminating null character. This fixes the problem since data is written only to properly allocated space.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM