Basic C Input Expressions The contents of this

Basic C++ Input Expressions The contents of this lecture prepared by the instructors at the University of Manitoba in Canada and modified by Dr. Ahmad Reza Hadaegh A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 1

Basic C++ Input - Computers often require input from the program user - interactively (such as query to the user) from a file - In C++, we get input in a manner similar to print output A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 2

Basic C++ Input #include<iostream> using namespace std; int main() { float x; cout<<“Enter x ”; cin>>x; cout<<“The square of x is ”<<x*x <<endl; } A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 3

Basic C++ Input - cin is the standard input stream - like cout, it is defined in iostream - Input to a variable (like x) is accomplished by the extraction operator >> - Basically, we think of the insertion (<<) and extraction (>>) operators as pointing in the direction that the data is flowing - Input is flowing from cin to x A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 4

Basic C++ Input - Stream input reads the next item(s) of the desired type from the input stream. For example, float x, y; cin>>x; //reads one float into x cin>>x>>y; //reads a float into x and another float into y reads floats from the standard input stream - It does nothing if the next input is not a float, or if something else goes wrong (like an “end-of-life”) - More on this later. . . A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 5

Basic C++ Input - C++ input ignores whitespace - It reads the next item on the input stream, skipping any leading whitespace - If the C++ statements char onechar; float onefloat; int oneint; cin>>onefloat>>oneint; cin>>onechar; are executed in input 3. 14 25 x - the variables have values onefloat = 3. 14 oneint = 25 A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) onechar = ‘x’ Page 6

The type bool - Boolean data & variables can have two possible values: true & false - Such variables are typically used to represent the state of the computation at a particular time. For example, - If we are looking for something in a database, we might have a boolean variable “found” which is false until we find what we are looking for A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 7

The type bool - In C++, Boolean variables are of type bool and have possible values as true or false - bool is a type - true & false are literals int main() { bool a, b; a = true; b = false; cout<<“True is”<<a<<endl; cout<<“False is”<<b<<endl; } A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 8

The type bool - The output is: True is 1 False is 0 - Note that by default, true & false are represented by the integers 0 and 1 - In general, false is represented by 0 and true is represented by any non-zero integer - This is an example of a coercion - You generally don’t need to think about this coercion A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 9

The type bool - We can also do I/0 on type bool with “English” names use the “boolalpha” manipulator. For example, int main() { bool a, b; a = true; b = false; cout<<boolalpha; //insert manipulator cout<<“True is”<<a<<endl; cout<<“False is”<<b<<endl; } - The output is: True is true False is false A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 10

Boolean Expressions - bool values are generally obtained from comparing variables of other types: - For example: float x, y; bool c; c = (x<=y) // c is “true” if x is less than or equal to y. // c is “false” otherwise A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 11

Boolean Expressions - There a number of basic comparisons that C++ defines between 2 variables or constants: == != > < >= <= A. R. Hadaegh Dr. Ahmad R. Hadaegh Equal to (note: different from =) Not equal to Greater than Less than Greater than or Equal to Less than or Equal to California State University San Marcos (CSUSM) Page 12

Boolean Expressions - For type int, the result of such comparisons is relatively clear - For type float, be careful of round off: 1. 33333333 = = (4. 0/3. 0) // ? ? - For type char, order consistent with alphabet of same case ‘a’<’z’ ‘A’<=‘B’ ‘a’<‘B’ A. R. Hadaegh Dr. Ahmad R. Hadaegh true false (!!) California State University San Marcos (CSUSM) Page 13

Logical Operators - In mathematical logic, we combine logical expressions with AND, OR, and NOT to form more complex expressions - C++ offers these operators as well: AND is represented by && in C++ OR is represented by || in C++ NOT is represented by ! in C++ A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 14

Logical Operators - For bool variables a, b: a && b true only when a and b are both true, false otherwise a || b true when one or more of a and b are true, false otherwise !a true when a is false, false when a is true A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 15

Logical Operators #include <iostream> using namespace std; int main() { float x, y, z; bool c; cout<<“Enter x, y, z: ”; cin>>x>>y>>z; c = (x<=y) && (y<=z); cout<<"It is " << boolalpha<<c <<" that x, y, and z are in ascending order? " << endl; } < See: Example 1> A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 16

Logical Operators #include <iostream> using namespace std; int main() { float x, y, z; bool c; cout<<“Enter x, y, z”; cin>>x>>y>>z; c=((x<=y) && (y<=z)) || ((x>=y) && (y>=z)) cout<<“It is ” << boolalpha << c << “ that x, y, and z are in ascending or descending order? ” << endl; } A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 17

C++ Input from a File - we can obtain C++ input from a file rather than getting it from the screen: #include<iostream> #include<fstream> using namespace std; //input or output stream declarations int main() { float x; ifstream fin; //input stream named fin. open(“myinputfile. txt”); //attach to file “myinputfile” fin>>x; //get x from input file cout<<“The input is”<<x<<endl; } < See: Example 2>California State University San Marcos (CSUSM) A. R. Hadaegh Dr. Ahmad R. Hadaegh Page 18

C++ Output from a File - we can also put C++ output into a file rather than showing it on the screen: #include<iostream> #include<fstream> //input/output stream declarations using namespace std; int main() { ofstream fout; //output stream named fout. open(“myoutputfile”); //open a file called myoutputfile or // create the file if it does not exit fout <<“------------------------” << endl; fout<< “Name : Joe Anderson” << endl; fout << “Assignment #: 1 ” << endl; fout << “ Date: Oct 20, 2002 ” << endl; fout <<“------------------------” << endl; } < See: Example 3> A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 19
- Slides: 19