Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   C++ Tutorial I (http://www.go4expert.com/articles/cpp-tutorial-i-t12660/)

GreenGrass 2Aug2008 20:29

C++ Tutorial I
 
Here i am going to show you some basic C++ Programming... If you have any questions just ask. :)


Code: Cpp

#include <cstdlib> // This must be used if you are going to be a MS-DOS Application.
#include <iostream> // This must be included in the header file if you are going to use "Cout" and "Cin"
using namespace std;

int MittSvar(int tall1, int tall2, char stykke); // Here we make an Function so is going to calculate for us.
void Manual(); // This is user Manual so is going to tell us how to use the Program.

int main(int argc, char *argv[]) // int main() is the program file.
{
    // starting program. (Starting Block)
    // we are defind the inputs so you are going to use so the program know how to handle them.
    int tall1; // int = Integer = This will say that only input so will be accepted is integer.
    int tall2; // int = Integer = This will say that only input so will be accepted is integer.
    char stykke; // char = Character = This will say that only input so will be accepted is letters. ect +

    // The Function void does't need any return Value.
    Manual(); // Here will the User Manaul be Displayed.
    system("PAUSE >nul"); // Press an Key to Continue (Hidden).
    system("cls"); // Clear Screen.
    cout<<"Type: "<<endl; // Prints "Type" on the screen.
    cin>>tall1>>stykke>>tall2; // Exampel 13 + 13. Remember to press Enter between them.
    system("cls"); // Clear Screen.
    int svar = MittSvar(tall1, tall2, stykke); // Svar is the return Value of the Function.
    cout<<tall1<<stykke<<tall2<<"="<<svar; // Prints: tall1+tall2=svar (Exampel 20+20=40)
    system("PAUSE >nul"); // Press an Key to Continue (Hidden)
    return 0; // Exit Program.
} // End Program (End Block)

int MittSvar( // Start of the Calc Function.
int tall1,
int tall2,
char stykke)
{ // (Starting Block)
    int svar; // Answer of the Calc Value is going to be saved in.
    // "if" command is used to true or false. IF (stykke == '+') means if the "char stykke" is + it.
    // int Svar will give answer in +. Exampel (13+13=26) 13 = tall1 , + = stykke , 13 = tall2;
    if (stykke == '+') svar = tall1 + tall2; // Here are we showing the Function how it should act when you put in the Values.
    if (stykke == '-') svar = tall1 - tall2; // Here are we showing the Function how it should act when you put in the Values.
    if (stykke == '*') svar = tall1 * tall2; // Here are we showing the Function how it should act when you put in the Values.
    if (stykke == '/') svar = tall1 / tall2; // Here are we showing the Function how it should act when you put in the Values.
} // (End Block)

void Manual() //Starting Void Function.
{ // (Starting Block)
    cout<<"This is a calc."; // Here is what so should be displayed in the Manual Function.
    cout<<"Writing in what you want to calculate."<<endl; // Here is what so should be displayed in the Manual Function.
    cout<<"Example 20 + 20 = 40. Allowed characters are "; // Here is what so should be displayed in the Manual Function.
    cout<<"'+' . '-' . '/' . '*'"; // Here is what so should be displayed in the Manual Function.
} // (End Block)
 


hanleyhansen 8Aug2008 23:46

Re: C++ Tutorial I
 
Good comments! But you should explain in detail how you would put this together as opposed to just showing us. It would help the newbs a lot.

seangtz 9Aug2008 12:23

Re: C++ Tutorial I
 
Thx for sharing........ useful information.

GreenGrass 9Aug2008 17:11

Re: C++ Tutorial I
 
thanks for the comment hanleyhansen i will try remember that to next time.. :)

Echo 17Aug2008 14:14

Re: C++ Tutorial I
 
Not bad but, here you define the function as an int rather then a void yet it returns nothing. Why?
Code:

int MittSvar()
Code:

if (stykke == '+') svar = tall1 + tall2; // Here are we showing the Function how it should act when you put in the Values.
    if (stykke == '-') svar = tall1 - tall2; // Here are we showing the Function how it should act when you put in the Values.
    if (stykke == '*') svar = tall1 * tall2; // Here are we showing the Function how it should act when you put in the Values.
    if (stykke == '/') svar = tall1 / tall2; // Here are we showing the Function how it should act when you put in the Values.

Badly made since a switch/case is superior here.

Code:

system("PAUSE >nul");
Never use system calls for this, since you use C++ do cin.getline().

Otherwise great stuff.

GreenGrass 17Aug2008 15:01

Re: C++ Tutorial I
 
thanks for your comment Echo :)
Well i am not so good in C++ i am pretty new to it...

Echo 17Aug2008 19:51

Re: C++ Tutorial I
 
No problem.
Keep it up ;)

cyberex06 9Sep2008 14:34

Re: C++ Tutorial I
 
guyz... please help me... send me a software of c++ and examples of this programs to my e-mail address: glrex06@yahoo.com.ph. Thank you in advance...

mybestjavascript.com 15Sep2008 17:06

Re: C++ Tutorial I
 
good nice and keep going

Unknown 27Sep2008 06:59

Re: C++ Tutorial I
 
nice example


All times are GMT +5.5. The time now is 17:54.