HOME                                           http://bool.cjb.net 

                                     WELCOME TO HASIB'S WEB WORLD

                                                     PROGRAMMING WITH C++

                         (Some sample program code) Helpful for the beginners

Sample 1

1: #include <iostream.h>
2:
3: float Convert(float);
4: int main()
5: {
6: float TempFer;
7: float TempCel;
8:
9: cout << "Please enter the temperature in Fahrenheit: ";
10: cin >> TempFer;
11: TempCel = Convert(TempFer);
12: cout << "\nHere's the temperature in Celsius: ";
13: cout << TempCel << endl;
14: return 0;
15: }
16:
17: float Convert(float TempFer)
18: {
19: float TempCel;
20: TempCel = ((TempFer - 32) * 5) / 9;
21: return TempCel;
22: }

Output: Please enter the temperature in Fahrenheit: 212

Here's the temperature in Celsius: 100

Please enter the temperature in Fahrenheit: 32

Here's the temperature in Celsius: 0

Please enter the temperature in Fahrenheit: 85

Here's the temperature in Celsius: 29.4444

Sample 2

1: // demonstrates variables
2: // scoped within a block
3:
4: #include <iostream.h>
5:
6: void myFunc();
7:
8: int main()
9: {
10: int x = 5;
11: cout << "\nIn main x is: " << x;
12:
13: myFunc();
14:
15: cout << "\nBack in main, x is: " << x;
16: return 0;
17: }
18:
19: void myFunc()
20: {
21:
22: int x = 8;
23: cout << "\nIn myFunc, local x: " << x << endl;
24:
25: {
26: cout << "\nIn block in myFunc, x is: " << x;
27:
28: int x = 9;
29:
30: cout << "\nVery local x: " << x;
31: }
32:
33: cout << "\nOut of block, in myFunc, x: " << x << endl;
34: }

Output: In main x is: 5
In myFunc, local x: 8

In block in myFunc, x is: 8
Very local x: 9
Out of block, in myFunc, x: 8

Back in main, x is: 5

Sample 3

1: // 
2: // Looping with goto
3:
4: #include <iostream.h>
5:
6: int main()
7: {
8: int counter = 0; // initialize counter
9: loop: counter ++; // top of the loop
10: cout << "counter: " << counter << "\n";
11: if (counter < 5) // test the value
12: goto loop; // jump to the top
13:
14: cout << "Complete. Counter: " << counter << ".\n";
15: return 0;
16: }
Output: counter: 1
counter: 2
counter: 3
counter: 4
counter: 5
Complete. Counter: 5.

Sample 4

1: //
2: // Looping with while
3:
4: #include <iostream.h>
5:
6: int main()
7: {
8: int counter = 0; // initialize the condition
9:
10: while(counter < 5) // test condition still true
11: {
12: counter++; // body of the loop
13: cout << "counter: " << counter << "\n";
14: }
15:
16: cout << "Complete. Counter: " << counter << ".\n";
17: return 0;
18: }
Output: counter: 1
counter: 2
counter: 3
counter: 4
counter: 5
Complete. Counter: 5.

Sample 5

1: //
2: // demonstrates multiple statements in
3: // for loops
4:
5: #include <iostream.h>
6:
7: int main()
8: {
9: for (int i=0, j=0; i<3; i++, j++)
10: cout << "i: " << i << " j: " << j << endl;
11: return 0;
12: }

Output: i: 0 j: 0
i: 1 j: 1
i: 2 j: 2

Sample 6

1: // Using get() with parameters
2: #include <iostream.h>
3:
4: int main()
5: {
6: char a, b, c;
7:
8: cout << "Enter three letters: ";
9:
10: cin.get(a).get(b).get(c);
11:
12: cout << "a: " << a << "\nb: " << b << "\nc: " << c << endl;
13: return 0;
14: }

Output: Enter three letters: one
a: o
b: n
c: e

Sample 7

1: //Using getline()
2: #include <iostream.h>
3:
4: int main()
5: {
6: char stringOne[256];
7: char stringTwo[256];
8: char stringThree[256];
9:
10: cout << "Enter string one: ";
11: cin.getline(stringOne,256);
12: cout << "stringOne: " << stringOne << endl;
13:
14: cout << "Enter string two: ";
15: cin >> stringTwo;
16: cout << "stringTwo: " << stringTwo << endl;
17:
18: cout << "Enter string three: ";
19: cin.getline(stringThree,256);
20: cout << "stringThree: " << stringThree << endl;
21: return 0;
22: }

Output: Enter string one: one two three
stringOne: one two three
Enter string two: four five six
stringTwo: four
Enter string three: stringThree: five six

Sample 8

1: #include <iostream.h>
2:
3: inline unsigned long Square(unsigned long a) { return a * a; }
4: inline unsigned long Cube(unsigned long a) 
5: { return a * a * a; }
6: int main()
7: {
8: unsigned long x=1 ;
9: for (;;)
10: {
11: cout << "Enter a number (0 to quit): ";
12: cin >> x;
13: if (x == 0)
14: break;
15: cout << "You entered: " << x;
16: cout << ". Square(" << x << "): ";
17: cout << Square(x);
18: cout<< ". Cube(" _<< x << "): ";
19: cout << Cube(x) << "." << endl;
20: }
21: return 0;
22: }

Output: Enter a number (0 to quit): 1
You entered: 1. Square(1): 1. Cube(1): 1.
Enter a number (0 to quit): 2
You entered: 2. Square(2): 4. Cube(2): 8.
Enter a number (0 to quit): 3
You entered: 3. Square(3): 9. Cube(3): 27.
Enter a number (0 to quit): 4
You entered: 4. Square(4): 16. Cube(4): 64.
Enter a number (0 to quit): 5
You entered: 5. Square(5): 25. Cube(5): 125.
Enter a number (0 to quit): 6
You entered: 6. Square(6): 36. Cube(6): 216.
Enter a number (0 to quit): 0

Sample 9

1: #include <time.h>
2: #include <iostream.h>
3:
4: int main()
5: {
6: time_t currentTime;
7:
8: // get and print the current time
9: time (&currentTime); // fill now with the current time
10: cout << "It is now " << ctime(&currentTime) << endl;
11:
12: struct tm * ptm= localtime(&currentTime);
13:
14: cout << "Today is " << ((ptm->tm_mon)+1) << "/";
15: cout << ptm->tm_mday << "/";
16: cout << ptm->tm_year << endl;
17:
18: cout << "\nDone.";
19: return 0;
20: }
Output: It is now Mon Mar 31 13:50:10 1997

Today is 3/31/97

Done.

Hosted by www.Geocities.ws

1