CMPE110 - EXPERIMENT 1 * MICROSOFT VISUAL STUDIO AND C++ PROGRAMMING

Similar documents
MICROSOFT VISUAL STUDIO AND C PROGRAMMING

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Understanding main() function Input/Output Streams

2 nd Week Lecture Notes

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

CHAPTER 3 BASIC INSTRUCTION OF C++

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Tutorial 2: Compiling and Running C++ Source Code

A First Program - Greeting.cpp

C++ Support Classes (Data and Variables)

Lab # 02. Basic Elements of C++ _ Part1

Computer Programming : C++

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Program Organization and Comments

4. Structure of a C++ program

VARIABLES & ASSIGNMENTS

Your First C++ Program. September 1, 2010

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

BITG 1233: Introduction to C++

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Chapter 2: Overview of C++

Chapter 2 C++ Fundamentals

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

The C++ Language. Arizona State University 1

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

CSc Introduction to Computing

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

A SHORT COURSE ON C++

Lecture 2 Tao Wang 1

CSc Introduc/on to Compu/ng. Lecture 17 Edgardo Molina Fall 2011 City College of New York

CS2255 HOMEWORK #1 Fall 2012

Ch 6. Functions. Example: function calls function

Programming with C++ as a Second Language

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

LECTURE 02 INTRODUCTION TO C++

Unit 7. 'while' Loops

Fundamentals of Programming Session 4

C++ Programming: From Problem Analysis to Program Design, Third Edition

Creating a C++ Program

Getting Started with Visual Studio

Lab 4: Introduction to Programming

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

UNIT- 3 Introduction to C++

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Chapter 2 Basic Elements of C++

Variable Definitions and Scope

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Elementary Programming

Chapter 2: Basic Elements of C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Getting started with C++ (Part 2)

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Chapter 1 INTRODUCTION

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

10. Functions (Part 2)

Homework #3 CS2255 Fall 2012

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Chapter 1 - What s in a program?

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

Objectives. In this chapter, you will:

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

CSCE Practice Midterm. Data Types

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

CMSC 202 Midterm Exam 1 Fall 2015

OBJECT ORIENTED PROGRAMMING USING C++

This watermark does not appear in the registered version - Slide 1

Pointers, Arrays and C-Strings

Streams. Ali Malik

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

A A B U n i v e r s i t y

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

5. Control Statements

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

6 Functions. 6.1 Focus on Software Engineering: Modular Programming TOPICS. CONCEPT: A program may be broken up into manageable functions.

UEE1302 (1102) F10: Introduction to Computers and Programming

Number: Name-Surname :... Group:...

Introduction to C++ Chapter

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Preprocessor Directives

EP241 Computer Programming

Programming. C++ Basics

Chapter 2. Outline. Simple C++ Programs

1) Log on to the computer using your PU net ID and password.

Transcription:

CMPE110 - EXPERIMENT 1 * MICROSOFT VISUAL STUDIO AND C++ PROGRAMMING Aims 1. Learning primary functions of Microsoft Visual Studio 2008 * 2. Introduction to C++ Programming 3. Running C++ programs using Microsoft Visual Studio Creating a New Project To create a new project, start Visual Studio 2008 (Start Programs Microsoft Visual Studio 2008 Microsoft Visual Studio 2008). Select Project from the New list contained in the File menu, as shown in Figure 1. This operation will display a dialog screen shown in Figure 2. Select Visual C++ from the Project types list shown on the left. Select Empty project Figure 1: File menu from the right list and give a name to your project. (Note that you may need your project later on; therefore, choose a suitable name that you can remember). These steps will create an empty project for you to work on. * Translated by Cem Kalyoncu and Yıltan Bitirim from BLGM101 laboratory sheets. 1

Figure 2: New project window The first step to do in a new project is to create a file that we can write our codes into. Firstly, find Project explorer window. If it is hidden, you can open it from View menu. Right click on empty part of Project explorer and choose New item from Add menu as shown in Figure 3. This operation will open the window shown in Figure 4. From this window, choose C++ file and supply its name to create your file. Figure 3: Add file menu 2

Figure 4: New item dialog Running C++ Programs To run your program you can press the button shown in Figure 5 or F5 key. After issuing run command, the window shown in Figure 5 will be displayed. This dialog window asks if we want to compile our program. Since we want to run the codes we typed we have to compile our program. Therefore, the answer should be Yes. If there are no errors in your program, a console screen will be displayed. If your program requires input, you can enter it through this screen. Also your program output will be shown in here. 3

Run button Figure 5: Running your program Fixing Problems If there is an error in your program while compiling, Visual studio will ask if you want to run last working version of your program. Since we need to fix the problems we will answer No to this question and abort the running our program. After closing this dialog, a list of errors will be displayed. This window is shown in Figure 6. If you cannot see this window in your screen, you can open it using View menu. On the top of this list you will find Errors, Warnings and Messages buttons. These buttons control the visibility of these types of items. We are mostly interested in Errors and Warnings, so make sure that these two will be displayed. If you double click on an error or warning in this list, Visual Studio will take you to the line that is causing the error or warning. Some errors are caused by the general settings of your program; therefore, the line number information might not be available. 4

Figure 6: Error list It is important to know that an error might be caused by previous line. If you are getting too many errors or if you are sure that the line displayed in the error list does not contain any problems, remember to check quotes, parentheses or semicolons ( ; ) in the previous line. Most of the time missing semicolon error is given to the next line. There are errors that cause your program to generate unexpected results. These are called logic errors and will not be handled by compiler. If you have such an error, examine your program carefully. These types of errors will be explained later. The following is the list of most common errors. There might be errors that cannot be detected while your program is being compiled. Also common errors falling to this category is listed here. 1. Invalid symbol at include command Error: Expected Filename... After include preprocessor command, the file name should be written in angled brackets (< >). 2. Quotation errors Error: unterminated string literal In C programming language string literals should be surrounded by straight double quotes and each string should be written in a single line. Word processors replaces straight double quotes with opening and closing quotes, so be careful while copying from text documents. 3. main function is missing or defined multiple times Error: undefined symbol main or symbol main is already defined All C programs must have a main function. Check the name of it. In C language a function can be defined only once (there are exceptions to this rule) per project, therefore, you cannot add a second file and write another main function in there. 4. WinMain function is required Error: undefined symbol WinMain You can write Windows applications in Visual Studio 2008. If you open such a project, the steps you should follow are different from what are shown in this lecture. Therefore, if you have this 5

error, it means that you have chosen wrong project type. Create a new project and move your code to it. 5. Semicolon, coma or parenthesis missing Error: Unexpected... expecting... This error states that you have forgotten the specified symbol. Do not forget that this error is actually exist in somewhere before the error. Therefore, check the previous statements and even previous lines. 6. Unassigned variable Warning: Variable... is used without initializing This occurs if the given variable is not initialized before it is used. In C programming language the initial value of variables are undefined. Therefore, you have to assign a value to it before you use. This problem causes a warning to be issued and you can tell it to continue. However, your program might have problems in run time. 7. Address Error Error: System error or access denied Experiments Edit and execute the following codes and perform the given tasks: 1. #include<iostream> using namespace std; int main(){ cout << "Hello" << endl; cout << "My name is Ali" <<endl; system("pause"); return 0;} a) How can you modify the code produce the following output on the computer monitor: Hello My name is Ali 2. The following code reads two integer numbers (x, y) from the keyboard and finds their sum, i.e., x+y. #include<iostream> using namespace std; int main(){ int x,y; cout << "Enter two Numbers:"; cin >> x >> y; cout << x << "+" << y << "=" << x + y << endl; system("pause"); return 0;} a) Execute this code using the following input entered from the keyboard: 5 12 b) How can you modify this code to read three integer numbers (x, y, z) and compute their product, i.e., x*y*z. Use the following input for execution: 5 12 2 3. Consider the following code that computes ad prints the area of a circle with the radius r. The area of the circle is computed by: area r 2 6

#include<iostream> #include<cmath> using namespace std; int main(){ const double pi=22.0/7.0; double radius,area; cout << "Enter the radius of a circle: "; cin >> radius; area=pi*pow(radius,2); cout << "The area of the circle=" << area << endl; system("pause"); return 0;} a) Edit, compile and execute this code. Use the following input values for the radius r: 4 b) Modify the given code to read the radius of a sphere and then compute and print the sphere volume. Note: The volume of a sphere of radius r is computed as: 4 3 volume r 3 A sample run of the program must be as follows: *** PROGRAM TO CACLCULATE THE VOLUME OF A SPHERE *** Enter the radius of the sphere: 2.0 The volume of the sphere is 33.5. 7