C - Help

+1 Robert Summers · December 15, 2014
So i have used typecasting and am stuck as to why, when I run my program I enter 10 for pumpkins sold and it gives me zero not 50 as I calculated before hand, why is this. 
#include <stdio.h>

int main()
    float moneyMade = 0;
    int pumpkinPrice = 5;
    int pumpkinsSold = 0;

    printf("How many pumpkins did you sell: ");
    scanf(" %.2f", &pumpkinsSold);

    moneyMade = ((float)pumpkinPrice * (float)pumpkinsSold);

    printf("Money made = %.2f", moneyMade);

    return 0;

Thank you for your help in return :)

Post a Reply


Oldest  Newest  Rating
0 c student · December 15, 2014
int pumpkinsSold = 0;  // declared as integer type

scanf (" %.2d", &pumpkinsSold);  // need to change your scan type as an integer "%d"
0 Sagor Chowdhury · December 15, 2014
pumpkinsold has to int type.. you have to use %d
0 Robert Summers · December 15, 2014
Still returns 00 on my prompt
0 c student · December 15, 2014
What are you using to compile your program?  You might be having all of these errors because your program is compiling and ignoring potential errors and warnings which might cause weird behaviour.  

Your scanf() function should be:
 scanf("%d", &pumpkinsSold);
0 Joe Gonzalez · December 16, 2014
How would you write a summation problem such as   2a2/6a+b3  + 4a3/12a+b4 .....to the nth term( in others words infinite). I just need help with writing the math into code. I know how to do everything else. Thank you
0 c student · December 16, 2014
you can easily break down your formula into separate smaller ones however, you would be looking for a general algorithm for your specific mathematical expression such as some function of the likes of:
for (n=1; ;n++) {
        result += (((2*n)*pow(a,n+1))/((6*n)*pow(a,n))+pow(b,n+2));

for expressions which would sum towards infinity, you require another expression or some kind of limit-testing function (to see if it converges or diverges) which would resolve your problem because simply summing numbers to infinity will obviously yield an infinite "number" result and this simply won't be appropriate.  
0 Joe Gonzalez · December 17, 2014
Thank you.
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator