New Posts  All Forums:Forum Nav:

Help a noob out...

post #1 of 8
Thread Starter 
I'm just trying to ease myself into programming, so tell me what I'm doing wrong please, and no put downs on how easy to find my mistake is .

I'm getting into if and else statements, i got IF statements down, but for some reason my else statement won't work.

The tutorial im reading says:

if ( TRUE ) {
// Execute these statements if TRUE
}
else {
// Execute these statements if FALSE
}

This is my code:

#include <iostream>
using namespace std;
int main ()
{
if (5 > 10)
cout<<"my name is bob";
cin.ignore ();
}
else {
cout<<"my name is kris";
cin.ignore ();

}

however, the error i get says error: expected unqualified id before 'else'
My One and Only
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 760 @ 4.1GHz MSI P55-CD53 EVGA GTX 570 SC 8 GB Mushkin Blackline DDR3 PC1333 
Hard DriveOptical DriveOSMonitor
500GB WD Blue Lite-On DVD Burner Windows 7 Ultimate 64bit AOC 23" LED LCD 1920 x 1080 
PowerCaseMouse
Silverstone 1000W Modular CM HAF 922 Razer Deathadder 
  hide details  
Reply
My One and Only
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 760 @ 4.1GHz MSI P55-CD53 EVGA GTX 570 SC 8 GB Mushkin Blackline DDR3 PC1333 
Hard DriveOptical DriveOSMonitor
500GB WD Blue Lite-On DVD Burner Windows 7 Ultimate 64bit AOC 23" LED LCD 1920 x 1080 
PowerCaseMouse
Silverstone 1000W Modular CM HAF 922 Razer Deathadder 
  hide details  
Reply
post #2 of 8
You're missing the brace after your if (5 > 10).

You're also missing a brace at the end of your main() function, and a return value.

Code:
#include <iostream>
using namespace std;

int main ()
{
    if (5 > 10)
    {
        cout << "my name is bob";
        cin.ignore();
    }
    else
    {
        cout << "my name is kris";
        cin.ignore();
    }

    return 1;
}

Edited by lordikon - 4/11/11 at 7:53pm
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 #3 of 8
Thread Starter 
Thanks a ton, didn't know i needed an extra set of brackets. As far as the return value, is that always needed?
My One and Only
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 760 @ 4.1GHz MSI P55-CD53 EVGA GTX 570 SC 8 GB Mushkin Blackline DDR3 PC1333 
Hard DriveOptical DriveOSMonitor
500GB WD Blue Lite-On DVD Burner Windows 7 Ultimate 64bit AOC 23" LED LCD 1920 x 1080 
PowerCaseMouse
Silverstone 1000W Modular CM HAF 922 Razer Deathadder 
  hide details  
Reply
My One and Only
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 760 @ 4.1GHz MSI P55-CD53 EVGA GTX 570 SC 8 GB Mushkin Blackline DDR3 PC1333 
Hard DriveOptical DriveOSMonitor
500GB WD Blue Lite-On DVD Burner Windows 7 Ultimate 64bit AOC 23" LED LCD 1920 x 1080 
PowerCaseMouse
Silverstone 1000W Modular CM HAF 922 Razer Deathadder 
  hide details  
Reply
post #4 of 8
Quote:
Originally Posted by KrisBKreeme View Post
Thanks a ton, didn't know i needed an extra set of brackets. As far as the return value, is that always needed?
Your function definition for 'int main()' begins with 'int', which is you telling the compiler that your function will be returning an integer value.

If you don't care about a return value then you can use 'void' instead, however main() is a special function that should begin with int because the OS often cares about main's return value so it can know if the program exited successfully or not.

Code:
// Function that has no return value
void PerformSomeAction()
{
   // Do stuff
}

// Function with a return value
bool IsNumberEven( int num )
{
    // Returns 'true' if a number is even, 'false' if it's odd.
    return ( (num % 2) == 0 );
}

// Now lets use our functions
int main()
{
    DoSomeAction();

    int i = 5;
    bool isEven = IsNumberEven(i);

    if ( isEven )
    {
        cout << "Number is even";
    }
    else
    {
        cout << "Number is odd";
    }
  
    return 1;
}

Edited by lordikon - 4/11/11 at 7:53pm
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 #5 of 8
As for the brackets, it's actually quite easy. You need to close every function, method ect. that you open. Just like tags in your posts here on the forum.

It's acutally why IDE's line up the code up as they do. That gives you a visual representation of how many "layers" deep you are, and makes it easier to see what belongs to what in the code
SP4
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500 ASUS P8P67 Pro B3 ASUS EAH6870 1GB Corsair Vengeance DDR3-12800 
Hard DriveOptical DriveOSMonitor
64GB Kingston V+ SDD Samsung Super WriteMaster Windows 7 64Bit LG 20" Widescreen 
KeyboardPowerCaseMouse
Steelseries Shift Corsair TX650 V2 4 Unit Rack Case Steelseries Sensei 
Mouse Pad
SteelPad 3S 
  hide details  
Reply
SP4
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500 ASUS P8P67 Pro B3 ASUS EAH6870 1GB Corsair Vengeance DDR3-12800 
Hard DriveOptical DriveOSMonitor
64GB Kingston V+ SDD Samsung Super WriteMaster Windows 7 64Bit LG 20" Widescreen 
KeyboardPowerCaseMouse
Steelseries Shift Corsair TX650 V2 4 Unit Rack Case Steelseries Sensei 
Mouse Pad
SteelPad 3S 
  hide details  
Reply
post #6 of 8
Quote:
Originally Posted by SkipSkovhugger View Post
As for the brackets, it's actually quite easy. You need to close every function, method ect. that you open. Just like tags in your posts here on the forum.

It's acutally why IDE's line up the code up as they do. That gives you a visual representation of how many "layers" deep you are, and makes it easier to see what belongs to what in the code
While it's true that every open brace requires a close brace, and vice versa, not every statement requires braces.


Code:
// This works
if ( 1 < 2 )
{
    SomeFunction();
}
Code:
// This works too
if ( 1 < 2 )
    SomeFunction();
Be careful though, without brackets you can't use multiple lines, both examples in the following code will execute the same. For this reason I tend to always use braces for conditional statements.
Code:
// Example #1
if ( 1 < 2 )
    SomeFunction();
    SomeOtherFunction();

// Example #2
if ( 1 < 2 )
     SomeFunction();

SomeOtherFunction();
In summary, if you use a braces, they must always be used in pairs.
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 #7 of 8
Thread Starter 
Thanks a lot guys! I'm really enjoying learning C++, as challenging as it is. I'm trying to get a feel of what studying my major will be like, and I thought since there are thousands of tutorials, its best to start now while high school is dwindling down to its last month for me . I should just start bringing pages of Python tutorials to math class and get the teacher po'd. lol jk


EDIT: Also the fact that i have a quad core machine and all I do is play occasional Bad Company 2, made me think of other things to use my PC for. So I installed Ubuntu, and hoping that within the next year or two so of learning I can write small applications for personal use just to test my abilities
Edited by KrisBKreeme - 4/13/11 at 8:21pm
My One and Only
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 760 @ 4.1GHz MSI P55-CD53 EVGA GTX 570 SC 8 GB Mushkin Blackline DDR3 PC1333 
Hard DriveOptical DriveOSMonitor
500GB WD Blue Lite-On DVD Burner Windows 7 Ultimate 64bit AOC 23" LED LCD 1920 x 1080 
PowerCaseMouse
Silverstone 1000W Modular CM HAF 922 Razer Deathadder 
  hide details  
Reply
My One and Only
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 760 @ 4.1GHz MSI P55-CD53 EVGA GTX 570 SC 8 GB Mushkin Blackline DDR3 PC1333 
Hard DriveOptical DriveOSMonitor
500GB WD Blue Lite-On DVD Burner Windows 7 Ultimate 64bit AOC 23" LED LCD 1920 x 1080 
PowerCaseMouse
Silverstone 1000W Modular CM HAF 922 Razer Deathadder 
  hide details  
Reply
post #8 of 8
Quote:
Originally Posted by KrisBKreeme View Post
Thanks a lot guys! I'm really enjoying learning C++, as challenging as it is. I'm trying to get a feel of what studying my major will be like, and I thought since there are thousands of tutorials, its best to start now while high school is dwindling down to its last month for me . I should just start bringing pages of Python tutorials to math class and get the teacher po'd. lol jk


EDIT: Also the fact that i have a quad core machine and all I do is play occasional Bad Company 2, made me think of other things to use my PC for. So I installed Ubuntu, and hoping that within the next year or two so of learning I can write small applications for personal use just to test my abilities
As you get further into it you'll become more comfortable with the language and it'll break wide open when you start to realize the things you're able to do. This is what I love about game programming, I can literally create a virtual world in which I create all of the rules and limitations, a game where the limit is quite literally my imagination. Sounds corny, but I love it.
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