Tutorial: First C++ Program

Author: Ronald S. Holland

Total Application Works





To visit my site

HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#


To contact us

2002 - 2016 All Rights Reserved Total Application Works


  • Tell a friend about this site (copy and paste the following HTML page into a file (e.g., using Notepad).)

  • <html>
    <head>
    <title>Example of a link </title>
    </head>
    <body>
    <a href="http://sumtotalz.com/TotalAppsWorks/CPP/GettingStarted/FirstCppProgram_Tut.html">
    Tutorial: First C++ Program </a>
    </table>
    </body>
    </html>
    Download Source



Read Me First

The FCPlus compressed file contains the following seven projects:

  1. helloworld.VC++ 6 Project (.dsp)
  2. secondPgm.VC++ 6 Project (.dsp)
  3. thirdPgm.VC++ 6 Project (.dsp)
  4. fourthPgm.VC++ 6 Project (.dsp)
  5. fifthPgm.VC++ 6 Project (.dsp)
  6. sixthPgm.VC++ 6 Project (.dsp)
  7. seventhPgm.VC++ 6 Project (.dsp)
To run any of the projects:
  1. Go to the folder where you downloaded the FCPlus compressed file
  2. Extract the files
  3. Right clck on one of the project names - click on the version of Microsoft studio you have
  4. When Microsoft studio IDE is started
  5. Click on theDebug menu
  6. Click on theStart without Debugging menu
    • the particular project will begin to execute

Introduction

This tutorial is intended for that person who is getting started with C++. In order for you to be able to follow this tutorial, you need a C++ compiler. I used Microsoft's Visual Studio C++ 2008 Edition . I don't believe in buying something if you can get it for free. At this point, if you don't have a compiler, you can download one for free from

Some tutorials on Microsoft's Visual Studio C++ 2008 Edition


There are some books on the market that come with a CD containing a compiler e.g., Sam's Teach Yourself C++ in 21 Days. This is not an endorsement of the book, but simply a suggestion of how to get a top quality C++ compiler. If you buy a book, make sure it contains a copy of a C++ Compiler or preferably a copy of the MS Visual Studio C++. First year college textbooks on C++ may also be a good source. Before you spend any money, download Microsoft's Visual Studio C++ 2008 Edition free copy.

What is a Template?

If you look in a dictionary, template is defined as

    a pattern, usually in the form of a thin plate of metal, wood, plastic, etc., for forming an accurate copy of an object or shape

Our template looks like:

  
/***************************************************
 * Comments
 * 
 **************************************************/

// #include header files

/* All C++ programs must have one main() function */
void main() {
   //code to do something        
}

  

In the above template, { begins the body of the program (where the code resides), and } indicates the end of the main() function. You should compile and execute the above template.

Writing Our First Program

We will use the above template to write our first program.

  1. We will change the multiline comment to look like:
      
    /***************************************************
     * File: helloworld.cpp
     *
     * Purpose: Write a message to the standard out
     * 
     **************************************************/
      
  2. We will change the include to
      
    
    #include <iostream> // this file must be included by any
                          // program tha output to the screen
      
  3. We will change the main() function to
    Source
      
    /*****************************************************
     * File: helloworld.cpp
     *
     * Purpose: Write a message to the standard out
     ****************************************************/
    
    #include <iostream>
    
    void main()  {
        cout << " Hello World!\n\n" << endl ;
        cout << " from\n\n" << endl ;
        cout << " Ronald S. Holland\n\n" << endl ;
        cout << "at\n\n" << endl ;
        cout << " Total Application Works\n\n\" << endl ;
    }
    
      

    Figure 1: Hello World
      
    void main()  {
        cout << " Hello World! \n\n\n";
    }
      
  4. When we put it all together, we get
    Source
      
    /*****************************************************
     * File: helloworld.cpp
     *
     * Purpose: Write a message to the standard out
     ****************************************************/
    
    #include <iostream>
    
    void main()  {
        cout << " Hello World!\n\n" << endl ;
        cout << " from\n\n" << endl ;
        cout << " Ronald S. Holland\n\n" << endl ;
        cout << "at\n\n" << endl ;
        cout << " Total Application Works\n\n\" << endl ;
    }
    
      

    Output
     Hello World!
    
    
     from
    
    
     Ronald S. Holland
    
    
     at
    
    
     Total Application Works
    
    
    Press any key to continue . . .
    
    Figure 2: Hello World
    In the above code,
    • cout - The object controls insertions to the standard output e.g., screen
    • << - this is the stream insertion operator. When the program executes, the data (" Hello World!") is sent to the screen.
    • endl - This manipulator, when inserted into an output stream, inserts a newline character and then flushes the buffer.

Writing Our Second Program

Our Second Program will

  • Declare two integer variables a and b.
  • Initialize a to 5 and b to 10.
  • Add a and b together.
  • Write the result to the screen.
Source
  
/*****************************************************
 * File: secondPgm.cpp
 *
 * Purpose: add two number and write to the standard out
 ****************************************************/

#include <iostream>

void main()  {
   int a, b;

   a = 5 ;
   b = 10 ; 

   cout << "The value of a is " << a << endl;
   cout << "The value of b is " <<  b << endl;
   cout << "The value of a + b is " << (a + b) << endl;
} 

  

Output
The value of a is 5

The value of b is 10

The value of a + b is 15

Press any key to continue . . .
Figure 3: Second program source and output

Writing Our Third Program

Our Third Program will

  • Declare three integer variables a, b and c.
  • Initialize a to 5 and b to 10.
  • Add a and b together and store the result in c.
  • Write the value of c to the screen.
Source Code
  
/*****************************************************
 * File: thirdPgm.cpp
 *
 * Purpose: add two numbers, store the result in 
 * variable c, and then write the value of c out to the screen
 *           
 ****************************************************/

#include <iostream>

void main()  {
   int a, b, c;

   a = 5 ;
   b = 10 ; 

   c = a + b ;

   cout << "The value of a is " << a << endl;
   cout << "The value of b is " <<  b << endl;
   cout << "The value of c = a + b is " << c << endl;
} 

  

Output
 
The value of a is 5

The value of b is 10

The value of a + b is 15

Press any key to continue . . .
 
Figure 4: Third program example

Writing Our Fourth Program

Instead of initializing the values for a and b, our Fourth Program will request the values from the user.

  • Declare three integer variables a, b and sum.
  • Request that the user enter two integers, storing one integer in a and the other in b.
  • Add a and b together and store the result in sum.
  • Write the value of sum to the screen.
Source Code
  
/*****************************************************
 * File: fourthPgm.cpp
 *
 * Purpose: request two numbers from the user, 
 * 1- Add the two numbers and store the result in 
 *    variable sum and write the value of sum out to 
 *    the screen
 * 2- Multiply the two numbers and store the result in 
 *    variable product and write the value of product 
 *    out to the screen
 * 3- Average the two numbers and store the result in 
 *    variable average and write the value of average 
 *    out to the screen
 *           
 *****************************************************/

#include <iostream>

void main()  {
   int a, b, sum, product, average;

   cout << "Enter the first integer" << endl;
   cin >> a ;

   cout << "Enter the second integer" << endl;
   cin >> b ;

   sum     = a + b ;
   product = a * b ;
   average = (a + b) / 2 ;


   cout << "The value of a is " << a << endl;
   cout << "The value of b is " <<  b << endl;
   cout << "The value of sum = a + b is " << sum << endl;
   cout << "The value of product = a * b is " << product << endl;
   cout << "The value of average = (a + b) / 2 is " << average << endl;
} 

  
Output
 
Enter the first integer
5
Enter the second integer
5
The value of a is 5
The value of b is 5
The value of sum = a + b is 10
The value of product = a * b is 25
The value of average = (a + b) / 2 is 5
Press any key to continue . . .
 
Figure 5: Fourth Program Example


In the above example, cin controls extractions from the standard input (keyboard).

Writing Our Fifth Program

In our fifth program, we will

  • Declare four integer variables a, b, c, sum, product, and average,.
  • Request that the user enter three integers, storing one integer in a, one integer in b and the third in c.
  • Add a, b and c together and store the result in sum.
      Write the value of sum to the screen
  • Multiply a, b and c together and store the result in product.
      Write the value of product to the screen
  • Add a, b and c together, calculate the average and store the result in average.
      Write the value of sum to the screen

Writing Our Sixth Program

Our Sixth Program will request two numbers from the user and raise the first value to the power of the second value.

  • Declare four integer variables x, y, ii and power.
  • Request that the user enter two integers, storing one integer in x and the other in y.
  • Multiply x and y times and store the result in power.
  • Write the value of power to the screen.
Source Code
  
/*****************************************************
 * File: sixthPgm.cpp
 *
 * Purpose: Obtain two numbers. raise the first number 
 *          to the power of the second e.g., x ^ y
 *          - raise x to the y power 
 ****************************************************/


#include 

using namespace std;

int main()
{
   int x, y, ii, power;

   ii = 1;
   power = 1;
   cout << "Enter the first integer" << endl;
   cin >> x ;

   cout << "Enter the second integer" << endl;
   cin >> y ;

   while (ii <= y) {
      power *= x;
      ++ii;
   }

   cout << "The value of " << x << "^" << y << " is " << power << "\n\n\n" << endl;
   return 0;
}


  
Output
 
Enter the first integer
5
Enter the second integer
3
The value of 5^3 is 125



Press any key to continue . . .

 
Figure 6: Sixth Program Example


In the above example, cin controls extractions from the standard input (keyboard).

Writing Our Seventh Program

Our Seventh Program will request two numbers from the user and raise the first value to the power of the second value.

  • Declare one integer variable workDays.
  • Declare three float variables workHours, payRate, weeklyPay.
  • Multiply the workDays by workHours by payRate to get weeklyPay.
  • Write the value of workDays, workHours, and weeklyPay to the screen.
Source Code
  

/*****************************************************
 * File: seventhPgm.cpp
 *
 * Purpose: Obtain two numbers. raise the first number 
 *          to the power of the second e.g., x ^ y
 *          - raise x to the y power 
 ****************************************************/


#include 

using namespace std;

int main() {
   int	    workDays  = 5; 
   float    workHours = 8.0, payRate = 38.55, weeklyPay;

   weeklyPay = (float)workDays * workHours * payRate;

   cout << "workDays =s " << workDays << endl ;
   cout << "workHours =s " << workHours << endl ;
   cout << "\n\nWeekly Pay =s " << weeklyPay << endl ;
   cout << "\n\n\n";

   return 0;
}


  
Output
 
workDays  =s 5
workHours =s 8


Weekly Pay =s 1638.38



Press any key to continue . . .

 
Figure 7: Seventh Program Example


In the above example, cin controls extractions from the standard input (keyboard).

Definitions

Comment - comments help document a program but do cause the computer to perform any action. They are ignored when the program executes. They are of the form:

  • // this symbol indicates that all that follows the // is a comment
  • A multiline comment starts with /* and ends with */
    /*
     * This is a multiline comment
     */
    

Function - a named section of a program that performs the specific task included between the { and the }.

<iostream> - to declare objects that control reading from and writing to the standard streams. This is often the only header you need include to perform input and output from a C++ program.

Solution to #5

Source Code
  

  
  
/*****************************************************
 * File: fifthPgm.cpp
 * Purpose: request three numbers from the user,  

 * 1- Add the three numbers and store the result in 
 *    variable sum
 * 2- Multiply the three numbers and store the result 
 *    in variable product
 * 3- add the three numbers, determine the average of 
 *    the three numbers and store the result in 
 *    variable average
 *
 * 4- Write the three values entered by the user
 * 5- Write the value of sum out to the screen
 * 6- Write the value of product out to the screen 
 * 7- Write the value of average out to the screen 
 *           
 *****************************************************/

#include <iostream>

void main()  {
   int a, b, c, sum, product, average;

   cout << "Enter the first integer" << endl;
   cin >> a ;

   cout << "Enter the second integer" << endl;
   cin >> b ;

   cout << "Enter the third integer" << endl;
   cin >> c ;

   sum      = a + b + c ;
   product  = a * b * c ;
   average = (a + b + c) / 3 ;

   cout << "The value of a is " << a << endl;
   cout << "The value of b is " <<  b << endl;
   cout << "The value of c is " <<  c << endl;
   cout << "The value of a + b + c is " << sum << endl;
   cout << "The value of a * b * c is " << product << endl;
   cout << "The value of (a + b + c) / 3 is " << average << endl;
} 
  

  
Output
 
Enter the first integer
5
Enter the second integer
5
Enter the third integer
5
The value of a is 5
The value of b is 5
The value of c is 5
The value of a + b + c is 15
The value of a * b * c is 125
The value of (a + b + c) / 3 is 5
Press any key to continue . . .
 
Figure 8: Fifth Program Example

Installing Microsoft's Visual Studio C++ 2008 Edition

This tool is not supported by Microsoft product support services. Below you will find the Readme instructions that comes with the Virtual CD-ROM Control Panel v2.0.1.1 download.

 
  1. System Requirements
    • Windows XP Home or Windows XP Professional
    • I copied the XP copy of Microsoft's Visual Studio C++ 2008 Edition onto the Vista operating system
      • Windows Explorer doesn't see the virtual drive.
  2. Installation instructions
    1. Copy VCdRom.sys to your WINDOWS\system32\drivers folder.
    2. Execute VCdControlTool.exe



    3. Click Driver control



    4. If the "Install Driver button is available, click it. Navigate to the WINDOWS\system32\drivers folder, select VCdRom.sys, and click Open.
    5. Click Start
    6. Click OK.
    7. Click Add Drive to add a drive to the drive list. Ensure that the drive added is not a local drive. If it is, continue to click Add Drive until an unused drive letter is available.



    8. Select an unused drive letter from the drive list and click Mount.
    9. Navigate to the image file, select it, and press OK. UNC naming conventions should not be used, however mapped network drives should be OK. Click OK.



    At this point, you may can use the drive letter as if it were a local CD-ROM device. When you are finished you may
    1. unmount,
    2. stop, and
    3. remove the driver from memory using the driver control dialog.




HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#

2002 - 2016 All Rights Reserved Total Application Works

<