Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Aspiring programmer looking for advice
New Posts  All Forums:Forum Nav:

Aspiring programmer looking for advice

post #1 of 20
Thread Starter 
I know I'm a total n00b and this question has been asked over and over, but I'm just looking to learn C++ and code "efficiently" and not just throw together some sloppy code.

So my questions are:

1.) Is it better to use spaces in between something or not use spaces?

Example 1:

#include iostream // Quotation marks are here, but its not showing up in the browser.
#include string
#include stdio.h
using namespace std;
int main () {
int a,b,c;
int result;
a=1;
b=2;
c=3;
result=a*b*c/c+a;
cout<<result<<endl;
cin.ignore();
string mystring;
mystring="Test string";
cout<<mystring<<endl;
cin.ignore();
printf("Finished, press enter to exit");
cin.ignore();
cout << "Please enter a number: ";
int age;
cin>>age;
cin.ignore();
if ( age<20 ){
cout<<"Your younger than 20 years old.\n";
}
if (age>20){
cout<<"Your older than 20 years old.\n";
}
if (age==20){
cout<<"Your 20 years old exactly.\n";
}
cin.ignore();
}


Example 2:


#include
#include
#include
using namespace std;
int main ()
{
int a, b, c;
int result;
a = 1;
b = 2;
c = 3;
result = a * b * c / c + a;
cout << result << endl;
cin.ignore();
string mystring;
mystring = "Test string";
cout << mystring << endl;
cin.ignore();
printf("Finished, press enter to exit");
cin.ignore();
cout << "Please enter a number: ";
int age;
cin >> age;
cin.ignore();
if ( age < 20 ) {
cout << "Your younger than 20 years old.\n";
}
if ( age > 20 ) {
cout << "Your older than 20 years old.\n";
}
if ( age == 20 ) {
cout << "Your 20 years old exactly.\n";
}
cin.ignore();
}

This was just a very quick program I threw together off the top of my head. Note the spacing ( cout<<result<<endl; vs cout << result <<endl;)

Since I'm learning coding, I would rather learn how to go "good" instead of learning it sloppy and having to go back over my mistakes for coding sloppy.
Edited by *ka24e* - 3/24/13 at 6:20am
post #2 of 20
My professors always wanted to know the exact location of a syntax error so he told his classes to never use spaces for the initial rough draft. But I use spaces all the time for a lengthier, yet, more legible code.
post #3 of 20
i prefer to use spaces between parts of the line

the big important stuff to do is to use proper indentation techniques, use the same naming style (terrainGenerator or terrain_generator), and please please please put in useful comments. The last one is extremely important if other people are going to read or work with the code.
ShadowForge
(12 items)
 
Defiant
(14 items)
 
CarbonCat
(13 items)
 
CPUMotherboardGraphicsRAM
Phenom II x6 1405T (unlocked Athlon II X4 640T) ASUS M5A99X EVO AM3+ Asus ENGTX470/2DI/1280MD5/V2 16 GB (4x4GB) G.Skill DDR3 1600 CAS9 1.35v 
Hard DriveHard DriveOptical DriveCooling
WD Caviar Blue 250 2.5" Laptop Drive WD Caviar Black 1TB 3.5" Pioneer BDR-203 BluRay Burner Corsair H60 push 
OSOSMonitorPower
Windows 7 Pro x64 Ubuntu 11.10 Samsung 40" 60hz  ANTEC NEO ECO 520W 
CaseMouse
NZXT Gamma Microsoft Bluetooth Notebook Mouse 5000 
CPUMotherboardGraphicsRAM
i7 - 2600k [5.0 1.42v] ASUS P8Z68 Deluxe Sapphire HD6950 2gb Dirt 3 Edition 8GB G.Skill DDR3 2133 CAS11 
Hard DriveOSMonitorKeyboard
60GB G.Skill Sniper + 2x1TB Spinpoint F3 Raid0 Win 7 Pro x64 ASUS VW266H Razer Blackwidow 
PowerCaseMouse
Seasonic X750 Gold Corsair Carbide 500R White G9 
  hide details  
Reply
ShadowForge
(12 items)
 
Defiant
(14 items)
 
CarbonCat
(13 items)
 
CPUMotherboardGraphicsRAM
Phenom II x6 1405T (unlocked Athlon II X4 640T) ASUS M5A99X EVO AM3+ Asus ENGTX470/2DI/1280MD5/V2 16 GB (4x4GB) G.Skill DDR3 1600 CAS9 1.35v 
Hard DriveHard DriveOptical DriveCooling
WD Caviar Blue 250 2.5" Laptop Drive WD Caviar Black 1TB 3.5" Pioneer BDR-203 BluRay Burner Corsair H60 push 
OSOSMonitorPower
Windows 7 Pro x64 Ubuntu 11.10 Samsung 40" 60hz  ANTEC NEO ECO 520W 
CaseMouse
NZXT Gamma Microsoft Bluetooth Notebook Mouse 5000 
CPUMotherboardGraphicsRAM
i7 - 2600k [5.0 1.42v] ASUS P8Z68 Deluxe Sapphire HD6950 2gb Dirt 3 Edition 8GB G.Skill DDR3 2133 CAS11 
Hard DriveOSMonitorKeyboard
60GB G.Skill Sniper + 2x1TB Spinpoint F3 Raid0 Win 7 Pro x64 ASUS VW266H Razer Blackwidow 
PowerCaseMouse
Seasonic X750 Gold Corsair Carbide 500R White G9 
  hide details  
Reply
post #4 of 20
Spacing, layout, indentation, proper comments and naming conventions don't effect functionality but they make it WAY easier to work on twelve months later, I always work:
Code:
if ( age<20 )
{
     cout<<"Your younger than 20 years old.\n";
}

in C# and Java at work with plenty of comments. It's much easier to have that work supported by other developers then also
Edited by Kane2207 - 3/24/13 at 6:31am
Meh
(11 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600k @4.7GHz Asus P8Z68-V GEN3 GTX Titan 16GB Mushkin Redline Ridgeback 
Hard DriveHard DriveCoolingOS
Crucial M4 128GB  2x 500GB WD Green RAID1 + WD 2TB Green  Noctua Nh-DH 14 Win 8 Pro x64 
MonitorPowerCase
Shimian Achieva 2560x1440p IPS Corsair AX850 Lian-Li PC-A05N 
  hide details  
Reply
Meh
(11 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600k @4.7GHz Asus P8Z68-V GEN3 GTX Titan 16GB Mushkin Redline Ridgeback 
Hard DriveHard DriveCoolingOS
Crucial M4 128GB  2x 500GB WD Green RAID1 + WD 2TB Green  Noctua Nh-DH 14 Win 8 Pro x64 
MonitorPowerCase
Shimian Achieva 2560x1440p IPS Corsair AX850 Lian-Li PC-A05N 
  hide details  
Reply
post #5 of 20
Yes, use spaces, make it descriptive and legible. If someone else or you after a while go over the code again it helps a lot in making sense of the code and saves you a lot of time.

Don't worry about spaces making it less efficient, extra spaces can be easily removed if needed with a script, so you can have a full version and a minified version.
post #6 of 20
http://en.wikipedia.org/wiki/Indent_style

Pick one and stick with it. Do this and you won't have any problems.
post #7 of 20
Quote:
Originally Posted by hajile View Post

http://en.wikipedia.org/wiki/Indent_style

Pick one and stick with it. Do this and you won't have any problems.

You forgot be prepared to defend it to the death!
Edited by mitchtaydev - 3/25/13 at 1:24am
Felix
(11 items)
 
ESXi 1
(9 items)
 
Dexter
(12 items)
 
CPUMotherboardGraphicsRAM
Intel Core i7 3770S 3.1 GHz Socket 1155 Box  Intel DQ77KB Intel HD4000 integrated gpu STRONTIUM 8GB Sodimm Single DDR3 1600Mhz 
Hard DriveCoolingOSKeyboard
Intel 520 Series SATA3 25nm 120G 2.5' Scythe Kozuti CPU low profile heatsink fan Microsoft Windows 8 Pro w/ Media Centre Corsair Vengeance K90. Performance, MMO, Mechan... 
PowerCaseMouse
Dell 19V 4.62A (Round) PA-10 Original AC Adapter Antec ISK110 VESA Mini-ITX Case, 90-watt adapte... Corsair Vengeance M90. Performance, MMO, Mechan... 
CPUMotherboardGraphicsRAM
Intel Core i7 3770 3.40 GHz 8M LGA1155 Processor Intel DQ77MK Intel HD4000 integrated gpu STRONTIUM 8GB 1600MHz DDR3 Dimm Single Module P... 
Hard DriveCoolingOSCase
SanDisk Blade 4GB Scythe Big Shuriken Rev. 2 ESXi vSphere 5.1 Coolermaster Elite 343 MATX Mini Tower Case Bla... 
Other
Dell X3959 dual port Intel GbE NIC 
CPUMotherboardGraphicsRAM
Intel Ivy Bridge Core i7 3770K Unlocked Quad Core ASUS SABERTOOTH-Z77 PCI-E3.0 x 16 Motherboard Intel HD4000 integrated gpu Corsair Vengeance 16GB (4x4GB) DDR3 1600MHz CL9... 
Hard DriveHard DriveCoolingCooling
Western Digital Scorpio Black 2.5 inch 320GB Western Digital Caviar Raid Edition4 1000GB (1TB) Corsair Cooling Hydro Series H100 Liquid CPU Corsair "Air Series" SP120 Quiet Edition Case Fan 
OSPowerCaseOther
Openfiler 2.99-2 Corsair AX850 850W  Corsair Obsidian 650D Black Mid Tower Case with... Dell Perc 6/i RAID Controller 
  hide details  
Reply
Felix
(11 items)
 
ESXi 1
(9 items)
 
Dexter
(12 items)
 
CPUMotherboardGraphicsRAM
Intel Core i7 3770S 3.1 GHz Socket 1155 Box  Intel DQ77KB Intel HD4000 integrated gpu STRONTIUM 8GB Sodimm Single DDR3 1600Mhz 
Hard DriveCoolingOSKeyboard
Intel 520 Series SATA3 25nm 120G 2.5' Scythe Kozuti CPU low profile heatsink fan Microsoft Windows 8 Pro w/ Media Centre Corsair Vengeance K90. Performance, MMO, Mechan... 
PowerCaseMouse
Dell 19V 4.62A (Round) PA-10 Original AC Adapter Antec ISK110 VESA Mini-ITX Case, 90-watt adapte... Corsair Vengeance M90. Performance, MMO, Mechan... 
CPUMotherboardGraphicsRAM
Intel Core i7 3770 3.40 GHz 8M LGA1155 Processor Intel DQ77MK Intel HD4000 integrated gpu STRONTIUM 8GB 1600MHz DDR3 Dimm Single Module P... 
Hard DriveCoolingOSCase
SanDisk Blade 4GB Scythe Big Shuriken Rev. 2 ESXi vSphere 5.1 Coolermaster Elite 343 MATX Mini Tower Case Bla... 
Other
Dell X3959 dual port Intel GbE NIC 
CPUMotherboardGraphicsRAM
Intel Ivy Bridge Core i7 3770K Unlocked Quad Core ASUS SABERTOOTH-Z77 PCI-E3.0 x 16 Motherboard Intel HD4000 integrated gpu Corsair Vengeance 16GB (4x4GB) DDR3 1600MHz CL9... 
Hard DriveHard DriveCoolingCooling
Western Digital Scorpio Black 2.5 inch 320GB Western Digital Caviar Raid Edition4 1000GB (1TB) Corsair Cooling Hydro Series H100 Liquid CPU Corsair "Air Series" SP120 Quiet Edition Case Fan 
OSPowerCaseOther
Openfiler 2.99-2 Corsair AX850 850W  Corsair Obsidian 650D Black Mid Tower Case with... Dell Perc 6/i RAID Controller 
  hide details  
Reply
post #8 of 20
Indentation is an absolute *MUST*.
Quote:
Originally Posted by MakubeX View Post

Don't worry about spaces making it less efficient, extra spaces can be easily removed if needed with a script, so you can have a full version and a minified version.
Spaces wouldn't effect code efficiency as one of the first thing compilers do is strip formatting out of the code.

Also, you're talking from the perspective of Javascript, this is C++. Minifying C++ would be insane.
post #9 of 20
Quote:
Originally Posted by Kane2207 View Post

Spacing, layout, indentation, proper comments and naming conventions don't effect functionality but they make it WAY easier to work on twelve months later, I always work:
Code:
if ( age<20 )
{
     cout<<"Your younger than 20 years old.\n";
}

in C# and Java at work with plenty of comments. It's much easier to have that work supported by other developers then also

not always the case.

Some programming languages like MUMPS (a language built on C and actually spits out a C source code file after compiling) will throw exceptions if any extra spaces are used at all.

It is all language dependant.
Edited by Bobicon - 3/27/13 at 8:37am
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
post #10 of 20
Thread Starter 
Thanks everyone for the advice. I've been spending about an hour a day learning the basics. For some reason the code just looks... sloppy when I use spaces. I don't know, it's probably just me but I'm working on it, lol.

Here's something I threw together off memory alone. How am I doing so far?
Code:
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;

int mult (int a, int b);

void startgame()
{
cout<<endl;
cout<<"Starting new game.\n";
}
void options()
{
cout<<endl;
cout<<"There are no options, you can leave now.\n";
}
void about()
{
cout<<endl;
cout<<"Made by *ka24e* lol.\n";
}
int main (){

printf("Lets start some C++ practice. Press enter to continue");
cin.ignore();
cout<<endl;

printf("First a simple string. Press enter to continue.\n");
cin.ignore();

string mystring;
mystring="This is my string.\n";
cout<<mystring;
cin.ignore();

 int age;
 cout<<"How old are you?: ";
 cin>>age;
 cout<<endl;
 cout<<"Your "<<age<<" years old!\n";
 cin.ignore();
 cout<<endl;
 
  printf("The next statment will be 22 x 44. Press enter to continue.");
  cin.ignore();
  cout<<endl;
  
   int x,y;
   int result;
   x=22;
   y=44;
   result=x*y;
   cout<<result<<endl;
   cin.ignore();
   
    printf("Next is a simple math problem. Press enter to continue.");
    cin.ignore();
    cout<<endl;
    
     int ras;
     cout<<"What is 2 x 2: ";
     cin>>ras;
     cout<<endl;
     cin.ignore();
       
        if (ras==4){
        cout<<"Correct!\n";
        cin.ignore();
        }
        if (ras>4){
        cout<<"Sorry, your a little too high.\n";
        cin.ignore();
        }
        if (ras<4){
        cout<<"Sorry, your a little too low.\n";
        cin.ignore();
        }
        
         int input;
         cout<<"1. Start game.\n";
         cout<<"2. Options.\n";
         cout<<"3. About.\n";
         cout<<"4. Exit.\n";
         cout<<"Solution: ";
         cin>>input;
         cin.ignore();
            
            switch (input){
              case 1:
               startgame();
               break;
              case 2:
               options();
               break;
              case 3:
               about();
               break;
              case 4:
               cout<<"Go ahead and leave, see if I care...\n";
               break;
              default:
               cout<<"Bad input, press enter to leave.\n";
               break;
               }
               
                                        cout<<endl;
                
                 int a;
                 int b;
                 cout<<"Please enter two numbers to be multiplied: ";
                 cin>>a>>b;
                 cout<<endl;
                 cout<<"The two numbers you entered multiplied equals "<<mult(a,b)<<endl;
                 cin.get();
                 cout<<endl;
                 
                  printf("End practice C++. Press enter to close");
                  cin.ignore();
}
int mult (int a, int b)
{
return a*b;
}
               
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Aspiring programmer looking for advice