1 / 15

Quiz

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.

Download Presentation

Quiz

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Quiz • Thursday • Everything through Last Week and Lecture 7 : • while, for, and do-while loops • switch/case

  2. 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

  3. #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); }

  4. #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); }

  5. #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); }

  6. #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); }

  7. #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); }

  8. 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

  9. 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); }

  10. 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

  11. 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();

  12. 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();

  13. 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

  14. 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

  15. Quiz • Thursday • Everything through Last Week abd Lab 7 : • while, for, and do-while loops • switch/case

More Related