View Single Post
Old 10-27-2005, 10:41 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
write the previus problem as a class solution, with a friend istream &operator>>(istream &is, class_name &c) and friend ostream &operator<<(ostream &os, class_name c) to handle the read_in and print_out for the class.

Heres a short class example:
Code:
#include <iostream>
#include <string>

class removed{
private:
    std::string orig_line;    /* orriginal line from the user */
    std::string del_line;     /* orig_line after word has been removed */
public:
   /* remove the string represented by s from orig_str */
    bool remove(std::string &s){
        std::cout << "Removing \"" << s << "\" from \"" << orig_line << "\"" << std::endl;
        /* use s as a string (or char*) and remove any occurences from orig_line 
         * store the result in del_line and return true for success else return false
         */
    };
   /* read in from is to the orig_line */
    friend istream &operator>>(istream &is, removed &r){
        std::cout << "Please give the orriginal line of text: ";
        std::cout.flush();
        std::getline(is, r.orig_line);
        return is;
    };
   /* print the final del_line with the word removed */
    friend ostream &operator<<(ostream &os, removed r){
        os << "The line: \"" << r.orig_line << "\" << std::endl;
        os << "After deletion: \"" << r.del_line << "\" << std::endl;
        return os;
    };
};

int main()
{
    removed r;
    std::string word;
    std::cin >> r;
    std::cout << "Give word to be removed: ";
    std::cout.flush();
    std::cin >> word;
    if(r.remove(word))
        std::cout << r;
    else
        std::cout << "Error removing word \"" << word "\"" << std::endl;
    return 0;
}
same goes as the first time, carefully read teh code and try to understand what is going on.

Note: none of the code above has been tested, it is written directly into the composer window, so it may or may not have syntactic errors.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote