CSC 221: Computer Programming I Fall 2001  C++ basics  program structure: comments, #include, main, return  output: streams, cout, endl  input: variables,

Slides:



Advertisements
Similar presentations
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
Advertisements

1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Chapter 2: Basic Elements of C++
Chapter 2: Introduction to C++.
Basic Elements of C++ Chapter 2.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Input, Output, and Processing
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Course websites CS201 page link at my website: Lecture slides Assistant’s Information Recitations Office Hours Make-up.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Fundamental Programming: Fundamental Programming Introduction to C++
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Chapter 2 part #1 C++ Program Structure
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 2: Basic Elements of C++
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Bill Tucker Austin Community College COSC 1315
C++ First Steps.
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Completing the Problem-Solving Process
Computing Fundamentals
Basic Elements of C++.
Revision Lecture
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Course websites CS201 page link at my website: Lecture slides
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
Chapter 2: Introduction to C++.
Programs written in C and C++ can run on many different computers
C++ Programming Basics
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

CSC 221: Computer Programming I Fall 2001  C++ basics  program structure: comments, #include, main, return  output: streams, cout, endl  input: variables, identifiers  expressions: data types, operators, strings  using Visual C++  projects, C++ source files, build & run

syntax & semantics syntax: the precise form that programs & instructions must follow  computers are very picky  at first, we will spend time learning the computer's rules (e.g., semi-colon here, no space there)  don't get the impression that syntax is what programming is about foreign language analogy: vocabulary first  literature semantics: the meaning of the programs & instructions  computers are literal-minded i.e., they do what you say, not what you mean!  we will develop a repertoire of instructions/tools for solving problems carpentry analogy: simple tools first  artisanship  don't get the impression that programs are trivial, silly things we will start simple and build complexity as we learn

first C++ program // hello1.cpp Dave Reed 8/24/01 // // This simple program demonstrates C++ program structure. ////////////////////////////////////////////////////////// #include using namespace std; int main() { cout << "Hello and welcome to CSC221." << endl; return 0; } when compiled & executed, this program writes the message Hello and welcome to CSC221. on the screen

program components: comments // hello1.cpp Dave Reed 8/24/01 // // This simple program demonstrates C++ program structure. ////////////////////////////////////////////////////////// #include using namespace std; int main() { cout << "Hello and welcome to CSC221." << endl; return 0; } any text that appears on a line preceded by // is a comment  comments are completely ignored by the C++ compiler, so why have them?  can also specify comments using notation: /* comment */ every program you write should have a comment block at the top with  file name, author name, date, short description

program components: main // hello1.cpp Dave Reed 8/24/01 // // This simple program demonstrates C++ program structure. ////////////////////////////////////////////////////////// #include using namespace std; int main() { cout << "Hello and welcome to CSC221." << endl; return 0; } every program must have a function named main  this is the control center of the program whatever statements appear inside the curly braces are executed in order when the program executes  the function must "return" an integer value don't worry about it for now, just put "return 0;" at end int main() { statement1; statement2;... statementN; return 0; } in general:

program components: #include // hello1.cpp Dave Reed 8/24/01 // // This simple program demonstrates C++ program structure. ////////////////////////////////////////////////////////// #include using namespace std; int main() { cout << "Hello and welcome to CSC221." << endl; return 0; } C++ provides many useful libraries (separate files of code)  libraries are loaded using the #include directive #include loads the iostream library, containing input/output routines  note: in-class examples will follow the ANSI/ISO standard library files do not end in.h, must include using namespace std; at end

program components: output statements // hello1.cpp Dave Reed 8/24/01 // // This simple program demonstrates C++ program structure. ////////////////////////////////////////////////////////// #include using namespace std; int main() { cout << "Hello and welcome to CSC221." << endl; return 0; } cout is the standard C++ output stream, used for writing messages stream analogy: place message on stream, current carries it out  the stream insertion operator << is used to place messages on the stream text placed inside quotes is displayed as is (without the quotes) endl is a special keyword symbolizing a new-line character can place multiple items on the stream using multiple instances of <<

program components: output statements (cont.) // hello2.cpp Dave Reed 8/24/01 // // This simple program demonstrates C++ program structure. ////////////////////////////////////////////////////////// #include using namespace std; int main() { cout << "Hello and welcome to CSC221." << endl << endl; cout << "Programming is both intellectually challenging" << endl << "and artistically rewarding. Enjoy!" << endl; cout << " -- Dr. Reed" << endl; return 0; }  can have multiple cout statements in a program each is executed in order, top-to-bottom & left-to-right  can have a single cout statement that spans multiple lines more readable for long sentences (but can't break text in the middle)  can have multiple endl's to generate blank lines of output

program components: misc. // hello1.cpp Dave Reed 8/24/01 // // This simple program demonstrates C++ program structure. ////////////////////////////////////////////////////////// #include using namespace std; int main() { cout << "Hello and welcome to CSC221." << endl; return 0; }  C++ programs must be stored with a.cpp extension  statements must end with a semi-colon exceptions (since technically not statements): comments, #include, { and }  the return statement must come last inside main when the return statement is reached & executed, the program ends

program components: more misc. #include using namespace std; int main(){cout<< "Hello and welcome to CSC221."<<endl;return 0;}  blank lines and indentation are irrelevant to the compiler but help people to read the code & understand natural groupings of statements which of the above would you rather read and debug? // hello1.cpp Dave Reed 8/24/01 // // This simple program demonstrates C++ program structure. ////////////////////////////////////////////////////////// #include using namespace std; int main() { cout << "Hello and welcome to CSC221." << endl; return 0; }

syntax errors // hello3.cpp Dave Reed 8/24/01 // / This bug-filled program demonstrates syntax errors. ///////////////////////////////////////////////////////// %include using namespace std; int main() { cout << "Hello and welcome to CSC221. << endl; returm 0; } any violation of the format rules is called a syntax error  will be caught by the C++ compiler when you try to compile the program compiler will give you a descriptive error message, can click to identify a single problem can cause multiple error messages  fix first error then try again get used to errors, you will see them over & over (but try to learn from them!) how many syntax errors in the above program?

input & variables to read and process user input, first need a place to store the input a variable is a memory location where a value is stored  must declare a variable by specifying a type and a name variableType variableName; int age; // creates variable named age to store an integer double salary; // creates variable named salary to store a real value char initial; // creates variable named initial to store a character bool found; // creates variable named found to store true/false  wide range of numeric data types are provided, different ranges of values integer types: short, int, long, …real types: float, double, long double, …  variable names (a.k.a. identifiers) are sequences of letters, underscores, and digits that start with a letter (can also start with underscore, but don't) variable names are case-sensitive, so age, Age, and AGE are all different double salary, bonus; // can combine declarations using comma

input statements cin statement reads in a value from the user & stores it in a variable  cin is the standard C++ input stream (complements cout ) stream analogy: input value floats on stream, extract as it goes by  the stream extraction operator >> is used to read values from the stream can read multiple values from the stream using multiple instances of >> cin >> variableName; cin >> age; // reads integer value into age variable cin >> salary >> bonus; // reads real values (salary first, then bonus)  as a result of the cin statement, the value entered by the user is stored in the variable any future reference to that variable will access that value cout << "You entered " << age << " as your age." << endl;

input example // age.cpp Dave Reed 8/24/01 // // This program reads the user's age and echoes it. ///////////////////////////////////////////////////////// #include using namespace std; int main() { int age; cout << "Please enter your age: "; cin >> age; cout << "You entered " << age << " as your age." << endl; return 0; } before reading a value from the user, should display a prompt message

expression example // birth.cpp Dave Reed 8/24/01 // // This program determines the user's age given the // current year and their birth year. ///////////////////////////////////////////////////////// #include using namespace std; int main() { int currentYear, birthYear; cout << "What year is it? "; cin >> currentYear; cout << "And what year were you born? "; cin >> birthYear; cout << "That means you are either " << (currentYear - birthYear - 1) << " or " << (currentYear - birthYear) << " years old." << endl; return 0; } once values are stored in variables, can use in arithmetic expressions (more details later) + addition- subtraction * multiplication/ division

another example // ftoc.cpp Dave Reed 8/24/01 // // This program converts a temperature from Fahrenheit // to Celsius. ///////////////////////////////////////////////////////// #include using namespace std; int main() { double tempInFahr; cout << "Enter the temperature (in degrees Fahrenheit): "; cin >> tempInFahr; cout << "You entered " << tempInFahr << " degrees Fahrenheit." << endl << "That's equivalent to " << (5.0/9.0 * (tempInFahr - 32)) << " degrees Celsius" << endl; return 0; }

C++ strings primitive data types for storing integers, reals, characters, & Booleans are built-in to C++ a string type has been added for storing text  must include a library file that contains the string definition #include  once this has been done, can declare and use variables of type string string firstName; cout << "What is your name? "; cin >> firstName; cout << "Nice to meet you " << firstName << "." << endl;  the string type has many useful operations associated with it MORE LATER

string example // greet.cpp Dave Reed 8/24/01 // // This program displays a personalized greeting. ///////////////////////////////////////////////////////// #include using namespace std; int main() { string firstName, lastName; cout << "Enter your name (first then last): "; cin >> firstName >> lastName; cout << "Nice to meet you, " << firstName << " "<< lastName << ". May I just call you " << firstName << "?" << endl; return 0; } strings are delimited by whitespace (i.e., arbitrary sequences of spaces, tabs, and new-lines)

using Visual C++ I will assume you are programming using Visual C++  available in G411 and numerous sites on campus other C++ compilers exist (some free), but you are on your own  to create a C++ program with Visual C++ 1.Open Visual C++. Start menu  Programs  Microsoft Visual Studio  Microsoft Visual C++ 2.Create a project (collection of files that are compiled & linked together). File  New  Win32 Console Application must enter project name (e.g., hw1) and select location (e.g., a: ) will be prompted for default setup: select Empty Project 3.Create C++ source file and add to project. File  New  C++ Source File must enter file name (e.g., hw1.cpp) and verify project/location 4.Enter C++ program in file editor. note: editor does color coding, automatic indentation, … save file by selecting: File  Save (or just Ctrl-S)

using Visual C++  to execute a C++ program 1.Compile program Build  Build hw1.exe (or just F7) 2.Status of compilation/linking will appear in window below. if syntax errors occur, click on first error message cursor will point to offending line in editor – fix & recompile. if linker error occurs, check library names & verify "using namespace std;" 3.Execute program (but only after successful compilation/linking) Build  Execute hw1.exe (or just Ctrl-F5)  to open an existing project 1.Open Visual C++ (as before) 2.Open the project/workspace. File  Open Workspace browse to correct directory, select.dsw file or just locate the directory for the project, and double click on the.dsw file