Download presentation
Presentation is loading. Please wait.
1
Jeff West - Quiz Section 6
CSE 143 Section AD Quiz Section 6 7/5/2001 Jeff West - Quiz Section 6
2
Jeff West - Quiz Section 6
Announcements If you are not yet officially registered for this course, please make sure to talk to me after class There will be two debugger tutorials next week… one on Tuesday at 3:30 and the other TBA 7/5/2001 Jeff West - Quiz Section 6
3
String “find” Member Function
If you’ve #included string, one function you can take advantage of is “find”: string s; s.find(“sometext”); will return the subscript position where you can find “sometext” (in the 0, 1, … , n number system) The function will return the constant string::npos if “sometext” is no where to be found! Create a program which will keep prompting a user for a sentence and tell the user where “hi” was first found in the sentence until the user enters a sentence which does not include the word “hi”, at which point the program will exit. 7/5/2001 Jeff West - Quiz Section 6
4
Jeff West - Quiz Section 6
A possible solution… #include<iostream> #include<string> using namespace std; void main() { string userEntry; // user sentence int pos; // position of word cout << “Enter a sentence: “; cin >> userEntry; pos = userEntry.find(“hi”); while(pos != string::npos) { cout << “hi can be found at position “ << pos << “.\n”; } 7/5/2001 Jeff West - Quiz Section 6
5
How to Implement a Menu…
Switch Statements and “Interpreter” functions – Example, HW 3 from Last Quarter “Clean” typedef example… a little advanced so if you don’t understand it, don’t use it and don’t worry about it … 7/5/2001 Jeff West - Quiz Section 6
6
A Switch Statement Menu
// options available to the user in the main menu enum MainMenuOption { ReadFile, SearchTitle, ... Quit }; // show main menu and handle the user's selections, maintaining the database. int main() { bool quit = false; // set to true when the user asks to quit. while( !quit ) { // exits directly using "return" if user quits MainMenuOption option = doMainMenu(); switch( option ) { case ReadFile: ... Call other functions, etc. break; 7/5/2001 Jeff West - Quiz Section 6
7
case SearchPerformer:
... Call other functions, etc. break; case Quit: quit = true; default: // shouldn't get here (unknown option) assert( false ); } cout << "Good bye!" << endl; return 0;
8
Jeff West - Quiz Section 6
MainMenuOption() // display the main menu and allow user to select an option from it. // returns the selected option. MainMenuOption doMainMenu() { string entry; // user's entry at menu prompt while( true ) { // loop exited using "return" cout << "Please choose from the following options:" << endl; cout << "(R)ead a file into the database" << endl; … cout << "(Q)uit." << endl; cin >> entry; if( entry.length() == 1 ) { switch( entry[ 0 ] ) { case 'R': case 'r': return ReadFile; case 'Q': case 'q': return Quit; } 7/5/2001 Jeff West - Quiz Section 6
9
Jeff West - Quiz Section 6
A Typedef Menu(1)… In Header File: // first test function int func1() { return 1; } // second test function int func2() { return 2; } // third test function int func3() { return 3; } // define a "command" type to lookup function to be called typedef int(*cmd_ptr)(); 7/5/2001 Jeff West - Quiz Section 6
10
Jeff West - Quiz Section 6
A Typedef Menu(2)… In .cpp file: #include<iostream> using namespace std; #include"functions.h" int main() { int cmd; // stores the command requested by the user cmd_ptr commands[] = { func1, func2, func3 }; // array of functions to be called while(true) { cin >> cmd; cout << (*commands[cmd-1])(); } return 0; 7/5/2001 Jeff West - Quiz Section 6
11
A Typedef Menu in a Class(1)
class Functions { public: // first test function int func1() { return 1; } // second test function int func2() { return 2; } // third test function int func3() { return 3; } }; // define a "command" type to lookup function to be called typedef int(Functions::*cmd_ptr)(); 7/5/2001 Jeff West - Quiz Section 6
12
A Typdef Menu in a Class(2)
#include<iostream> using namespace std; #include"functions.h" int main() { Functions tester; // tests the function lookup table int cmd; // stores the command requested by the user cmd_ptr commands[] = { &Functions::func1, &Functions::func2, \ &Functions::func3 }; // array of functions to be called // an infinite loop that assumes it is okay to go on forever while(true) { cout << "Enter a number for me to print: "; cin >> cmd; cout << (tester.*commands[cmd-1]) () << '\n'; } return 0; 7/5/2001 Jeff West - Quiz Section 6
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.