CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

Slides:



Advertisements
Similar presentations
What type of data can a variable hold?
Advertisements

This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:
Getting Data into Your Program
1 C++ string Class Chapter 6. 2 Agenda String Basics (cin, getline )  string operations mixed I/O using >> & getline() Table Output using setw() Functions.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Divisibility Rules Page 10 in textbook.
CS 117 Spring 2002 Basic Program Elements Chapter 2.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Hello World 2 What does all that mean?.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
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 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
M.T.Stanhope Oct Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 1 C++ Basics u Data types. u Variables and Constants.
C++ Basics #7 Basic Input and Output. In this video Standard output (cout) Standard input (cin) stringstream.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
My Book of Divisibility. THE 2’s Example: 30, 42, 24, 76, 98, Must be an even number Number must end in a 0, 2, 4, 6, or 8.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
1 Structure of Simple C++ Program Chapter 1 09/09/13.
arithmetic operator & cin I.Mona Alshehri The output formatting functions setw(width) setw(n) - output the value of the next expression in n columns.
C++ Lesson 1.
Basic concepts of C++ Presented by Prof. Satyajit De
Introduction to Computer Programming
TK1913 C++ Programming Basic Elements of C++.
Katherine Kampf / kkampf
Midterm Review.
while Repetition Structure
CMSC 202 Lesson 2 C++ Primer.
Chapter 2 Assignment and Interactive Input
Introduction to C++ October 2, 2017.
Hello World 2 What does all that mean?.
Chapter 2 Elementary Programming
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
הרצאה 03 אבני היסוד של תוכנית ב- C
Programming with Data Files
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
Chapter 3: Input/Output
Wednesday 09/23/13.
Introduction to C++ Programming
If Statements.
C++ Programming Lecture 3 C++ Basics – Part I
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Arithmetic Operations
Reading from and Writing to Files
Using string type variables
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
CS31 Discussion 1D Winter19: week 4
CS31 Discussion 1D Fall18: week 2
Reading from and Writing to Files
CSE Module 1 A Programming Primer
COMPUTING.
Arithmatic Logic Unit (ALU). ALU Input Data :  A0-A3  B0-B3 Output Data :  F0 – F3.
Presentation transcript:

CS31 You Lu CS31-1K TA

Hello World!

Variable

Out of the Range You input , but it outputs another different number.

I/O cin >> cout <<

Input one word or string cin>>s; //can only input one word, e.g., only my first name. getline(cin, s); //can input the whole string, e.g., my first and last name

cin.ignore(10000, '\n') Use cin.ignore(10000, '\n') after reading a number if the next input will be of a string (using getline)

Arithmatic Division: 5/2=2; 5.0/2=2.5 – as long as one of the number is a “double” we have a “double” division. Mod: 5%2=1 (remainder) Casting: – int a = 2.5 a = ? 2 – (double)5/2 = ? 2.5 – (double) (5/2) = ? 2

Visual Studio 2010 Check the output of compiler to see if there is any warning or error every time.

iostream precision Default is 6 digits If “double a = ”; “cout << a” will only display Here you can use “cout.precision(9)” to set the output as 9 digits. See the example below.

iostream precision Be more careful to use “cout”

header file #include //standard built-in libraries #include “ccc_time.h” //user-defined libraries

if … if (condition) statement; condition: non-zero value means true; zero value means false;

another example

if…else… if (expression) statement; else statement; Nested if statement: if() { if() {…} else {…} } else if() { } else { }

Boolean expression Greater than or equal: “>=“ Less than or equal: “<=” Equal: “==” Not equal: “!=” Logic expression: – AND: && – OR: || – NOT: !

priority Arithmetic operator > relational operator 10 > 1+2 ↔ 10 > (1+2) “ logic operator cout << “true AND false is ” << (true && false);

priority highest! > >= < <= == != && lowest||

while loop while (expression) { statement; } do{ statement; } while (expression)

example

for loop for (initialization; condition; increment) { statement; } infinite loop: for(;;) { }

“continue” skip the current round loop, go to the next round loop for(…) { code 1; code 2; continue; code 3; }

example

“break” stop the current loop and get out of the loop. for (…) { code 1; code 2; break; code 3; } code 4

example

“goto” goto and “lable” must be in the same function. lable: goto [lable];

another example Jump out from a deep nested loop for(…) { for(…) { while(…) { if(…) goto stop; … } stop: cout << “Error in program. \n”; //”break” can only jump out from only one loop

Online reference Some websites are really helpful for you to learn C++, like More detailed reference: Microsoft MSDN library – us/vstudio/hh us/vstudio/hh – us/library/cscc687y(v=vs.80).aspx us/library/cscc687y(v=vs.80).aspx – us/library/3bstk3k5.aspx us/library/3bstk3k5.aspx

Look up “rand()”

My webpage for CS31 1K