Sunteți pe pagina 1din 18

C++ Programming Lab Manual

Lab1

Lab 1 Overview of programming and problem solving


Objectives:
In this lab, you will: Log in to a computer successfully Be able to use Visual C++ editor and C++ compiler to accomplish the following tasks successfully: o Load a file containing a program o Alter a file containing a program o Save a file o Print a file o Compile a file o Run a file o Change a program and rerun it o Correct a program with errors o Enter and run a program o Exit the system. Examine a C++ program Explore how a C++ program is processed Be able to discover the error and how to correct it Learn how to execute a program to print Welcome to C++ on the screen successfully

C++ Programming Lab Manual

Lab1

Assignment Cover Sheet


Name: -----------------------------------Section: ------------------------------------

Lab 1
Student ID: ---------------------------Date: -----------------------------

Activities

Assigned check or list exercise numbers

Submit (1) (2) (3)

Completed

Inlab

C++ Programming Lab Manual

Lab1

Outlines:
Lesson 1-1: Introduction to C++

Lesson 1-2: Introduction to Programming

Lesson 1-3: The Phases of writing a program

Lesson 1-4: Introduction to Visual C++ 2008 express Edition

Lesson 1-5: How to use Visual C++

Lesson 1-6: How to write your first program

Lesson 1-7: How to run a program with errors, read the errors and correct them.

C++ Programming Lab Manual

Lab1

Introduction to C++

Lesson 1-1

Welcome to C++! We have worked hard to create what we hope be an appropriate text book to give you a guidance to program by using C++. C++, an extension of C, was developed by Bjarne Stroustrup starting in 1979 at Bell Labs as an enhancement to the C programming language and originally named C with Classes. It was renamed C++ in 1983. As one of the most popular programming languages ever created, C++ is widely used in the software industry. Some of its application domains include systems software, application software, device drivers, embedded software, high-performance server and client applications, and entertainment software such as video games. Several groups provide both free and proprietary C++ compiler software, including the GNU Project, Microsoft, Intel and Borland. C++ has greatly influenced many other popular programming languages, most notably Java. C++ is also used for hardware design, where design is initially described in C++, then analyzed, architecturally constrained, and scheduled to create a register transfer level hardware description language via high-level synthesis.

Introduction to Programming

Lesson 1-2

Programming is a process of problem solving. Computer programming is the process of designing, writing, testing, debugging / troubleshooting, and maintaining the source code of computer programs. This source code is written in a programming language. The purpose of programming is to create a program that exhibits a certain desired behaviour (customization). The process of writing source code often requires expertise in many different subjects, including knowledge of the application domain, specialized algorithms and formal logic.

C++ Programming Lab Manual

Lab1

The Phases of Writing a Program


Every program need to pass some or all of the following:

Lesson 1-3

Input: Before a program can operate on data, the data need to be entered by the user. Process: To make some calculations and manipulation for the inserted data. Output: To show the processed information or any notation on the screen or any proper output device. Algorithm: A step-by-step problem-solving process in which a solution is arrived at in a finite amount of time. Example: One of the simplest algorithms is to print succeed if the students mark is >=50, and print failed if the students mark is <50, the algorithm represented in the flowchart below:

Fig1-1: Algorithm example

C++ Programming Lab Manual

Lab1

A typical C++ environment: 1- Editor: program is created and saved on the disk 2- Preprocessor: process the data 3- Compiler: Compiler creates object code and stores in a disk 4- Linker: Links the object code with the libraries 5- Loader: To load the program in memory 6- CPU: execute the instructions

Introduction to Visual C++ 2008

Lesson 1-4

Microsoft Visual C++ (often abbreviated as MSVC or VC++) is a commercial integrated development environment (IDE) product engineered by Microsoft for the C, C++, and C++/CLI programming languages. It has tools for developing and debugging C++ code, especially code written for the Microsoft Windows API, the DirectX API, and the Microsoft .NET Framework. In this lab we are going to use Visual C++ version 2008. Any other versions such as 2005 or visual studio 6 are acceptable versions.

C++ Programming Lab Manual

Lab1

How to Use Visual C++


You need to follow the following steps to create your first project:

Lesson 1-5

1. From Start go to All Programs then click on Visual C++2008 Express Edition. Then a screen of C++ software will be opened as Fig1-2.

Fig1-2: The initial screen of C++ 2008

C++ Programming Lab Manual

Lab1

2. From File click on New then choose Project see Fig1-3. A New Project screen will be shown as Fig1-4.

Fig1-3: Project command from file menu

Fig1-4: New Project Window

C++ Programming Lab Manual

Lab1

3. From the (New Project) window that appears, select Win32 Console Application. Then choose a name for your project (As start name it Pro1), then choose a location from Browse button to save it there (As start create a new folder with your id). Then click on Ok. After that a pop up window will appear click on Next. See Fig1-5, 1-6.

Write the name of the project

Browse a location to the project

Fig1-5: Project Name and Location

4. Another popup window will appear, choose Empty Project. Then, click on Finish.

Fig1-6: A window to choose an empty Project

C++ Programming Lab Manual

Lab1

5. To create a new form, you can go to Project from menu bar, then choose new item. Another way, right click on Source Files, then a popup menu will appear choose Add then choose New Form. See Fig1-7.

Fig1-7: Right click on the source file

6. Select C++ File and write the name of your form. (eg. start file name with your name). Fig1-8.

Choose C++File(.cpp)

Write the name of the project

Browse a location to the project

Fig1-8: A new Item Window

10

C++ Programming Lab Manual

Lab1

7. A new screen will be opened to write the code. This window is the editor window. See Fig1-9.

Notice that you are ready to write a cod in Lab1_1.ccp.file

This is the editor area

Fig1-9: The Editor Window

8. Write your code in the code area as below, this code is the general structure of any C++ program. (Its a program that does not do anything).

Fig1-10: A code that do nothing

9. To check that your code is free of errors, go to Build from menu bar and click on Build Solution, to insure that the code does not have any syntax errors or warning. Fig1-10.

11

C++ Programming Lab Manual

Lab1

Fig1-11: Build Solution command to check the code

No errors, No warnings in the program

Fig1-12: The Build output screen

12

C++ Programming Lab Manual

Lab1

10. To execute the program, from Debug menu click on Start Without Debugging. See Fig112.

Fig1-13: Executing the code

12. There is no output, because the program does not do anything. See Fig1-13.

Fig1-14: The Output

13

C++ Programming Lab Manual

Lab1

How to write your first program in C++

Lesson 1-6

Redo the previous steps to write the following C++ program then save, compile and run it.

#include <iostream> using namespace std; int main() { cout<<"Welcome to C++ "; return 0; }

//line1 //line2 //line3 //line4 //line5 //line6 //line7

What is the output?

#include <iostream>: Preprocessor directives. This tells the preprocessor to include in the program the contents of the input/output stream header file <iostream>. This file should be included for any C++ program. Using namespace std: To give the program the ability to use cin, cout, or endl. Int main() :main is a function that should be written in every C++ program. C++ program start executing at function main. The keyword int means that the main will return an integer value. You could replace it with void which means to return nothing. {: The left brace must begin the body of every function. }: right brace to end every function. Cout<<Welcome to C++<<endl; : a C++ statement, each statement should be end with semicolon (; ). Cout<< is an order to print on the screen the following Welcome to C++. Endl means to end the line on the screen. Return 0: end the main function.

14

C++ Programming Lab Manual

Lab1

General form of a C++ program

Fig1-15: General form of a C++ program

15

C++ Programming Lab Manual

Lab1

How to deal with the errors?


to View from menu bar and choose Output, or Alt+2. You need to figure out your errors and try to fix it. Compilation errors like:o Directive error. o Int main error o {} braces error o Semicolon error o ( ) parenthesis error o cout error o double quotation error Logical errors( will be explained later)

Lesson 1-7

You need to read the errors from the output window. If this window was not opened, then go

1. Write this program that has some errors.

#include <oistream> using namespace std int main { cout>>"Welcome to C++ ; return ; {

//line1 //line2 //line3 //line4 //line5 //line6 //line7

2. Write the errors that come out from the above program in the provided Error sheet, then
correct them and write the corrections.

16

C++ Programming Lab Manual

Lab1

InLab Assignment
1. To write a new program: Write a program to print your name and your id on the paper then on visual C++ 2008

2. To Open and edit an existing file: Open the saved file from the previous question to print your name, your ID, your address, and your age each in a single line.

17

C++ Programming Lab Manual

Lab1

Error sheet
Name: ---------------------------------Section: ------------------------------# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 # represents the line number in the program. OK Type of Error

Lab1
Student ID: ----------------------------Date: -------------------------------Corrections

18

S-ar putea să vă placă și