0

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;
}
PlanSeekX
  • 19
  • 4
  • 1
    Please see: [Why not use Double or Float to represent currency?](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) – Weather Vane Mar 23 '19 at 22:55
  • 2
    With `char ask;`: `scanf("%s", &ask);` --> `scanf(" %c", &ask);` – dbush Mar 23 '19 at 23:00
  • 1st rule of accepting data from an external source (e.g. user): assume the external source is malicious and sanity check everything (and never ignore return values of functions). 1st rule of user interface design: always provide useful feedback when the user does something unexpected/unsupported (e.g. types "four" when you're asking for a number). – Brendan Mar 24 '19 at 01:42
  • You've made an incorrect assumption somewhere. There's nothing wrong with your `priceDifference` function. `priceDifference(2105.11, 1999.55)` returns *approximately* `-105.56`. Try temporarily adding this to the beginning of your `main` function: `printf("%f\n", priceDifference(2105.11, 1999.55)); return 0;` and you'll see what I mean. – Keith Thompson Mar 24 '19 at 01:47
  • Please read this: [mcve] .If fixing the `scanf` formats fixes the problem, you're done. If not, you need to show us what input you gave your program, what output it produced, and how that differed from what you expected. – Keith Thompson Mar 24 '19 at 01:53

2 Answers2

0

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.

PlanSeekX
  • 19
  • 4
  • using the wrong format specifier [invokes undefined behavior](https://stackoverflow.com/q/16864552/995714) which means anything can happen, including files on the HDD being destroyed or [nasal demons fly out of your nose](http://catb.org/jargon/html/N/nasal-demons.html). If it works in your PC then you're unlucky that the low byte of the address is equal to the character. But in most cases that shouldn't work – phuclv Mar 24 '19 at 04:00
0

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.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312