FRIHOST • FORUMS • SEARCH • FAQ • TOS • BLOGS • COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


C++ Program Basics





Jamestf347
Well I've been in a C++ programming class for the last 5 months. If you've coded Java it's not very different but it is : ). Well anyways here is the notes I've taken this year : ).

.:C++ PROGRAMMING BASICS:.[/CENTER]

Well first off you're going to need a program to compile the c++ right? Well I suggest "[COLOR=orange]Tclite Borland Turbo C++" (will post a link later).

To create a new c++ program the format is:
Code:

//James
//Period 1
//Cool
//Prints out a brief statement

#include <iostream.h>
main()
{
          cout<<"Computer Science";
          cout<<"is cool";
return 0;
}


**There must be a semicolon after each command line! (Wink
*The command lines (lines that don't show up in a program can start with // or /* this is for multiple lines */ (must have that at the end)

the includes are compiler directives like - #include <something.h>. Make sure that bracket after main() there is a tab (i hear its to make the program look neater which interms can sell your program better).

A few includes:

  • Iostream.h - input output screen
  • Conio.h - Mainly used for the clrscr; command


SAMPLE CODE

Code:

#include <whatever.h>
main()
{
        float Num1, Num2, Avg; //variables
        cout<<"Please Enter First Number"; //basic output
        cin>>Num1;   //cin is the user input command, it is used to assign a value to a variable
        cout<<"Please enter second number";
        cin>>Num2;
        Avg=(Num1+Num2)/2;
        cout<<"The Average of "<<Num1<<" and "<<Num2<<" is "<<Avg;
        getch();
return 0;
}


Math Operators


add = (+)
subtract = (-)
multiply = (*)
divide = (/)

ex. 2(x+1) 2(x-1) 2(x*1) 2(x/1)



Incrementing Variables


i=i+1;
i=i+5;
x=x*3;
A=A-1;
i=1++;
i=1;

Legal Variables

int Average;

float Mess;

Illegal Variables

char Last Name;
^space

int int;
^c++ reserved word

Code Format (updated)

Code:

//James
//Period 1
// Program Name
//Program Description and function
#include <whatever.h>
main()
{
         variable declerations;
         code;
return 0;
}


New Command
cin = Input command (console input is what it stands for)
(use) cin>>Num1;

**Remember to always prompt the user as to what they should enter!

Identifiers and output

Indentifiers / Variables - Values held in ram while the program is running

char = characters
int = integers from -32,768 to 32,767
float = decimals -3.4x10(-3Cool to 3.4x10(3Cool

Expansions

long = integers from -2, 147, 483, 648 to 2, 147, 483, 648
double = decimal from 1.7x10(-308) to 1.7x10(308)
long double = 3.4x10(-4932) to 1.7x10(4932)

Declaring Variables

1. Must start with a letter or the underscore char

2. After the first character, you may use any characters or numbers that are on your keyboard except the minus sign

3. Use a name that describes the use of the variable

4. There van be no spaces in your variables name (First_Name, an underscore must be used as a space).

5. You may not use C++ reserved words as variable names

For Loops

Use "for" loops when you know or can determine easily the exact number of times you want code to be repeated

Structure:
Code:

For (beginning value ; range of values ; increment)
{
     code to be repeated;
}


the variable that you use to control how many times the loop repeated is called the "loop control variable"

Example - Lets write a program that will add up to 10 numbers

Code:

#include <whatever.h>
main()
{
         int i, num, sum;
         for (i=1; i<=10; i=i+1)
         {
               cout<<"Please Enter numnber "<< i;
               cin>>num;
               sum=sum+num;
         }
         cout<<"The sum of your ten numbers is "<<sum;

return 0;
}


While and do-while loops

While Structure:
Code:

while (condition) //for the loop to procede
{
       code
}


Do-While Structure:
Code:

do
{
      code
} while (condition)


Example - Suppose you ask a user to ask a number less than 100 you can use this structure to make the program repeat the question until the user enters the appropriate #

Error Trapping

Code:

do
{
      cout<<"Enter a number less than 100";
      cin>>num;
}while (num>=100)



SUBROUTINES

Subroutine - A mini program inside another program

Use - Simplifies the writing of big programs (they come in very handy with menu driven programs)

STRUCTURE

Code:

#include <whatever.h>
void Name-of-subroutine();
Global Variables //Variables used in any routine (hence global)
main();
{
          variables;
          code; //include calls to subroutines
return 0;
}

void Name-of-subroutine()
{
           local variables;
           code;
}


Yay time for an example

EXAMPLE

Code:

#include <whatever.h>
void add();
void subtract();
void endit();

main();
{
           int choice;
do
        {
             cout<<"1 - Addition\n";
             cout<<"2 - Subtraction\n";
             cout<<"3 - Quit";
             cin>>choice;
           if (choice == 1)
           {
                add();
           }
           if (choice == 2)
           {
                subtract();
           }
           if (choice == 3
           {
                endit();
           }
      } while (choice !=3)

return 0;
}
void add()
{
     int x, y, ans, userans, count;
     x=rand()%10; //will choose a random number 1-9
     y=rand()%10;
   ect.
   ect. (you should be able to code the rest)
}


[I]
**REMEMBER TO ALWAYS FLOW CHART YOUR PROGRAMS

If you have any questions, ask. I'll try to help the best I can.
coreymanshack
Well, I actually never flow chart my programs, I code as I think them.
Jamestf347
I do that when i'm bored but, if it's really really intregate and you're trying to sell it... it's like a must have.
Marcuzzo
nice tutorial for beginners, but I think that this is more something for on the tutorial forum
snowboardalliance
You shouldn't use the ".h" includes, that is the C style. Code should be

Code:

#include <iostream>
using namespace std;


Now it is equivalent, but that is the standard way of doing it. (I believe the .h files just include that and say using namespace std in that file).

Just pointing that out. Also note that variables can be declared anywhere, not just at the top like in C.
Jamestf347
Thanks for the info. This is just the way I was taught in my highschool class =].
snowboardalliance
Jamestf347 wrote:
Thanks for the info. This is just the way I was taught in my highschool class =].


I know how that is. I'd recommend you read a good book on c++ if you plan to use it beyond that. In high school they taught us like nothing right (we used like a 10 year old compiler) and I've learned so much more since.
Jamestf347
snowboardalliance wrote:
Jamestf347 wrote:
Thanks for the info. This is just the way I was taught in my highschool class =].


I know how that is. I'd recommend you read a good book on c++ if you plan to use it beyond that. In high school they taught us like nothing right (we used like a 10 year old compiler) and I've learned so much more since.


I don't know if I really want to go on with C++...java is good but c++ is just too math intense (personally i hate it and im not good at it) but thanks for the tips =]
coreymanshack
Jamestf347 wrote:
snowboardalliance wrote:
Jamestf347 wrote:
Thanks for the info. This is just the way I was taught in my highschool class =].


I know how that is. I'd recommend you read a good book on c++ if you plan to use it beyond that. In high school they taught us like nothing right (we used like a 10 year old compiler) and I've learned so much more since.


I don't know if I really want to go on with C++...java is good but c++ is just too math intense (personally i hate it and im not good at it) but thanks for the tips =]


Java is more rescource intensive, but it has a lot more capabilities for starting programmers. Java is more of a web based language.
umeshtangnu
nice tutorial bro
but do gave do while under Error Trapping
snowboardalliance
Jamestf347 wrote:


I don't know if I really want to go on with C++...java is good but c++ is just too math intense (personally i hate it and im not good at it) but thanks for the tips =]


Hmm, well c++ can be more complex with memory such as using pointers properly while java just has garbage collection, but I don't know how either is "math intense" at least compared to any other programming language. Not saying c++ is perfect and you should use it, but I don't know how there is any more math involved in the language.
coreymanshack
snowboardalliance wrote:
Jamestf347 wrote:


I don't know if I really want to go on with C++...java is good but c++ is just too math intense (personally i hate it and im not good at it) but thanks for the tips =]


Hmm, well c++ can be more complex with memory such as using pointers properly while java just has garbage collection, but I don't know how either is "math intense" at least compared to any other programming language. Not saying c++ is perfect and you should use it, but I don't know how there is any more math involved in the language.


Yea I don't know what you mean by math intensive either.
Jamestf347
Well during my highschool class we have to code programs. These programs have A LOT to do with math and forumulas ect. That's how it's math intense, it's sort of getting on my nerves that we only do math software packages =\.
coreymanshack
Jamestf347 wrote:
Well during my highschool class we have to code programs. These programs have A LOT to do with math and forumulas ect. That's how it's math intense, it's sort of getting on my nerves that we only do math software packages =\.


Yea, that's pretty stupid.
snowboardalliance
Jamestf347 wrote:
Well during my highschool class we have to code programs. These programs have A LOT to do with math and forumulas ect. That's how it's math intense, it's sort of getting on my nerves that we only do math software packages =\.


Oh, you're class is math intensive, I see.
Jamestf347
Ya it dumb but eh, oh well it's a credit in highschool and i'll learn a little basic c++ while in the class. But i'm hoping to take possibly a higher level course in programming, cause this is sorta too easy and basic. But like i said anyone post any questions i'll be glad to try and answer for you.
disturbedscape
Cool, but I dont really need this. Really Basic
Jamestf347
everyone has to start from somewhere...
abhinavm24
well nice intro text to c++ really, i have been coding in c++ since my 10th grade and quite often i ran in to c intro text on various websites but this one is just out of the box.
just sharp and to the point for a starter to the language.
thanks Smile
Related topics
C++ TUTORIAL : INTRODUCTION TO C++
Creating an RPG bot in IRC (yes I wrote this)
A C program in Linux
CLOSE PLEASE-Need a C++ program - upto 200frih$
[C] Splitting names program
Deploying a C# program
10 frih$ contest: what does this c++ code do?
how to check if a file exists. please help (visual basics 6)
changing system time using a c program under cygwin
C++ Software
DEBUG MODE?
C++ program yazmaya başlayalım artık :=)
Writing your first C++ Program
My Small C# Program
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.