Post: Basic C++ Programming
06-20-2012, 04:43 AM #1
(adsbygoogle = window.adsbygoogle || []).push({}); Now I thought I'd help a few users out with basic C++ programming. I am still learning my self, but I'm getting more advanced.

Includes:
Includes basically reference items, dll's, and other programs. It incoorperates codes into your program, and it's helps usage. This can be easily done by the following code:
    
#include <object_name>


The object name usually ends with a ".h" but it can be a program or dll. This is placed before your main code.

Input and Output:
For this example we will be using the namespace std, and using the iostream. This makes it possible to use cout, cin, and endl.

(note: you always start your program with int main() (to have the program complete it will always return with 0 (what I learned from C++ studies)))

    
#include <iostream>

using namespace std;

int main()
{
cout << "Hello world.. \n";
}


Now what that is doing is printing hello work into console. cout is like print in C#. cout stands for console out. It is output. << is the bit movement. and the text output is Hello world... (\n is newline in program language)

but that will close console right away.. so we need to add a
    
cin.get();


this will require the user to press enter, before the program closes. Add it right after the cout call..

Now lets move onto user input

User input is just as easy as output but it's set up a little different :p

continued from our above code...

    
int main()
{
int age_here
cout << "please enter your age";
cin >> age_here;
cin.ignore(); //this causes to skip the user requiring to press enter...
cout << "You entered an age of" << age_here << "!\n";
cin.get();
return 0;
}


by using cin >> variable we are setting up user input to input data (such as a name, age, date, or whatever.). cin.ignore() skips the user needing to press enter (basically in continues the code). Then we output what the user entered. and then the program pauses with cin.get();

for strings you can use...

    
char string[number here];

cout<<"Enter your name.";
cin.getline (string, number here, '\n'Winky Winky;
cout<<"Your name is: "<<string<<endl;
cin.get();


Conclusion:
I hoped you learned something from this because I like teaching people new things. I am still learning myself so :p

Next tutorial I might do an if then statement, for loops, while loops, do while loops, structures, pointers, or variables. I don't know yet, but I hope maybe users can request things. Smile
Last edited by Jakes625 ; 06-20-2012 at 06:19 AM.

The following 4 users say thank you to Jakes625 for this useful post:

Davey, DinoFreak, Ninja, TheFuziioN-
06-22-2012, 07:41 PM #11
Kellis
LoanWolf
Originally posted by Pichu View Post
If you have prior knowledge of basic programming, you would somewhat understand. Yea, it looks hard but once you start learning it from the basics, you can understand it.


okay! Happy...

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo