Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 5: I/O and Strings #1 2000/01Scientific Computing in OOCourse code 3C59 Module 5: I/O and STRINGS In this module we will cover The C++ input and.

Similar presentations


Presentation on theme: "Module 5: I/O and Strings #1 2000/01Scientific Computing in OOCourse code 3C59 Module 5: I/O and STRINGS In this module we will cover The C++ input and."— Presentation transcript:

1 Module 5: I/O and Strings #1 2000/01Scientific Computing in OOCourse code 3C59 Module 5: I/O and STRINGS In this module we will cover The C++ input and output streams cin, cout, cerr C++ file handling The C++ string class

2 Module 5: I/O and Strings #2 2000/01Scientific Computing in OOCourse code 3C59 Aims of this module You do not get far with any program without the need to either inputto and/or output something from the program. This is particularly true in scientific analysis where the very minimum is likely to be a file of data points to analyse. This module covers the things you need to know to deal with user and file I/O In several precediing modules we have used "hands on examples" cout > to write things to the screen and read in from the keyboard. In this module the aim is to cover these more formally. We then show you how to get things from files. Finally, and a little out of place, we introduce the string class which provides a convenient way to deal with text strings. We do this here as it naturally fits with I/O.

3 Module 5: I/O and Strings #3 2000/01Scientific Computing in OOCourse code 3C59 5.5 STRINGS Old style C character handling (which we havnt covered) is awkward to use. By constrast, the C++ string class lets you handle text strings in a fairly intuitive way. These are easier to use and provide many more things for you #include // declare a string // it is empty string a; // declare a second string // but use the constructor that takes // a string of characters in quotes // like this: string b(“Hello World!”);

4 Module 5: I/O and Strings #4 2000/01Scientific Computing in OOCourse code 3C59 13.1 STRINGS You can set a string with the assign ( ) method #include //declare a string string a(“Hello World!”); //declare a second string //it starts off empty... string b; b.assign(a); // b is now // “Hello World!” b.assign(“Farewell!”); // b is now // “Farewell!”

5 Module 5: I/O and Strings #5 2000/01Scientific Computing in OOCourse code 3C59 13.1 STRINGS You can concatenate (add one string to the end of another) string objects with the append( ) method #include //declare a string string a(“Hello World!”); //declare a second string //it starts off empty... string b(“ And Goodbye!”); a.append(b); // a is now // “Hello World! And Goodbye!”

6 Module 5: I/O and Strings #6 2000/01Scientific Computing in OOCourse code 3C59 13.1 SOME MORE METHODS OF STRING #include string a(“Hello World!”); string b(“Goodbye cruel world!”); //you can find out how long they are int lengthOfa = a.size(); //you can access characters inside bool containsH = false; for (int i = 0; i < lengthOfa; i++){ if (a.at(i) == ‘H’) { containsH = true; } As always C++ counts from 0 to n-1 You can find out how long the string is with size( ) You can find out what character is at a certain position in the string with at( ) - this returns a C char Note single quotes for a C char

7 Module 5: I/O and Strings #7 2000/01Scientific Computing in OOCourse code 3C59 13.1 COMPARISONS OF STRINGS #include string a(“Hello World!”); string b(“Hello World!”); int test = a.compare(b); if (test == 0) { // string a was equal to string b... } You can use the compare( ) method on strings to see if one string is less than, equal to, or greater than another The compare( ) method returns an int: negative if you are less than this string I’ve given you 0 if you are equal to this string I’ve given you positive if you are greater than this string I’ve given you

8 Module 5: I/O and Strings #8 2000/01Scientific Computing in OOCourse code 3C59 13.1 COMPARISONS OF STRINGS #include string author1(“Clarke”); string author2(“Phillips”); string firstNameOnPaper; int test = author1.compare(author2); if (test < 0) { firstNameOnPaper = author1; } else { firstNameOnPaper = author2; } //sadly Clarke gets to be first Beware - there is a subtlety about what “greater than”, “less than” and “equal to” mean You might think the order would be “in alphabetical order”. But is “a” equal to “A”? And is “1” greater than “a”? The computer codes each symbol as an integer number (most use a system called ASCII to translate from numbers to symbols) ‘A’ and ‘a’ are different symbols and “Alpha” might be greater than or less than “alpha” on different systems So you can use compare to put things in alphabetical order but be a bit careful

9 Module 5: I/O and Strings #9 2000/01Scientific Computing in OOCourse code 3C59 A NOTE TO THE CLEVER PEOPLE WAY AHEAD OF THE REST OF US You may feel that using myString.assign(anotherString); and having to use an integer like int test = author1.compare(author2); is a bit painful when what we really want to say is myString = anotherString; if (author1 < author2) {... } There is a way to do that in C++ It is called operator overloading and we haven’t covered it yet

10 Module 5: I/O and Strings #10 2000/01Scientific Computing in OOCourse code 3C59 13.4 A C++ STUPIDITY #include string filename(“Degrees.dat”); ofstream myOutputFile(filename); If any sane person had written the C++ standard, this code would work: But it doesn’t, I’m afraid Instead you have to use the method of string called c_str to give you the C-style representation of the string before it will work: #include string filename(“Degrees.dat”); ofstream myOutputFile(filename.c_str());

11 Module 5: I/O and Strings #11 2000/01Scientific Computing in OOCourse code 3C59 Student exercise Sometime ago you wrote a little program that sorted numbers We’re going to write a class called GuestList that holds a list of guests’ names for us and can print them out in alphabetical order We will use the same algorithm as our number sorting program Copy my main program- it sets things up to read in the names from a file Copy my guestlst.hand guestlst.cxx files Implement the method void GuestList::sortIntoAlphabeticalOrder( ) that takes the array of strings held by the object and sorts them into alphabetical order main.cpp guestlst.hpp guestlst.cpp

12 Module 5: I/O and Strings #12 2000/01Scientific Computing in OOCourse code 3C59 Summary of Module 13: STRINGS AND I/O In this module we have covered: Input and output with iostreams cin cout and cerr file i/o with ofstream and ifstream The string class assign( ) compare( ) size( ) at( )


Download ppt "Module 5: I/O and Strings #1 2000/01Scientific Computing in OOCourse code 3C59 Module 5: I/O and STRINGS In this module we will cover The C++ input and."

Similar presentations


Ads by Google