ITEC 320 C++ Examples.

Slides:



Advertisements
Similar presentations
Factorial Preparatory Exercise #include using namespace std; double fact(double); int fact(int); int main(void) { const int n=20; ofstream my_file("results.txt");
Advertisements

Strings.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
CMSC 2021 C++ I/O and Other Topics. CMSC 2022 Using C++ Stream I/O Default input stream is called cin Default output stream is called cout Use the extraction.
1 DOS vs. UNIX files Ending lines with “\r\n” vs. “\n” Reading an entire line at a time getline() To skip white space or not cin >> ch; vs. ch = cin.get();
Computer Skills2 for Scientific Colleges 1 File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
File I/O. COMP104 File I/O / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
Command-line arguments CS 201 Fundamental Structures of Computer Science.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Multiple-Subscripted Array
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between kloc.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
 CS105 C++ Lecture 2 Arrays. Parameter passing  2 types  Pass-by-value  Pass-by-reference 2.
CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Week 12 Labs 4 and 5 are back File IO Looking ahead to the final.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
Lin Chen 09/06/2011. Global variables const int maxCandidates = 10; // Names of the candidates participating in this state's primary string candidate[maxCandidates];
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
1 Character Strings (Cstrings) Reference: CS215 textbook pages
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
System Programming Practical Session 7 C++ Memory Handling.
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
CSCI 333 Data Structures I/O with C++ 20 October, 2003.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Lecture 3: Getting Started & Input / Output (I/O)
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Strings (part 2) Dr. Xiao Qin Auburn.
Characters and Strings
Command Line Arguments
Multi-dimensional Array
Learning Objectives String Class.
Pointers & Functions.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
CS150 Introduction to Computer Science 1
Arrays Arrays A few types Structures of related data items
Reading from and Writing to Files
Pointers and dynamic objects
Pointers & Functions.
Class: Special Topics Overloading (methods) Copy Constructors
Characters and Strings
Presentation transcript:

ITEC 320 C++ Examples

Hello World #include <iostream> using namespace std; void main(int argc, char** argv) { cout << “Hello World” << endl; }

Variables #include <iostream> #include <string> using namespace std; void main(int argc, char** argv) { int x; double y; char z; string a; }

Loops #include <iostream> #include <string> using namespace std; void main(int argc, char** argv) { int i=0; while(i<10) cout << i << endl; i++; } for (int j=0; j<10; j++) cout << j << endl;

Functions #include <iostream> #include <string> using namespace std; void print(); void main(int argc, char** argv) { print(); } void print() cout << “Hello World” << endl;

Parameter passing #include <iostream> #include <string> using namespace std; void print(int& x, const int& y, const z); void main(int argc, char** argv) { int x=3; int y=4; int z=5; print(x,y,z); cout << x << y << z << endl; } void print(int& x, const int& y, const z) x=4; //X’s value in main is changed, Y/Z cannot be changed //A copy of Z is made but y is not copied

Multiple files Compile with: g++ -o prog main.cpp print.cpp header.h #ifndef __header_h_ #define __header_h_ void print(); #endif Compile with: g++ -o prog main.cpp print.cpp Run: prog or ./prog (windows / unix) header.h #include <iostream> #include <string> using namespace std; #include “header.h” void main(int argc, char** argv) { print(); } #include <iostream> #include <string> using namespace std; #include “header.h” void print() { cout << “Hello World” << endl; } main.cpp print.cpp

Pointers #include <iostream> #include <string> using namespace std; void main(int argc, char** argv) { int* p = NULL; p = new int; *p = 3; cout << *p << endl; }

Arrays #include <iostream> #include <string> using namespace std; void main(int argc, char** argv) { int first[10]; int* p = new int[10]; for (int i=0; i<10; i++) first[i] = i; p[i] = i; } delete [] p;

Input #include <iostream> #include <string> using namespace std; void main(int argc, char** argv) { int x; string line; cin >> x; //Read an integer, ignores preceding whitespace getline(cin, line); //Reads until end of line getline(cin, line, ‘\t’); //Reads up until the tab cin.ignore(200,’\n’); // Ignore 200 chars or a new line //whichever comes first }

Input / Looping #include <iostream> #include <string> using namespace std; void main(int argc, char** argv) { int x; cin >> x; //Read an integer, ignores preceding whitespace while (!cin.eof() && !cin.fail()) if (x == 5) cout << “Magic number” << endl; cin >> x; } Prime the input before the loop Otherwise it will repeat 1 more Time than it should

File I/O #include <fstream> #include <string> using namespace std; void main(int argc, char** argv) { ifstream i(“file.txt”); ofstream o(“output.txt”); int x; i >> x; o << x << endl; i.close(); o.close(); }