150 likes | 163 Views
This quiz covers topics such as while, for, and do-while loops, switch/case statements, strings, and file handling in C++. It also includes examples and explanations of each concept.
E N D
Quiz • Thursday • Everything through Last Week and Lecture 7 : • while, for, and do-while loops • switch/case
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'; cout << word << 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 << word << endl; 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 << word << endl; cout << "String length is: " << word.length() << endl; word = word + " " + "healthy" + " " + word; cout << word << endl; cout << "String length is: " << word.length() << endl; 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); }
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 -23 -4.55 Smith -3 -4e3 xyz #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); } Output: 8 9.3 Jon 6 14.335 Bill 0 3.567e+010 Mary -23 -4.55 Smith -3 -4000 xyz
Quiz • Thursday • Everything through Last Week abd Lab 7 : • while, for, and do-while loops • switch/case