New Posts  All Forums:Forum Nav:

c++ help

post #1 of 8
Thread Starter 
Here is my program

Code:
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

void add ()
{
    cout << a+b << endl;
}

main ()
{
    int a = 1;
    int b = 2;
    add();
}
Right now this program doesn't work because the "add" function cannot access the variables a and b. Is there any way to let "add" access the variables a and b? I'm trying to avoid using parameters because it adds unnecessary complications to the code.

Thanks.
post #2 of 8
You will have to make them global, put them below using namespace std;

The add function can only see globals and params
Edited by andrewmchugh - 7/25/11 at 12:14pm
^3
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 5930K  EVGA x99 Micro EVGA GTX 980 SC ACX 16GB Corsair DDR4 Vengeance LPX Black PC4-21300 
Hard DriveCoolingOSMonitor
500GB Samsung 840 EVO H105 Win 10 ASUS ROG PG279 
KeyboardPowerCase
Ducky Shine 1 MX-Red Corsair AX 860i  Fractal Design Node 804 Black 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
^3
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 5930K  EVGA x99 Micro EVGA GTX 980 SC ACX 16GB Corsair DDR4 Vengeance LPX Black PC4-21300 
Hard DriveCoolingOSMonitor
500GB Samsung 840 EVO H105 Win 10 ASUS ROG PG279 
KeyboardPowerCase
Ducky Shine 1 MX-Red Corsair AX 860i  Fractal Design Node 804 Black 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
post #3 of 8
Umm... I'm pretty sure you have to have parameters. When you call add, it's not acting on anything. You have to add parameters for it to work. All you have to do is change your function definition to add( int a, int b)

Edit: Or do what andrewmchugh said... lol
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #4 of 8
Thread Starter 
Ahhhh. Thank you. For some reason I never tried declaring variables under using namespace std;
post #5 of 8
Quote:
Originally Posted by chengbin View Post
Ahhhh. Thank you. For some reason I never tried declaring variables under using namespace std;
Global variables is a solution, just be careful using them. I wouldn't get in a habit of using them. It's easy to remember for smaller programs, but for larger, more complicated programs it's easy to lose track and cause issues.
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #6 of 8
Avoid global variables unless you really have to imo.
pass a and b to add. Like this:

void add (int a, int b) { ... }

So you call add with:

add(a,b);
The Dark Knight
(13 items)
 
Rebirth
(13 items)
 
 
CPUMotherboardGraphicsRAM
i5 2500K. 4.5Ghz (Summer Clocks) ASUS P8P67 Pro ( Rev 3.0 ) MSI 6970 Lightning (downclocked) CFX with MSI 6... 8GB G.Skill Sniper 1600Mhz 
Hard DriveOptical DriveOSMonitor
1 TB Spinpoint F3 +2TB F4(backups) Random LG drive Win7 SP1 64bit ASUS VH236H @1080p 
KeyboardPowerCase
Saitek Gaming keyboard ... >_> Corsair TX750 Rosewill Gear x3 
CPUMotherboardGraphicsRAM
C2D P8600 @ 2.4Ghz (undervolted) Crappy dell Studio XPS one 4670M (1GB) I don't remember... 
Hard DriveOptical DriveOSMonitor
Crucial M4 4x Blu-ray ROM Win7 SP1 64bit 15.6" WLED @ 1080p 
KeyboardPower
Old Saitek Gaming Keyboard 90W adapter 
  hide details  
Reply
The Dark Knight
(13 items)
 
Rebirth
(13 items)
 
 
CPUMotherboardGraphicsRAM
i5 2500K. 4.5Ghz (Summer Clocks) ASUS P8P67 Pro ( Rev 3.0 ) MSI 6970 Lightning (downclocked) CFX with MSI 6... 8GB G.Skill Sniper 1600Mhz 
Hard DriveOptical DriveOSMonitor
1 TB Spinpoint F3 +2TB F4(backups) Random LG drive Win7 SP1 64bit ASUS VH236H @1080p 
KeyboardPowerCase
Saitek Gaming keyboard ... >_> Corsair TX750 Rosewill Gear x3 
CPUMotherboardGraphicsRAM
C2D P8600 @ 2.4Ghz (undervolted) Crappy dell Studio XPS one 4670M (1GB) I don't remember... 
Hard DriveOptical DriveOSMonitor
Crucial M4 4x Blu-ray ROM Win7 SP1 64bit 15.6" WLED @ 1080p 
KeyboardPower
Old Saitek Gaming Keyboard 90W adapter 
  hide details  
Reply
post #7 of 8
Three things you can do:

1. Global variables...like the 2nd poster said.
2. Parameters.
3. If you are going to expand your program with many arithmetic functions and other operations then it would be best to use classes...but it seems you are at the beginning stages of programming...so use 1 or 2 for now.
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #8 of 8
Quote:
Originally Posted by chengbin View Post
Ahhhh. Thank you. For some reason I never tried declaring variables under using namespace std;
Global variables are generally bad practice and can cause problems when you start making larger programs. I would follow the advice in the thread that suggests you pass those integers into a function.

Code:
static int Add( int first, int second )
{
    return first + second;
}

int main()
{
    int a = 5;
    int b = 7;
    
    int result = Add(5, 7);

    return 1;
}
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
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming