Simplest program and Data Output Sen Zhang. The simplest program looks like a single person company! void main() { // this starts a comment line // here.

Slides:



Advertisements
Similar presentations
Objectives You should be able to describe: Introduction to Programming
Advertisements

Chapter 1 Getting Started
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 2: Basic Elements of C++
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
Data types and variables
Chapter 2: Basic Elements of C++
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 2: Basic Elements of C++
1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 3: Input/Output
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Objectives You should be able to describe: Data Types
Chapter 3 Getting Started with C++
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
FORMATTED INPUT OUTPUT. Topics to be discussed……………….. Formatted Console I/O operationsFormatted Console I/O operations Defining field width :Width()Defining.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
1 C++ Syntax and Semantics, and the Program Development Process.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
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.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
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 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Chapter 3 Arithmetic Expressions, Function Calls, and Output
Chapter 3: Assignment, Formatting, and Interactive Input.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++
Formatting Output  Escape Sequences  iomanip.h Objects  setw()  setiosflags(…)  setprecision()
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Introduction to C++.  Computers: CPU, Memory & Input / Output (IO)  Program: Sequence of instructions for the computer.  Operating system: Program.
Chapter 05 (Part II) Control Statements: Part II.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 2: Basic Elements of C++
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
1 A Simple “Hello World” Example #include // input-output library using namespace std; int main() // function main { cout
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
A First Book of C++ Chapter 1 Getting Started. Objectives In this chapter, you will learn about: –Introduction to Programming –Function and Class Names.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
C++ First Steps.
Chapter 2: Basic Elements of C++
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
Chapter Topics The Basics of a C++ Program Data Types
Topic Pre-processor cout To output a message.
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Chapter 3: Input/Output
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
CHAPTER 2: COMPONENTS OF PROGRAMMING LANGUAGE
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
C++ Programming Basics
COMS 261 Computer Science I
Presentation transcript:

Simplest program and Data Output Sen Zhang

The simplest program looks like a single person company! void main() { // this starts a comment line // here is where you put your code… } One function, with a head and the empty body

The simplest program looks like a single person company! int main() { // this starts a comment line // here is where you put your code… return 0; } One function, with a head and the empty body

Why main needs to return a value ? DOS command to determine the return code is ERRORLEVEL, most people use the name errorlevel. Errorlevels are not a standard feature of every command. A certain errorlevel may mean anything the programmer wanted it to. Most programmers agree that an errorlevel 0 means the command executed succesfully, and an errorlevel 1 or higher usually spells trouble. But there are many exceptions to this general rule. int main() { return 11.9; } c:\sen\t1222\Debug>echo %errorlevel% 0 c:\sen\t1222\Debug>t1222 c:\sen\t1222\Debug>echo %errorlevel% 11

5 The cout Object The cout object represents the standard output display device, so anything you insert into cout will reflect on the standard output display device. – The display device is usually a video screen – Name derived from Console OUTput and pronounced “see out” Data is passed to cout by the insertion symbol cout << “Hello there, World!”;

6 C++ Sample Code using cout

7 Preprocessor Command Performs an action before the compiler translates source code to machine code – An example is: #include – Causes the iostream file to be inserted wherever the #include command appears iostream is part of the C++ standard library – Included in iostream are two important classes: istream : Declarations and methods for data input ostream : Declarations and methods for data output

8 Namespaces Files accessed by compiler when looking for prewritten classes or functions Sample namespace statement: – using namespace std; – iostream contained in a namespace called std – Compiler uses iostream’s cout object from std whenever cout is referenced

9 More C++ Sample Code

10 More C++ Sample Code (continued)

#include using namespace std; int main() { cout<<"hi!"; cout<<endl; cout<<3+5; cout<<endl; cout<<"3+5"; return 0; }

12 Formatted Output Read book, page 53 to 66 Besides displaying correct information, a program must present results attractively. Field width manipulators: control format of numbers displayed by cout – Manipulators included in the output stream

Output manipulators Output manipulators are items used to manipulate how the output stream of characters is displayed.

Manipulators for columnizng styles setw(n) setfill(‘c’)

When a manipulator requiring an argument is used, the iomanip header file must be included as part of the program. This is accomplished by the preprocessor command #include.

Setw(width) The setw(width) manipulator included in the stream of data passed to cout is used to set the displayed field width. The width in this manipulator should be replaced by a concrete integer number say 10. It will reserves a field with a width of 10 for any subsequent number in the stream.

Setfill(symbol) Set the default fill character to x. (The default fill character is a space) It will use the symbol to fill out the space that has not been used by the data.

Special manipulators for numbers fixed setprecision(n)

fixed Always show a decimal point and use a default of 6 digits after the decimal point. Fill with trailing zeros, if necessary.

setprecision(number) Set the floating-point precision to the places of number. If the fixed manipulator is designated, number specifies the total number of displayed digits after the decimal point; otherwise, it specifies the total number of significant digits displayed (both integer portion and fractional portion)

21 Formatted Output (continued)

22 Formatted Output (continued)

Links to several lists of basic C++ constructs A list of keywords A list of operators, with associativity of each and precedence among them. A list of operators, with associativity of each and precedence among them. A list of io flags and format manipulators A c++ reference website Literals Tokens identifiers 23