Beruflich Dokumente
Kultur Dokumente
Program in C++.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
cout << "Hello World!" << endl << endl << "How are you?\n\n";
// This line will pause your program so it waits for you to press
any key and it will display:
// Press any key to continue . . .
system("pause");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
using namespace std;
void main()
{
// Below are variables and how to use them.
// Types are int, double, float, char and string.
// Initilization is important and goes after the =
// Reserved words are not to be used anywhere else.
int Num1 = 0, Num2 = 0, Num3 = 0;
double DNum1 = 0.0, DNum2 = 0.0 , DNum3 = 0.0;
float FNum1 = 0.0f, FNum2 = 0.00f, FNum3 = 0.00f;
char Letter1 = ' ', Letter2 = ' ', Letter3 = ' ';
string Word1 = "", Word2 = "", Word3 = "";
// This line will ask the user to input a number.
cout << "Please input Num1" << endl;
// This line will ask for an input. It will not display anything
// But it will wait for an input.
cin >> Num1;
cout << "Please input Num2\n";
cin >> Num2;
cout << "Please input Num3\n";
cin >> Num3;
// This outputs everything.
cout << Num1 << " " << Num2 << " " << Num3 << endl;
system("pause");
}
#include <iostream>
#include <string>
using namespace std;
void main()
{
// Variables and initilization.
int Num1 = 0, Num2 = 0, Num3 = 0;
double DNum1 = 0.0, DNum2 = 0.0 , DNum3 = 0.0;
12
float FNum1 = 0.0f, FNum2 = 0.00f, FNum3 = 0.00f;
13
14
char Letter1 = ' ', Letter2 = ' ', Letter3 = ' ';
15
string Word1 = "", Word2 = "", Word3 = "";
16
17
// Asking for input.
18
19
cout << "Please input Num1" << endl;
20
cin >> Num1;
21
22
cout << "Please input Num2\n";
23
cin >> Num2;
24
25
cout << "Please input Num3\n";
26
cin >> Num3;
27
28
// Example of if's.
29
30
// We HAVE to start with an if, then else if then else. You don't
31 have to have an else.
32
// You have to restate the Num1 each time you compare it.
33
// Example of and which is &&
34
35
if (Num1 == Num2 && Num1 == Num3)
36
{
37
cout << "Number 1 is equal to all other numbers" << endl;
38
}
39
40
// Else if is the same as if.
41
// Example of or which is ||
42
43
else if (Num1 == Num2 || Num1 == Num3)
44
{
45
46
// You have to start with if again since you are inside a
47 new bracet.
48
49
if (Num1 == Num2)
50
{
51
cout << "Number 1 is equal to Number 2" << endl;
52
}
53
54
else if (Num1 == Num3)
55
{
56
cout << "Number 1 is equal to Number 3" << endl;
57
}
58
}
59
60
// Example of else.
61
// It does not ge a condition.
62
63
else
64
{
65
cout << "Number 1 is not equal to any other numbers" <<
66 endl;
67
}
cout << Num1 << " " << Num2 << " " << Num3 << endl;
68
69
}
system("pause");
#include <iostream>
#include <string>
using namespace std;
void main()
{
// Variables.
int Num1 = 0, Num2 = 0, Num3 = 0;
double DNum1 = 0.0, DNum2 = 0.0 , DNum3 = 0.0;
float FNum1 = 0.0f, FNum2 = 0.00f, FNum3 = 0.00f;
char Letter1 = ' ', Letter2 = ' ', Letter3 = ' ';
string Word1 = "", Word2 = "", Word3 = "";
// Inputting an int in Num1.
cout << "Please input Num1" << endl;
cin >> Num1;
// Example of switch statement with numbers.
switch (Num1)
{
// In case it is a 1.
case 1:
cout << "You've entered number 1" << endl;
// You have to use breaks so the code stops here and won't
go on.
break;
// In case it is a 2.
case 2:
cout << "You've entered number 2" << endl;
break;
case 3:
case 'A':
cout << "You've entered letter A" << endl;
break;
case 'B':
cout << "You've entered letter B" << endl;
break;
case 'C':
cout << "You've entered letter C" << endl;
break;
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// In case it is between E - G
case 'E': case 'F': case 'G':
cout << "You've entered the letter between E - G" << endl;
break;
// Other letters go here.
default:
cout << "You've entered the wrong letter" << endl;
}
// Normal output. Not needed.
cout << Num1 << " " << Num2 << " " << Num3 << endl;
system("pause");
}
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;
void main()
{
// Initialization.
int Num1 = 0, Total = 0, i = 0;
// Processing.
// Example of do while loop.
// Goes at least once then checks the condition.
do
{
cout << "Please input a number: ";
cin >> Num1;
Total += Num1;
cout << endl;
// This is the counter;
i++;
27
28
// Condition right here.
29
30
} while(i < 5);
31
32
// Example of while loop.
33
34
// Used if we don't know how long we are looping.
35
// May not excute at all throuout the code if the condition is
36
false.
37
// Condition is while i is less than 5.
38
39
while (i < 5)
40
{
41
cout << "Please input a number: ";
42
cin >> Num1;
43
44
Total = Total + Num1;
45
cout << endl;
46
47
// Counter is right in here.
48
49
i++;
50
}
51
52
// Example of for loop.
53
54
// Start with where do we start from?
55
// How many do we go?
56
// Counter.
57
58
// (Start from; How many; counter)
59
60
for (int i = 0; i < 5; i++)
61
{
62
cout << "Please input a number: ";
63
cin >> Num1;
64
65
// These two codes are the same.
66
67
// Total = Total + Num1;
68
Total += Num1;
69
70
cout << endl;
71
}
72
73
// Output.
74
75
cout << "The Total is " << Total << endl;
76
77
system("pause");
78
}
This tutorial will show you how to use files for input and output.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
// Library for file input and output.
// Just like iostream, it is a stream.
#include <fstream>
using namespace std;
void main()
{
// Variable.
float Num1 = 0.0f, Num2 = 0.0f, Total = 0.0f;
char Oper = ' ';
// Making the file input and file output.
// They are just like variables.
ifstream fin;
ofstream fout;
// The open function opens files.
// fin will not create the file for you, you have to make
// it and it is very case sensative.
fin.open("Input.txt");
// The output file will be created for you.
fout.open("Output.txt");
// This if is to check if the file was opened or not.
if (!fin)
{
cout << "File is not opening right." << endl;
system("pause");
exit(0);
}
// Processing.
// Input before we go into the loop.
// The input will take a number, number, then a char.
//
//
//
//
//
//
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 }
Num1 + Num2;
Num1 - Num2;
Num1 * Num2;
Num1 / Num2;
"The operator used is wrong." << endl;
Total = 0.0f;
// Output.
fout << "Total is " << Total << endl;
// Input at the end of processing inside the loop
// to check for end of file correctly.
// Otherwise, we'll run the last line twice.
fin >> Num1 >> Num2 >> Oper;
}
system("pause");
Content of Input.txt
5 5 +
9 3.5 8 6 *
50 23 /
9 7 &
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// This is a global variable which can be used anywhere in the code.
string GlobalWord = "Hello";
// These are prototypes, they come before main.
// Reason for this is because the compiler is like a highway, if it
doesn't see
// that there is a function before it, it won't see it.
// To let it know that there is infact something behind it, we do
prototypes.
// You just copy the first line of a function and paste it here with a
semicolon.
string StrFunc1();
void VoidFunc1(string WordFromMain);
void SimpleCalc(char &Choice, float &Total, float &Num1, float &Num2,
ifstream &fin);
void main()
{
char Choice = ' ';
float Total = 0.0f, Num1 = 0.0f, Num2 = 0.0f;
ifstream fin;
fin.open("Input.txt");
fin >> Num1 >> Num2 >> Choice;
while (!fin.eof())
{
// This is how you call a function.
// The inputs have to match the parameters of the code.
SimpleCalc(Choice, Total, Num1, Num2, fin);
cout << Total << " " << GlobalWord << endl;
}
// VoidFunc1(Word);
/*cout << StrFunc1() << endl;*/
}
system("pause");
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
return Word;
cout << Word << " " << WordFromMain << endl;
// No need for a return because its a void function.
}
Content of Input.txt
50 40 *
3 6 /
5 8 9 6 +
4 8 "
Code:
1
2
3
#include <iostream>
using namespace std;
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
//
//
//
// You also need to position it here, right under using namespace std;
// so that every function can see it.
enum NumberChoice {ONE, TWO, THREE, ERROR};
// Function prototypes.
void processNum(int Num, NumberChoice &FunctionChoice);
void displayNum(NumberChoice Choice);
void main()
{
int Num = 0;
NumberChoice Choice = ERROR;
// Input from the user.
cout << "Please input a number between 1-3: ";
cin >> Num;
// This function checks what the Num is.
processNum(Num, Choice);
// This function displays what Num did the user display.
displayNum(Choice);
}
system("pause");
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
case 2:
case 3:
FunctionChoice = TWO;
break;
FunctionChoice = THREE;
break;
default:
FunctionChoice = ERROR;
}
}
This tutorial introduces you on how you can use arrays rather than
single variables. Its useful when dealing with files and a large
amount of data.
*When passing to a function, no need to include the brackets, just the variable name.
*When using inside a function, you need to use the brackets but without a size, the size will be
passed through main.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// Here we use a constant for the array size so
// we don't have to keep chaning the sizes all the time.
const int MAX_ARRAY = 5;
// Function prototypes.
void fillArray(int Num[], string Word[]);
void displayArray(int Num[], string Word[]);
void fillCharArray(ifstream &fin, char Letters[], int &size);
void displayCharArray(char Letters[], int size);
void main()
{
ifstream fin("Input.txt");
// Defining int, char and string arrays.
// To give a size, do [size]
// If you don't know the size, do []
// To fill all of them at once you can do:
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
}
}
// Displaying the array of chars.
void displayCharArray(char Letters[], int size)
{
for (int i = 0; i < size; i++)
{
cout << Letters[i];
}
}
Content of Input.txt
IndieDevelopment.co
This intro tutorial will show you how you can use structures to
better compact your code.
Example of Structure:
*Can not initialize variables inside it.
*Needs a semicolon after the last curly bracket.
Header File:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Libraries.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// This is the structor and person is the variable name.
struct person
{
// Structor variables.
string name;
float salary;
}; // Semicolin is needed here.
Main File:
1
2
3
4
5
6
endl;
}
}
system("pause");
This intro tutorial will show you how you can use classes which is
the most up to date way of programming and will help you use less
code and less variables and produce faster programs.
Class Example:
*Put into a Header file.
*Must use a semicolin after the last squirrely bracket.
*Classes need names.
*Items inside the private area can not be used outside the class unless we access the class first.
*Items used in the public area will have the ability to use the private items and will be used for
functions.
*Constructures run automatically once you call the class in main.
CardHeader.h File:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
// The class is here. Card is the class name.
class Card
{
// The private section is set by default but also can be
// written as private:
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
CardHeaderDriver.h File:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
face = 2;
suit = 'H';
72
73
74
75
76
77
78
79
80
81
int Card::getFace()
{
return face;
}
char Card::getSuit()
{
return suit;
}
Main File:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 }
hand.setSuit('H');
hand.setFace(1);
// Using display again.
hand.display();
hand.setFace(1);
// Using the getters to display the face and the suit with the
// cout.
cout << "This is the suit " << hand.getSuit()
<< " This is the face " << hand.getFace()
<< endl;
system("pause");