1 / 8

CSE 143 Section AD

CSE 143 Section AD. Quiz Section 4. Homework 1. string vs. char[] – C++ strings are a lot easier to use but character arrays (C strings) seem to have a much larger library of functions to use for separating strings, etc.

Download Presentation

CSE 143 Section AD

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. CSE 143 Section AD Quiz Section 4 Jeff West - Quiz Section 4

  2. Homework 1 • string vs. char[] – C++ strings are a lot easier to use but character arrays (C strings) seem to have a much larger library of functions to use for separating strings, etc. • Whenever you make a loop in a program think about how the INITIAL and FINAL cases will operate – many people had loops that were guaranteed to run at least once when certain data might require they never be entered at all. Jeff West - Quiz Section 4

  3. Example of a Looping Program Take five minutes to create a function that will prompt a user for their name and a number of times they would like to be greeted, after which it will greet them that many times. Stop as soon as the user inputs “Cindy” as their name and 1 as the number of times they wish to be greeted. NOTE: After Cindy has asked to be greeted 1 time you should NOT greet her, you should just end the function! Jeff West - Quiz Section 4

  4. Notes About This Program • The user should be prompted for information before the loop decides whether or not it should enter the first time. • What should the loop require to be entered? To end? Jeff West - Quiz Section 4

  5. A Sample Solution #include <iostream> #include <string> using namespace std; int main() { string currentName; // stores name of current user int numTimes; // stores number of times // current user wishes to be // greeted // prompt user for information cout << "What is your name? "; cin >> currentName;

  6. cout << "How many times should I greet you? "; cin >> numTimes; while(currentName != "Cindy" || numTimes != 1) { for(int i = 0; i < numTimes; i++) cout << "Hello, " << currentName << endl; // prompt user for information cout << "What is your name? "; cin >> currentName; cout << "How many times should I greet you? "; cin >> numTimes; } cout << "Thanks for using The Greeter version 1.0."; return 0; }

  7. Classes • Classes give us a chance to create our own datatypes that do really neat things! Think of classes as nouns which describe “categories.” Examples might include: * Person * Book * Fraction * Animal * Beverage Jeff West - Quiz Section 4

  8. Class Members • The way classes do “totally cool things” is by storing and manipulating their own data! As an example, what member functions might a book class have? What data members might it have? * Which members do you think should be private? Which members should be public? * How should the data be initialized? What constructor(s) might you wish to provide? Jeff West - Quiz Section 4

More Related