Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Simple addition in C, I fail so much?
New Posts  All Forums:Forum Nav:

Simple addition in C, I fail so much?

post #1 of 10
Thread Starter 
Okay, I am just trying to add two numbers the user types..What am I doing wrong? All I get is one of the two variables instead of a result!

Code:
#include <stdio.h>
#include <stdlib.h>

int main()

{

    char numa;
    char numb;
    char sum;
    
    printf("Enter the first number:");
    gets(&numa);
    printf("Enter the second number:");
    gets(&numb);
    numa=atoi(&numa);
    numb=atoi(&numb);
    sum=numa+numb;
    printf("The sum is:%d", sum);
    getchar();
    return (0);
}
Please help the newbie!

If I enter 2 then 4, I get the result 4! How is this possible?
Edited by AngeloG. - 8/15/11 at 11:14am
Current System
(18 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 @ 4GHz Gigabyte 990XA-UD3 AMD Radeon R9 380X Kingston 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Pro EVO Seagate 500GB LG DVD Burner 3x Coolermaster 80mm + 2 Thermaltake 200mm 
OSMonitorMonitorKeyboard
Windows 10 x64 LG TV LG Flatron W2361V Logitech K200 
PowerCaseMouseMouse Pad
Enermax Galaxy 850W Thermaltake Element S Logitech G502 Proteus Spectrum RGB Steelseries QcK 
AudioAudio
Realtec HDA Lexicon Omega 
  hide details  
Reply
Current System
(18 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 @ 4GHz Gigabyte 990XA-UD3 AMD Radeon R9 380X Kingston 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Pro EVO Seagate 500GB LG DVD Burner 3x Coolermaster 80mm + 2 Thermaltake 200mm 
OSMonitorMonitorKeyboard
Windows 10 x64 LG TV LG Flatron W2361V Logitech K200 
PowerCaseMouseMouse Pad
Enermax Galaxy 850W Thermaltake Element S Logitech G502 Proteus Spectrum RGB Steelseries QcK 
AudioAudio
Realtec HDA Lexicon Omega 
  hide details  
Reply
post #2 of 10
Quote:
Originally Posted by AngeloG. View Post
Okay, I am just trying to add two numbers the user types..What am I doing wrong? All I get is one of the two variables instead of a result!

Code:
#include <stdio.h>
#include <stdlib.h>

int main()

{

    char numa;
    char numb;
    char sum;
    
    printf("Enter the first number:");
    gets(&numa);
    printf("Enter the second number:");
    gets(&numb);
    numa=atoi(&numa);
    numb=atoi(&numb);
    sum=numa+numb;
    printf("The sum is:%d", sum);
    getchar();
    return (0);
}
Please help the newbie!

If I enter 2 then 4, I get the result 4! How is this possible?

Is there a reason sum is a char and not an int?
Also try declaring the int variables as int so your assignment looks like
int int_numa;
int_numa = atoi(&numa);
i7
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 D0 w/ HT On [4.01ghz @ 1.208v] Asus P6X58D Premium Asus HD5870 G.SKILL 6GB (3X2GB) DDR3 @ 1528MHz 
Hard DriveOptical DriveOSMonitor
OCZ Vertex Turbo 60GB FW1.5 2xSamsung Spinpoint F3 LG 8x Blu-ray Combo Drive Windows 7 64-bit Dell U2410 Rev. A04 
PowerCase
OCZ ModXStream Pro 700W V2.2 HAF 922 
  hide details  
Reply
i7
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 D0 w/ HT On [4.01ghz @ 1.208v] Asus P6X58D Premium Asus HD5870 G.SKILL 6GB (3X2GB) DDR3 @ 1528MHz 
Hard DriveOptical DriveOSMonitor
OCZ Vertex Turbo 60GB FW1.5 2xSamsung Spinpoint F3 LG 8x Blu-ray Combo Drive Windows 7 64-bit Dell U2410 Rev. A04 
PowerCase
OCZ ModXStream Pro 700W V2.2 HAF 922 
  hide details  
Reply
post #3 of 10
Thread Starter 
Here is the code I am using now, provided by Lordicon, but it crashes.

Code:

#include <stdio.h>
#include <stdlib.h>


int main()

{

    char* numa = NULL;
    char* numb = NULL;
    int sum = 0;
    
    printf("Enter the first number:\
");
    scanf("%s", numa);
    printf("Enter the second number:\
");
    scanf("%s", numb);
    sum = atoi(numa) + atoi(numb);
    printf("The sum is:%d\
", sum);
    scanf("%s" , numa);
}
For some reason, it crashes. I have no idea why.
Current System
(18 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 @ 4GHz Gigabyte 990XA-UD3 AMD Radeon R9 380X Kingston 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Pro EVO Seagate 500GB LG DVD Burner 3x Coolermaster 80mm + 2 Thermaltake 200mm 
OSMonitorMonitorKeyboard
Windows 10 x64 LG TV LG Flatron W2361V Logitech K200 
PowerCaseMouseMouse Pad
Enermax Galaxy 850W Thermaltake Element S Logitech G502 Proteus Spectrum RGB Steelseries QcK 
AudioAudio
Realtec HDA Lexicon Omega 
  hide details  
Reply
Current System
(18 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 @ 4GHz Gigabyte 990XA-UD3 AMD Radeon R9 380X Kingston 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Pro EVO Seagate 500GB LG DVD Burner 3x Coolermaster 80mm + 2 Thermaltake 200mm 
OSMonitorMonitorKeyboard
Windows 10 x64 LG TV LG Flatron W2361V Logitech K200 
PowerCaseMouseMouse Pad
Enermax Galaxy 850W Thermaltake Element S Logitech G502 Proteus Spectrum RGB Steelseries QcK 
AudioAudio
Realtec HDA Lexicon Omega 
  hide details  
Reply
post #4 of 10
I think it would be better to use ints instead of chars, and you have to use &numa and &numb in scanf, WITH an ampersand.

You should scanf for %d, integers, not strings.
Also, sum = numa + numb; that atoi stuff isn't required.
post #5 of 10
Thread Starter 
Quote:
Originally Posted by crust_cheese View Post
I think it would be better to use ints instead of chars, and you have to use &numa and &numb in scanf, WITH an ampersand.

You should scanf for %d, integers, not strings.
Also, sum = numa + numb; that atoi stuff isn't required.
Check out the post above you. Thanks for the reply.
Current System
(18 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 @ 4GHz Gigabyte 990XA-UD3 AMD Radeon R9 380X Kingston 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Pro EVO Seagate 500GB LG DVD Burner 3x Coolermaster 80mm + 2 Thermaltake 200mm 
OSMonitorMonitorKeyboard
Windows 10 x64 LG TV LG Flatron W2361V Logitech K200 
PowerCaseMouseMouse Pad
Enermax Galaxy 850W Thermaltake Element S Logitech G502 Proteus Spectrum RGB Steelseries QcK 
AudioAudio
Realtec HDA Lexicon Omega 
  hide details  
Reply
Current System
(18 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 @ 4GHz Gigabyte 990XA-UD3 AMD Radeon R9 380X Kingston 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Pro EVO Seagate 500GB LG DVD Burner 3x Coolermaster 80mm + 2 Thermaltake 200mm 
OSMonitorMonitorKeyboard
Windows 10 x64 LG TV LG Flatron W2361V Logitech K200 
PowerCaseMouseMouse Pad
Enermax Galaxy 850W Thermaltake Element S Logitech G502 Proteus Spectrum RGB Steelseries QcK 
AudioAudio
Realtec HDA Lexicon Omega 
  hide details  
Reply
post #6 of 10
This is how I would write it:
Code:
#include <stdio.h>

int main(void) {
 int numa, numb, sum;
 printf("Enter the first number:\
");
 scanf("%d", &numa);
 printf("Enter the second number:\
");
 scanf("%d", &numb);

 sum = numa + numb;
 printf("The sum is: %d", sum);

 return 0;
}
post #7 of 10
Thread Starter 
The program ends (normally), right after I enter the second number. Can I avoid that?
Current System
(18 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 @ 4GHz Gigabyte 990XA-UD3 AMD Radeon R9 380X Kingston 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Pro EVO Seagate 500GB LG DVD Burner 3x Coolermaster 80mm + 2 Thermaltake 200mm 
OSMonitorMonitorKeyboard
Windows 10 x64 LG TV LG Flatron W2361V Logitech K200 
PowerCaseMouseMouse Pad
Enermax Galaxy 850W Thermaltake Element S Logitech G502 Proteus Spectrum RGB Steelseries QcK 
AudioAudio
Realtec HDA Lexicon Omega 
  hide details  
Reply
Current System
(18 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 @ 4GHz Gigabyte 990XA-UD3 AMD Radeon R9 380X Kingston 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Pro EVO Seagate 500GB LG DVD Burner 3x Coolermaster 80mm + 2 Thermaltake 200mm 
OSMonitorMonitorKeyboard
Windows 10 x64 LG TV LG Flatron W2361V Logitech K200 
PowerCaseMouseMouse Pad
Enermax Galaxy 850W Thermaltake Element S Logitech G502 Proteus Spectrum RGB Steelseries QcK 
AudioAudio
Realtec HDA Lexicon Omega 
  hide details  
Reply
post #8 of 10
Thread Starter 
crust_cheese's code works just fine, however if I run the .exe directly, the program ends before I can read anything, and I don't know how to avoid that. The only way to see the result is open cmd, and point it to the .exe via its path.
Current System
(18 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 @ 4GHz Gigabyte 990XA-UD3 AMD Radeon R9 380X Kingston 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Pro EVO Seagate 500GB LG DVD Burner 3x Coolermaster 80mm + 2 Thermaltake 200mm 
OSMonitorMonitorKeyboard
Windows 10 x64 LG TV LG Flatron W2361V Logitech K200 
PowerCaseMouseMouse Pad
Enermax Galaxy 850W Thermaltake Element S Logitech G502 Proteus Spectrum RGB Steelseries QcK 
AudioAudio
Realtec HDA Lexicon Omega 
  hide details  
Reply
Current System
(18 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 @ 4GHz Gigabyte 990XA-UD3 AMD Radeon R9 380X Kingston 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Pro EVO Seagate 500GB LG DVD Burner 3x Coolermaster 80mm + 2 Thermaltake 200mm 
OSMonitorMonitorKeyboard
Windows 10 x64 LG TV LG Flatron W2361V Logitech K200 
PowerCaseMouseMouse Pad
Enermax Galaxy 850W Thermaltake Element S Logitech G502 Proteus Spectrum RGB Steelseries QcK 
AudioAudio
Realtec HDA Lexicon Omega 
  hide details  
Reply
post #9 of 10
Quote:
Originally Posted by AngeloG. View Post
crust_cheese's code works just fine, however if I run the .exe directly, the program ends before I can read anything, and I don't know how to avoid that. The only way to see the result is open cmd, and point it to the .exe via its path.
#include <conio.h>

getch(); // Use this, it will grab input and pause the program until you press a key
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #10 of 10
Quote:
Originally Posted by lordikon View Post
#include <conio.h>

getch(); // Use this, it will grab input and pause the program until you press a key
This.

Code:
#include <stdio.h>
#include <stdlib.h>


int main()

{
    char* numa = NULL;
    char* numb = NULL;
    int sum = 0;
    
    printf("Enter the first number:\
");
    scanf("%s", numa);
    printf("Enter the second number:\
");
    scanf("%s", numb);
    sum = atoi(numa) + atoi(numb);
    printf("The sum is:%d\
", sum);
    scanf("%s" , numa);
}
FYI, that code crashes because numa and numb are NULL pointers. They are not referring to any memory. When you try and scanf() into numa or numb, you're trying to store data into memory that hasn't yet been allocated.

You could have used something like char numa[33] and that would be good enough for a 32-digit number. The 33rd byte is reserved for the the NULL character, '\\0', which terminates strings. That said, scanning directly into an int is a much better method.
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Simple addition in C, I fail so much?