C++ Tutorial I

GreenGrass's Avatar author of C++ Tutorial I
This is an article on C++ Tutorial I in C++.
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)
 
Pro contributor
8Aug2008,23:46   #2
hanleyhansen's Avatar
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.
Ambitious contributor
9Aug2008,12:23   #3
seangtz's Avatar
Thx for sharing........ useful information.
Ambitious contributor
9Aug2008,17:11   #4
GreenGrass's Avatar
thanks for the comment hanleyhansen i will try remember that to next time..
Newbie Member
17Aug2008,14:14   #5
Echo's Avatar
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.
Ambitious contributor
17Aug2008,15:01   #6
GreenGrass's Avatar
thanks for your comment Echo
Well i am not so good in C++ i am pretty new to it...
Newbie Member
17Aug2008,19:51   #7
Echo's Avatar
No problem.
Keep it up
Newbie Member
9Sep2008,14:34   #8
cyberex06's Avatar
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...
Newbie Member
15Sep2008,17:06   #9
mybestjavascript.com's Avatar
good nice and keep going
Newbie Member
27Sep2008,06:59   #10
Unknown's Avatar
nice example