Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019

Similar presentations


Presentation on theme: "EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019"— Presentation transcript:

1 EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lecture 2: C++ program structure Basic I/O in C++

2 Data Structures: Lecture 2
Lecture outline Announcements/reminders Program 1 due date TBD (within 2 weeks or so … probably less) Today’s lecture Basic C++ program structure I/O in C++ 5/22/2019 Data Structures: Lecture 2

3 Data Structures: Lecture 2
Hello World! in C++ #include <iostream> // C++ input/output library using namespace std; int main() { cout << "Hello World!\n"; return 0; } 5/22/2019 Data Structures: Lecture 2

4 Namespaces; using Directive
The using directive instructs the compiler to use files defined within a specific namespace Namespaces allow us to declare different scopes Typically written right after the relevant header file(s) Example: using namespace std; std is the name of the Standard C++ namespace Including this line avoids listing namespace for every identifier in headers … … but allows everything in the std namespace Compromise: list namespace members actually used using std::cout; Otherwise, you’d have to write “std::cout” every time Which you prefer is matter of style 5/22/2019 Data Structures: Lecture 2

5 Data Structures: Lecture 2
Review: C I/O basics Output: printf() Prints most characters exactly as shown in quotes To print variables, provide format specifiers + comma-separated list of variables Format specifiers provide type, formatting We covered precision; also field width & extra characters Example: printf("x = %.2lf\n", x); Input: scanf() Arguments: string with format specifiers, address list Reading numbers skips whitespace Reading chars skips whitespace if space before %c Example: scanf("%d %c%d”, &v1, &c1, &v2); 5/22/2019 Data Structures: Lecture 2

6 C++ I/O basics: I/O streams
C++ has three standard input/output streams cin is the standard input (e.g., keyboard) cout is the standard output cerr is the standard error 5/22/2019 Data Structures: Lecture 2

7 C++ I/O basics: Standard output
Insertion operator << directs data to cout General Form: cout << expr << expr; expr can be any C++ constant, identifier, formula, or function call endl: newline (like'\n'); also flushes buffer Forces output to be printed immediately Example: cout << "x = " << x << endl; No need to specify type for x—simply insert in output 5/22/2019 Data Structures: Lecture 2

8 Modified program: two cout statements
#include <iostream> using std::cout; // Only include part // of std namespace // you actually use int main() { // display message cout << "Welcome "; cout << "to C++!\n"; return 0; } 5/22/2019 Data Structures: Lecture 2

9 Modified program: multiple output lines
#include <iostream> using std::cout; int main() { cout << "Welcome\nto\n\nC++!\n"; return 0; } 5/22/2019 Data Structures: Lecture 2

10 C++ I/O basics: standard input
Extraction operator >> to direct keyboard input to variables General Form: cin >> identifier >> identifier; Input value must be compatible with identifier type Extraction operator always ignores leading whitespace 5/22/2019 Data Structures: Lecture 2

11 C++ input/output example
#include <iostream> using std::cout; using std::cin; using std::endl; int main() { int number1, number2; // Input variables // prompt user for data and read into appropriate variables cout << "Enter first integer: "; cin >> number1; cout << "Enter second integer: "; cin >> number2; cout << "Sum is " << number1 + number2 << endl; // display sum; end line return 0; } 5/22/2019 Data Structures: Lecture 2

12 Example 1: find output #include <iostream> using std::cout; using std::cin; using std::endl; int main() { int i, j; double x; cin >> i >> j; cin >> x; cout << "output \n"; cout << i << ',' << j << endl << x << "cm" << endl; return 0; } Assume following input: 5/22/2019 ECE 264: Lecture 4

13 Example 2: find output #include <iostream> using std::cout; using std::cin; using std::endl; int main() { int i, j; double x, y; cin >> i >> j >> x >> y; cout << "First output " << endl; cout << i << ',' << j << ',' << x << ',' << y << endl; cin >> x >> y >> i >> j; cout << "Second output" << endl; return 0; } Assume following input: 1 2 3.4 5 5/22/2019 ECE 264: Lecture 4

14 Data Structures: Lecture 2
Solutions Example 1 (note lack of spaces): output 1,2 4.5cm Example 2: First output 1,2,3.4,5 Second output 3,?,2,3 In VS, j unchanged  ? = 2 In Xcode, j set to 0  ? = 0 Key point: make sure types match 5/22/2019 Data Structures: Lecture 2

15 Data Structures: Lecture 2
Final notes Next time: more on going from C to C++ Structures in C++ Reminders: Sign up for the course discussion group on Piazza! Program 1 due date TBD (within 2 weeks or so) All programs to be submitted via Dropbox Dr. Geiger for access to shared Dropbox folder 5/22/2019 Data Structures: Lecture 2


Download ppt "EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019"

Similar presentations


Ads by Google