Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2.
Advertisements

 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Chapter 2: Introduction to C++.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
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.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Chapter 3 Getting Started with C++
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
Input & Output: Console
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.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts 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++
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
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
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
CSCI 130 Basic Input and Output Chapter 9. The printf ( ) function Printf(“\nThe value of x is %d”, x); Displayed to screen (assume x = 12): –The value.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Lecture 3: Getting Started & Input / Output (I/O)
Bill Tucker Austin Community College COSC 1315
C++ First Steps.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Topic Pre-processor cout To output a message.
Chapter 1.2 Introduction to C++ Programming
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
CSC201: Computer Programming
Chapter 2, Part I Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Basic Elements of C++.
Command Line Arguments
Chapter 2 Overview of C.
Getting Started with C.
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2: Introduction to C++.
Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)

Overview Getting Started  Semi-colons, curly-braces, whitespace  Comments  #include's  Main program Input / Output A little more detail on the compiling process

Nearly every line of C/C++ code ends with a semi- colon  Frustrating at first, second-nature later... White-space is generally ignored “Containing” is indicated by pairs of curly-braces  No requirements they appear on different lines int i=5; // N ote – this line has a blank line (whitespace) i++; while (i < 10)// Note: no semi-colon here { cout << i << endl; } int i=5; // N ote – this line has a blank line (whitespace) i++; while (i < 10)// Note: no semi-colon here { cout << i << endl; }

A way to add descriptive text to a source file  More readable To you To other people that have to look at it  Documentation What a section of code does, and how Doesn't affect the size of the executable  (although the size of the source files is increased)

Comments, cont. Syntax:  Complete single-line  End-of-line  Multi-line (or in-line) Bold black text is actual C/C++ code // This is a single line comment x = 5; // This is an end-of-line comment /* This is a multi- line Comment */ // The 10 below is an in-line comment for (int i=0; i < /*10*/ 5; i++) { } // This is a single line comment x = 5; // This is an end-of-line comment /* This is a multi- line Comment */ // The 10 below is an in-line comment for (int i=0; i < /*10*/ 5; i++) { }

A technique to include a collection (library) of pre- defined symbols  Functions  Classes  Constants …… Just a (text) file with a collection of C/C++ code  [Look in $CodeBlocks\MinGW\lib\gcc\mingw32\4.7.1\includ e\c++\iostream] For now, just “built-in” collections  Later: our own collections, downloaded libraries  Later: more precise definition of what they really are...

#include, cont. Syntax: Put at the top of a.c or.cpp file. #include is part of the Pre-processor sub-language of C/C++  [Note the # at the beginning] #include // A standard include file #include “my_file.h”// A “local” include file (later) #include // A standard include file #include “my_file.h”// A “local” include file (later)

#include, cont. How it works:  #include textually copy/pastes the content of the referenced file in-place of the #include directive  This happens before the compiler even starts. Example:  [Local include file, with a const variable – show what compiler sees]

Where the program starts  In a.c or.cpp file  Can only appear once in a project For console app's, 4 types:  void return type (C++ only)  int return type (C/C++)  With command-line arguments  without command-line arguments Windows, OSX, etc. app's might have a different form...

Main Program, cont. void main()// C++ only { /* Program code goes here... */ } void main()// C++ only { /* Program code goes here... */ } int main() { /* Program code goes here... */ return 0; // Indicates everything ran OK // Return non-zero to indicate errors. } int main() { /* Program code goes here... */ return 0; // Indicates everything ran OK // Return non-zero to indicate errors. } void main(int argc, char ** argv)// C++ only { /* Program code goes here... */ } void main(int argc, char ** argv)// C++ only { /* Program code goes here... */ } int main(int argc, char ** argv) { /* Program code goes here... */ return 0; } int main(int argc, char ** argv) { /* Program code goes here... */ return 0; }

A way to input or output data from our program  Input Keyboard [Mouse another day, hopefully]  Output Command-line output (screen) (As will become the norm) we'll look at this in both C and C++  Most of the time they'll be the same...

Output to command-line (C++) Uses the header (collection)  Uses namespaces (we'll look at these later...)  For now, just put “using namespace std;” below the #include's Uses the pre-defined cout object  By default, the screen (can be “remapped”)  A stream Write data elements to it using the chevron operator << Conversions are applied as necessary (automatically)

Output to command-line (C++), cont. #include using namespace std; void main() { cout << “Hello, World!”; cout << “A (fake) second line.”; cout << endl << “The real second line!” << endl; cout << “An integer: “ << 5 << “, A double: “ << << “, A string: “ << “'Hi!'” << endl; } #include using namespace std; void main() { cout << “Hello, World!”; cout << “A (fake) second line.”; cout << endl << “The real second line!” << endl; cout << “An integer: “ << 5 << “, A double: “ << << “, A string: “ << “'Hi!'” << endl; }

Escape sequences A way to put a “hard-to-type” character in a string Start with a backslash and then a 1-character code indicating which escape sequence to use. \n// New-line \t// Tab \\// An actual back-slash \”// A quote cout << << endl; Output: ****

Output to command-line (C) Also defaults to the screen. You need to include...  if compiling with a C++ compiler  if compiling with either a C or a C++ compiler Done with the printf function. Uses a “format string” followed by 0 or more values %d// An integer %f// A float or double %s// A string // There are more – these are just the basics

Output to command line (C), cont. #include int main() { printf(“Hello, World!”); printf(“A (fake) second line.”); printf(“\nThe real second line!\n”); printf(“An integer: %d, A double: %f, A string: %s\n”, 5, , “'Hi!'”); return 0; } #include int main() { printf(“Hello, World!”); printf(“A (fake) second line.”); printf(“\nThe real second line!\n”); printf(“An integer: %d, A double: %f, A string: %s\n”, 5, , “'Hi!'”); return 0; }

Input from the command line (C++) By default, reads keyboard input Also included by  Don't forget the “using namespace std;” line Uses the pre-made cin object In the example, I'll use variables  We'll discuss these in detail in the next section Also uses the chevron operator, but this time they go the other way (>>)  I think of this as pointing from cin to the variables

Input from the command-line (C++), cont. #include using namespace std; int main() { int myInt; float myFloat; string myString; cout << "Enter an integer, then press enter: "; cin >> myInt; cout << "Enter a float and a string separated by a "; cout << "space, then press enter: "; cin >> myFloat >> myString; cout << "Your int was: " << myInt << endl; cout << "Your float was: " << myFloat << endl; cout << "Your string was: '" << myString << "'" << endl; return 0; } #include using namespace std; int main() { int myInt; float myFloat; string myString; cout << "Enter an integer, then press enter: "; cin >> myInt; cout << "Enter a float and a string separated by a "; cout << "space, then press enter: "; cin >> myFloat >> myString; cout << "Your int was: " << myInt << endl; cout << "Your float was: " << myFloat << endl; cout << "Your string was: '" << myString << "'" << endl; return 0; }

Input from the command-line (C) Also defaults to the keyboard. You need to include...(just like printf)  if compiling with a C++ compiler  if compiling with either a C or a C++ compiler Done with the scanf function. Uses a “format string” like printf.  However, you pass the address of variables in which you wish to store the typed value.

Input from command-line (C), cont. #include int main() { int myInt; float myFloat; char myString[256]; printf("Enter an integer, then press enter: "); scanf("%d", &myInt); printf("Enter a float and a string separated by a "); printf("space, then press enter: "); scanf("%f %s", &myFloat, myString); printf("Your int was: %d\n", myInt); printf("Your float was: %f\n", myFloat); printf("Your string was: '%s'\n", myString); return 0; } #include int main() { int myInt; float myFloat; char myString[256]; printf("Enter an integer, then press enter: "); scanf("%d", &myInt); printf("Enter a float and a string separated by a "); printf("space, then press enter: "); scanf("%f %s", &myFloat, myString); printf("Your int was: %d\n", myInt); printf("Your float was: %f\n", myFloat); printf("Your string was: '%s'\n", myString); return 0; }

Input / Output to (Text and Binary) Files Very similar syntax to command-line input/output We'll look at this shortly  After we've looked at variables