New Posts  All Forums:Forum Nav:

Help in C programming

post #1 of 11
Thread Starter 
Hey there. Im taking a beginners course in c programming and i'm stuck. Im trying to add an integer and double, but the sum is showing only the integer value and not the result? here's the code:

int main (void)
{
// declare variables
int a;
double b;
double purchasePrice;

printf("\nNumber of items purchased\n");
scanf("%d",&a);

// (printf)Prompt user for number of items

// Take user input for number of items (scanf)

printf("\nEnter the Unit Price\n");
scanf("%.2f",&b);

// (printf) Prompt for unit price

// take user input for unit price (scanf)
purchasePrice = a + b;
printf("\nPurchase Price is %.2f\n",purchasePrice);
post #2 of 11
You'll have to cast the int into a double.
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 2600k Asus Maximus IV Extreme evga gtx 670 ftw G.Skill Ripjaws X 
Hard DriveOptical DriveCoolingOS
Western Digital Caviar Black 1TB 7200 RPM 64MB ... LITE-ON Black 12X BD-R 2X BD-RE 16X DVD+R 12X D... Noctua NH-D14 Windows 7 Professional x64 
MonitorKeyboardPowerCase
DELL Ultrasharp 2410 Saitek Cyborg Corsair HX850 Cooler Master HAF-X 
Mouse
logitech g500 
  hide details  
Reply
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 2600k Asus Maximus IV Extreme evga gtx 670 ftw G.Skill Ripjaws X 
Hard DriveOptical DriveCoolingOS
Western Digital Caviar Black 1TB 7200 RPM 64MB ... LITE-ON Black 12X BD-R 2X BD-RE 16X DVD+R 12X D... Noctua NH-D14 Windows 7 Professional x64 
MonitorKeyboardPowerCase
DELL Ultrasharp 2410 Saitek Cyborg Corsair HX850 Cooler Master HAF-X 
Mouse
logitech g500 
  hide details  
Reply
post #3 of 11
Quote:
Originally Posted by corrinthians View Post

You'll have to cast the int into a double.

This. To do the cast just type:
Code:
purchasePrice = (double)a + b;
Ti-89 Titanium
(13 items)
 
   
CPUMotherboardRAMHard Drive
Motorola 68k @16 MHz Ti-89 HW4 256 KB 2.6 MB Flash ROM 
OSMonitorPowerCase
Ti-89 OS 3.10 LCD Dot-Matrix 160x100 4x 1.5V AAA Ti-89 HW4 Black 
CPUMotherboardGraphicsRAM
Intel Core™ i7 930 Gigabyte GA-X58A-UD3R 2.0 Gigabyte GTX 470 SOC G.Skill RX 2x4GB 1600C8 
Hard DriveHard DriveOptical DriveCooling
PNY Prevail Elite 120GB Hitachi 7K1000.C 1TB LG GH24LS50 Noctua NH-C12P SE14 
OSOSMonitorKeyboard
Windows 8 Pro x64 Lubuntu 13.10 x64 Samsung BX2250 Logitech Illuminated 
PowerCaseMouseAudio
Be Quiet! S.P. E⁷ 600W Fractal Design Core 3000 Logitech G300 AKG K240 Studio 
  hide details  
Reply
Ti-89 Titanium
(13 items)
 
   
CPUMotherboardRAMHard Drive
Motorola 68k @16 MHz Ti-89 HW4 256 KB 2.6 MB Flash ROM 
OSMonitorPowerCase
Ti-89 OS 3.10 LCD Dot-Matrix 160x100 4x 1.5V AAA Ti-89 HW4 Black 
CPUMotherboardGraphicsRAM
Intel Core™ i7 930 Gigabyte GA-X58A-UD3R 2.0 Gigabyte GTX 470 SOC G.Skill RX 2x4GB 1600C8 
Hard DriveHard DriveOptical DriveCooling
PNY Prevail Elite 120GB Hitachi 7K1000.C 1TB LG GH24LS50 Noctua NH-C12P SE14 
OSOSMonitorKeyboard
Windows 8 Pro x64 Lubuntu 13.10 x64 Samsung BX2250 Logitech Illuminated 
PowerCaseMouseAudio
Be Quiet! S.P. E⁷ 600W Fractal Design Core 3000 Logitech G300 AKG K240 Studio 
  hide details  
Reply
post #4 of 11
It's nice to add an explanation.

Why cast? Because you're adding a integer to a double and the compiler will automatically cast the double to integer, losing all decimal cases, and returning an integer to the purchaseValue. When specifically casting the integer to a double, (double) a, you're saying to the compiler to treat a as a double instead of an integer, in that specific operation.
Rubbish
(15 items)
 
  
CPUMotherboardGraphicsRAM
E7400 @ 4ghz 1.376v Asus P5Q3 Sapphire HD6870 Kingston 4GB DDR3 1333MHZ 
Hard DriveHard DriveOSOS
Samsung Spinpoint F3 WD Green Windows 7 64bit Lubuntu 64bit 
PowerCaseMouse
OCZ ModXStream 700W Cooler Master 690 II with side window Razer Deathadder Black Edition 
  hide details  
Reply
Rubbish
(15 items)
 
  
CPUMotherboardGraphicsRAM
E7400 @ 4ghz 1.376v Asus P5Q3 Sapphire HD6870 Kingston 4GB DDR3 1333MHZ 
Hard DriveHard DriveOSOS
Samsung Spinpoint F3 WD Green Windows 7 64bit Lubuntu 64bit 
PowerCaseMouse
OCZ ModXStream 700W Cooler Master 690 II with side window Razer Deathadder Black Edition 
  hide details  
Reply
post #5 of 11
Thread Starter 
thnx u guys. but it still doesnt work frown.gif
post #6 of 11
If you try printing b directly after your scanf call, you will see that b is actually zero no matter what value you enter. This is because you are telling scanf to interpret the input as a float, but you are trying to store it in a double.

You must use %lf in your scanf to read the input as a double. Alternatively, you can store your numbers as floats instead of doubles (the precision difference won't matter for your code).

To everyone suggesting a cast. The integer will be automatically promoted to a float in the addition, so the cast has no effect.

ps. Please use code tags in future smile.gif
Daily
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K @ 4.8GHz, 1.4V Maximus IV Extreme GTX 560 DCII TOP 4x4GB Kingston HyperX 1866MHz CL11 
Hard DriveHard DriveCoolingCooling
Kingston HyperX 3K 120GB Samsung 640GB Thermochill PA120.2 Jingway DB-1 Pump 
CoolingCoolingOSMonitor
2x Kaze Jyuni 1900RPM EK Supreme HF Copper plexi Arch Linux x64 Acer X223HQ 1920x1080 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750 Modded Corsair Carbide 500R Logitech M500 
  hide details  
Reply
Daily
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K @ 4.8GHz, 1.4V Maximus IV Extreme GTX 560 DCII TOP 4x4GB Kingston HyperX 1866MHz CL11 
Hard DriveHard DriveCoolingCooling
Kingston HyperX 3K 120GB Samsung 640GB Thermochill PA120.2 Jingway DB-1 Pump 
CoolingCoolingOSMonitor
2x Kaze Jyuni 1900RPM EK Supreme HF Copper plexi Arch Linux x64 Acer X223HQ 1920x1080 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750 Modded Corsair Carbide 500R Logitech M500 
  hide details  
Reply
post #7 of 11
Code:
int main(void) {
        int a;
        double b;

        printf("\nNumber of items purchased\n");
        scanf("%d", &a);;

        printf("\nEnter the Unit Price\n");
        scanf("%lf", &b);
        
        printf("\nPurchase Price is %.2lf", a + b);
}

That should work for you, the problem was you need to use %lf for doubles (long float), %f is for float.
    
CPUMotherboardGraphicsRAM
Intel Core i7 5820K EVGA X99 Micro2 EVGA GTX 980 32GB DDR4-2400 
Hard DriveOSMonitorPower
Samsung 850 Pro Windows 10 x64 Pro Qnix 1440p EVGA 850W Gold 
Case
CaseLabs Mercury S5 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i7 5820K EVGA X99 Micro2 EVGA GTX 980 32GB DDR4-2400 
Hard DriveOSMonitorPower
Samsung 850 Pro Windows 10 x64 Pro Qnix 1440p EVGA 850W Gold 
Case
CaseLabs Mercury S5 
  hide details  
Reply
post #8 of 11
Thread Starter 
thanx all u guys!!
post #9 of 11
Thread Starter 
this works yay! smile.gif
post #10 of 11
Quote:
Originally Posted by RAND0M1ZER View Post

That should work for you, the problem was you need to use %lf for doubles (long float), %f is for float.

Noticed that too but what I didn't understand is why "%.2lf" and not just "%lf".
Ti-89 Titanium
(13 items)
 
   
CPUMotherboardRAMHard Drive
Motorola 68k @16 MHz Ti-89 HW4 256 KB 2.6 MB Flash ROM 
OSMonitorPowerCase
Ti-89 OS 3.10 LCD Dot-Matrix 160x100 4x 1.5V AAA Ti-89 HW4 Black 
CPUMotherboardGraphicsRAM
Intel Core™ i7 930 Gigabyte GA-X58A-UD3R 2.0 Gigabyte GTX 470 SOC G.Skill RX 2x4GB 1600C8 
Hard DriveHard DriveOptical DriveCooling
PNY Prevail Elite 120GB Hitachi 7K1000.C 1TB LG GH24LS50 Noctua NH-C12P SE14 
OSOSMonitorKeyboard
Windows 8 Pro x64 Lubuntu 13.10 x64 Samsung BX2250 Logitech Illuminated 
PowerCaseMouseAudio
Be Quiet! S.P. E⁷ 600W Fractal Design Core 3000 Logitech G300 AKG K240 Studio 
  hide details  
Reply
Ti-89 Titanium
(13 items)
 
   
CPUMotherboardRAMHard Drive
Motorola 68k @16 MHz Ti-89 HW4 256 KB 2.6 MB Flash ROM 
OSMonitorPowerCase
Ti-89 OS 3.10 LCD Dot-Matrix 160x100 4x 1.5V AAA Ti-89 HW4 Black 
CPUMotherboardGraphicsRAM
Intel Core™ i7 930 Gigabyte GA-X58A-UD3R 2.0 Gigabyte GTX 470 SOC G.Skill RX 2x4GB 1600C8 
Hard DriveHard DriveOptical DriveCooling
PNY Prevail Elite 120GB Hitachi 7K1000.C 1TB LG GH24LS50 Noctua NH-C12P SE14 
OSOSMonitorKeyboard
Windows 8 Pro x64 Lubuntu 13.10 x64 Samsung BX2250 Logitech Illuminated 
PowerCaseMouseAudio
Be Quiet! S.P. E⁷ 600W Fractal Design Core 3000 Logitech G300 AKG K240 Studio 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming