Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

ENSC 151 – Week 3 Professor: Yuri Rodrigues, PhD Based on material from Dr. Maira Monteiro of the Seattle Pacific University, used here with permission Objectives We will be expanding concepts related to I/O, random numbers and debugging in C++ programming. For this, the followi...

ENSC 151 – Week 3 Professor: Yuri Rodrigues, PhD Based on material from Dr. Maira Monteiro of the Seattle Pacific University, used here with permission Objectives We will be expanding concepts related to I/O, random numbers and debugging in C++ programming. For this, the following key concepts are covered: Input and output operations with the string data type Manipulators to format output Random numbers Debugging logic errors cin and the Extraction Operator >> (1 of 3) Data Type of a Valid Input for a char One printable character except the blank. int An integer, possibly preceded by a + or - sign. A decimal number, possibly preceded by a + or - sign. If the actual double data input is an integer, the input is converted to a decimal number with the zero decimal part. When reading data into a char variable >> skips leading whitespace, finds and stores only the next character Reading stops after a single character To read data into an int or double variable >> skips leading whitespace, reads + or - sign (if any), reads the digits (including decimal for floating-point variables) Reading stops on whitespace or a non-digit character cin and the Extraction Operator >> (2 of 3) cin and the Extraction Operator >> (3 of 3) Strings Getting a string from input cin >> strVar; To get a string from input into a variable: The initial whitespace is automatically skipped, then the characters are extracted until the next whitespace. For example: 1) Hi there. cin >> firstStr; firstStr = “Hi” cin >> secondStr; secondStr = “there.” 2) Hello! cin >> firstStr; firstStr = “Hello!” I am happy! cin >> secondStr; secondStr = “I” For the given input, indicate what string will be put into: Quick questions cin >> firstString; cin >> secondString; 1) Oh my! firstString gets Oh secondString gets my! 2) Frank firstString gets Frank Sinatra secondString gets Sinatra 3) Oh... firstString gets Oh… secondString gets …no!...no! 4) We all firstString gets We know secondString gets all Input with getline Function Get all remaining characters on the current input line. int main() { Gets whitespace from input string univName; Stops accepting characters when it receives a newline char i.e., a \n character or user hits cout leaves the newline in the input while getline() does not skip leading whitespace. 1 cin and the ignore Function Example 1: ignore function Discards a portion of the input The syntax to use the function ignore is: intExp is an integer expression chExp is a char expression If intExp is a value m, the Example 2: statement says to ignore the next m characters or all characters until the character specified by chExp cin.ignore(); (‘\n’ removed) Quick questions 1) What does the following get into str2? cin >> str1; getline(cin,str2); A) Simon Fraser University B) Fraser University Simon Fraser University C) Fraser University (has a leading space) ENSC 151 2) What does the following get into str3? cin >> str0; cin >> str1; cin >> str2; getline(cin,str3); A) University B) (blank) C) ENSC 151 Output Formatting Formatting Output Syntax of cout when used with

Use Quizgecko on...
Browser
Browser