CMIS 102 input A program that can't do input of data from the user or from a file is very limited because it can do only one computation, the one based on its "built-in" constant data. To run another computation, the program itself has to be changed, recompiled, and run again. The example programs looked at so far, with the data built-in, have been somewhat artificial. You want the same program can input different sets of data each time it is run. Another word for input is "read". Doing input is very simple in C++, although there are some details that you need to be aware of so as not to make mistakes. To input from the user (more precisely, the keyboard), use cin. This example reads the integer that the user types in on the keyboard: int num; cin >> num; Unlike cout, cin can only have a variable after each of its operators (>> the "stream insertion operator", or input operator for short). //cout: strings, variables, characters, manipulators, expressions. cout << "hello " << num << 'X' << num/(2*y) << endl; //cin: variables only cin >> num >> y; The direction of the arrows is supposed to be intuitive: cout << "to" the output cin >> "from" the input Getting it wrong is a common syntax error. A couple of things to remember: cin skips over any blanks, tabs and newlines (these are the "whitespace" characters). The user should enter the correct type of data. The only real error is if the user tries to enter letters or punctuation to an int or float variable. int num; cin >> num; //if user types hello, program probably crashes. We won't worry about how to deal with this situation, as a real program would have to. It's some messy details that isn't covered in the book because it's not directly relevant to the big picture of learning how to create algorithms and programs. Understand that when inputting into a char variable, the next non-whitepace character is read into the variable: char ch; cin >> ch; //if user types 6, then value of ch is character '6', //not int 6 C++ uses the idea of a "newline" character to represent the end of a line. A newline character is input by the user upon every Enter key press. It's also output by the endl. When you need to input the very next character the user enters, or are inputting every character the user inputs, and thus don't want the automatic skip over whitespace, use the get function: char ch; cin.get(ch); //special syntax, you aren't expected to understand it now! You usually want to "prompt" the user to indicate what input is expected; the user won't know what to input otherwise. int num_quinces; cout << "Enter the number of quinces you'd like to buy: "; cin >> num_quinces; cout << "Thank you for your order of " << num_quinces << " quinces." << endl; FILE I/O When there's a lot of data to be input and/or output, it'll often be from/to a file. A file can store data permanently, whereas the data in a running program only exists while the program is running. Conceptually, file I/O is the same as user I/O (cin and cin). All I/O in C++ is "stream" I/O, i.e. the data is a stream of characters. The system converts digit characters that the user types into an int or float when inputting into an int or float variable, whether from cin or from a file. Similarly, an int or float value is translated into digit characters when outputting. There are a few steps to using files in a C++ program. 1. #include 2. Declare a variable of special type ifstream or ofstream: ifstream inf; //for input ofstream outf; //for output 3. Prepare the file for inputting or outputting by "opening" it: inf.open("mydata.txt"); //an input file outf.open("output1"); //output file because outf is ofstream type These are the disk file names. The names are relative to the running program's working directory. You can also give full pathnames: inf.open("a:\\cmis102\\inmpg.dat"); // \\ means one backslash Extension is irrelevant to C. All these files are ASCII text files. It's an error to try to input from a non-existent file. We'll learn how to deal with that later. A file opened for output is recreated if it already exists, meaning its current contents are wiped out. Be careful. 4. Read or write the file. Use >> and << EXACTLY like with cin and cout: inf >> num >> y; //the file has to have correct data in it outf << "hello" << num << endl; Input Failure will make you aware that when the input process has messed up, you won't necessarily be informed by the machine. Some IDEs tend to make you aware by crashing, but the official definition of C++ allows the program to continue on its merry way oblivious of the input being wrong. Wrong here means if impossible-to-convert-to-number characters have been entered by the user into an int or float variable, or if a non-existent file has been opened for input.