Jeff West - Quiz Section 6

Slides:



Advertisements
Similar presentations
True or false A variable of type char can hold the value 301. ( F )
Advertisements

Conditional Operator (?:) Conditional operator (?:) takes three arguments (ternary) Syntax for using the conditional operator:
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Writing and Testing Programs Drivers and Stubs Supplement to text.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Chapter 6 Control Structures.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
1 Lecture 19 Structs HW 5 has been posted. 2 C++ structs l Syntax:Example: l Think of a struct as a way to combine heterogeneous data values together.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
C++ Classes and Data Structures Jeffrey S. Childs
1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Switch Statements Comparing Exact Values
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
1 C++ Classes and Data Structures Course link…..
Chapter 4 Repetition Structures
Chapter 1.2 Introduction to C++ Programming
Review 1.
Chapter 1.2 Introduction to C++ Programming
Chapter 3 Control Statements
Jeff West - Quiz Section 2
Chapter 1.2 Introduction to C++ Programming
Basics (Variables, Assignments, I/O)
REPETITION CONTROL STRUCTURE
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Chapter 1.2 Introduction to C++ Programming
Objectives In this chapter, you will:
Engineering Problem Solving with C++, Etter/Ingber
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Multi-dimensional Array
Monday, February 26, 2018 Announcements… For Today…
לולאות קרן כליף.
User-defined Functions
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
Basics (Variables, Assignments, I/O)
One-Dimensional Array Introduction Lesson xx
Announcements General rules about homeworks
Announcements General rules about homeworks
User-defined Functions
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Control Structures Part 3
Looping III (do … while statement)
Announcements Homework 1 will be assigned this week,
Jeff West - Quiz Section 4
Jeff West - Quiz Section 8
Jeff West - Quiz Section 9
Programming Introduction to C++.
Statements and flow control
Objectives You should be able to describe: The while Statement
Reading from and Writing to Files
Announcements General rules about homeworks
Using string type variables
(Dreaded) Quiz 2 Next Monday.
Reading from and Writing to Files
CSE Module 1 A Programming Primer
Jeff West - Quiz Section 3
Presentation transcript:

Jeff West - Quiz Section 6 CSE 143 Section AD Quiz Section 6 7/5/2001 Jeff West - Quiz Section 6

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

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

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

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

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

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;

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

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

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

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

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