Quiz Next Monday
Strings Revisited string Is a Type in C++ Library <string> May Initialize or Assign with Double Quoted Values May Compare using Equality Operators Can Access Single Characters using Square Bracket Operator ([ ]) Can Use length() Object Function Can Add to String
#include <iostream> #include <string> using namespace std; int main() { string lastname, firstname; cout << "Enter last name: "; cin >> lastname; cout << "Enter first name: "; cin >> firstname; cout << "Your name is " << firstname << " " << lastname << endl; return(0); }
#include <iostream> #include <string> using namespace std; int main() { string lastname, firstname; bool instructor; cout << "Enter last name: "; cin >> lastname; cout << "Enter Firstname: "; cin >> firstname; if (lastname == "hanrath") instructor = true; else instructor = false; return(0); }
#include <iostream> #include <string> using namespace std; int main() { string word = "start"; cout << word << endl; word[0] = 'h'; word[1] = 'e'; return(0); }
#include <iostream> #include <string> using namespace std; int main() { string word = "start"; cout << word << endl; word[0] = 'h'; word[1] = 'e'; cout << "String length is: " << word.length() << endl; return(0); }
#include <iostream> #include <string> using namespace std; int main() { string word = "start"; cout << word << endl; word[0] = 'h'; word[1] = 'e'; cout << "String length is: " << word.length() << endl; word = word + " " + "healthy" + " " + word; return(0); }
More on Input Input Failure Common *MUST* Be Part of Test Plan cin Used for Input If User Enters Bad Type of Data, cin Left in failed state Must Use cin.clear() function to Start Over Must Use cin.ignore() function to Get Beyond Bad Input May Use cin as Condition in if Statement Pages 111-113, 175 in Malik
Input Failure Example #include <iostream> #include <string> using namespace std; int main() { int numEmployees; bool done = false; string garbage; while (!done) cout << "Enter number of employees: "; cin >> numEmployees; if (!cin) //if input stream in failed state cout << "Bad input, try again." << endl; cin.clear(); // changes input state to ok cin.ignore(200,'\n'); // gets beyond bad input characters } else done = true; return(0);
Know for Quiz Everything through Exam 1 while, for, and do-while Loops New string Features Input Failure
What Is Wrong? #include <iostream> #include <string.h> using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) cout >> "Hello World!" << endl; } return(0);
What Is Wrong? (Corrections) #include <iostream> #include <string> using namespace std; int main() { int i; for(i = 0; i < 3; i++) cout << "Hello World!" << endl; } return(0);
Files Data in Main Memory is “volatile” File: Place for “permanent” data storage C: drive, A: drive, Flash drive, etc. Main Memory progEx2Prob3.exe File main() int num; string firstname; Disk
Output File Streams In C++, the Programmer Can Declare Output Streams #include <iostream> #include <fstream> using namespace std; ofstream outputStream; //ofstream object int num1, num2, num3; cout << “Enter 3 Numbers for datafile: “; cin >> num1 >> num2 >> num3; outputStream.open(“datafile.txt”); outputStream << num1 << “ “; outputStream << num2 << “ “; outputStream << num3; outputStream.close();
Input File Streams In C++, the Programmer Can Declare Input Streams #include <iostream> #include <fstream> using namespace std; ifstream inputStream; //ifstream object int num1, num2, num3; inputStream.open(“datafile.txt”); inputStream >> num1 >> num2 >> num3; inputStream.close();
To Read or Write from a File Declare the File Stream Object Name Associate a File Name with the File Stream Object by Opening the File Read or Write to the File using << or >> Operators Close the File
mydata.txt file 5 8 9.3 Jon 6 14.335 Bill 0 35.67e9 Mary #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream input; int loops, integer, i; float decimal; string name; input.open("mydata.txt"); input >> loops; for(i= 0 ; i < loops; i++) input >> integer; input >> decimal; input >> name; cout << integer << " "; cout << decimal << " "; cout << name << endl; } return(0); mydata.txt file 5 8 9.3 Jon 6 14.335 Bill 0 35.67e9 Mary -23 -4.55 Smith -3 -4e3 xyz Output: 8 9.3 Jon 6 14.335 Bill 0 3.567e+010 Mary -23 -4.55 Smith -3 -4000 xyz
Functions Lines of code with a Name() Name()can be executed over and over again in the same program.