Solved

How to convert  object data members to lower case.

Posted on 2003-11-13
7
237 Views
Last Modified: 2011-09-20
Hello experts:

I have a Book class and am trying to figure out how to change just two of the data members (author_name and title) to lower case.  Is there any way to adapt the make_lower function (see code below) to work with objects?  Or am I going in the wrong direction alltogether.  I have spent many hours looking for examples online and in texts but can't seem to find anything close.  Any help would be appreciated!


-----------------------------------------------------------------
#include <string>
using namespace std;

# ifndef BOOK_H
# define BOOK_H

class Book
{
      public:
            Book() { };
            Book(string an_author_name, string a_title, string a_publisher,                    string an_isbn, string a_pub_year,
                   string a_page_count, string a_price, string a_book_type)
            {
                  author_name = an_author_name;
                  title = a_title;
                  publisher = a_publisher;
                  isbn = an_isbn;
                  pub_year = a_pub_year;
                  page_count = a_page_count;
                  price = a_price;
                  book_type = a_book_type;
            }

            ~Book() { };
            
            string get_author_name() const;
            string get_title() const;
            string get_publisher() const;
            string get_isbn() const;
            string get_pub_year() const;
            string get_page_count() const;
            string get_price() const;
            string get_book_type() const; //hardcover or paperback
            
            void set_author_name(string new_author_name);
            void set_title(string new_title);
            void set_publisher(string new_publisher);
            void set_isbn(string new_isbn);
            void set_pub_year(string new_pub_year);
            void set_page_count(string new_page_count);
            void set_price(string new_price);
            void set_book_type(string new_book_type);  
            
            string to_string();
            string make_lower(const string& str);

      private:
            string author_name;
            string title;
            string publisher;
            string isbn;
            string pub_year;
            string page_count;
            string price;
            string book_type;
};
#endif;

--------------------------------------------------
// file name: book8.cpp
// implementation file for Book class

#include <iostream>
#include <sstream>
#include <iomanip>
#include <ctype.h>
#include "book8.h"

using namespace std;

string Book::get_author_name() const
{
      return author_name;
}

string Book::get_title() const
{
      return title;
}
string Book::get_publisher() const
{
      return publisher;
}

string Book::get_isbn() const
{
      return isbn;
}

string Book::get_pub_year() const
{
      return pub_year;
}

string Book::get_page_count() const
{
      return page_count;
}

string Book::get_price() const
{
      return price;
}

string Book::get_book_type() const
{
      return book_type;
}

void Book::set_author_name(string new_author_name)
{
      author_name = new_author_name;
}

void Book::set_title(string new_title)
{
      title = new_title;
}

void Book::set_publisher(string new_publisher)
{
      publisher = new_publisher;
}

void Book::set_isbn(string new_isbn)
{
      isbn = new_isbn;
}

void Book::set_pub_year(string new_pub_year)
{
      pub_year = new_pub_year;
}

void Book::set_page_count(string new_page_count)
{
      page_count = new_page_count;
}

void Book::set_price(string new_price)
{
      price = new_price;
}

void Book::set_book_type(string new_book_type)
{
      book_type = new_book_type;
}

string Book::to_string()
{
      
      return ("NAME: " + author_name + " | " + "TITLE: " + title + " | "
                "PUBLISHER: " + publisher + " | " + "ISBN: " + isbn +
                 " | " + "YEAR: " + pub_year + " | " + "PAGES: " +
                   page_count + " | " + "PRICE: " + price + " | " + book_type);      
               
}

string make_lower(const string& str)
{
      string temp(str);

      for (unsigned int i = 0; i < str.length; i++)
      {
            temp[i] = tolower(str[i]);
            return temp;
      }
}
-----------------------------------------------
//file name: book_user8.cpp

#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>
#include "book8.h"

using namespace std;

int main()
{
      string(str);
      ofstream fout;
      
      Book b1("Bill W. Smith", "Utopia", "Mcgraw Hill", "00-321-4567", "1978", "800",
                    "5.99", "hardcover");
      
      cout << b1.to_string() << endl << endl;
      fout.open("C:\\CSC311\\files\\book8_out.txt");
      fout << b1.to_string() << endl << endl;

      b1.set_author_name("William W. Smith");
      
      cout << b1.to_string() << endl << endl; //for debugging
      fout << b1.to_string() << endl << endl; //for debugging
      
      Book b2;
      
      b2.set_author_name("John J. Jones");
      b2.set_title("The Best You Can Be!");
      b2.set_publisher("Mcmillan");
      b2.set_book_type("paperback");
      b2.set_page_count("1003");
      b2.set_pub_year("1897");
      b2.set_price("47.95");
      b2.set_isbn("23-98-10004");

      cout << b2.to_string() << endl << endl; //for debugging
      fout << b2.to_string() << endl << endl; //for debugging

      b2.make_lower();

      return 0;
}



                             
0
Comment
Question by:deekakes
  • 3
  • 3
7 Comments
 
LVL 19

Assisted Solution

by:Dexstar
Dexstar earned 75 total points
ID: 9743775
deekakes:

> I have a Book class and am trying to figure out how to change just two of the
> data members (author_name and title) to lower case.  Is there any way to adapt
> the make_lower function (see code below) to work with objects?  Or am I going
> in the wrong direction alltogether.  I have spent many hours looking for
> examples online and in texts but can't seem to find anything close.  Any help
> would be appreciated!

You can't use on objects like that, but you could add a "make_members_lower" method to your class, and have it call the make_lower function for each string it has.

void Book::make_members_lower()
{
      author_name = make_lower( author_name );
      // Repeat for other members
}

Hope That Helps,
Dex*
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9743815
Hi deekakes,

First of all, I'm not sure that make_lower would be a method of the book class, as it does not do anything to the object itself, it is just an auxiliary function.

Here's your make_lower function working with strings, it receives a string as a paremter and returns it in lowercase

for example:

make_lower( "MyStringExample" ) returns mystringexample


string make_lower(const string& str)
{
     string temp(str);
       int i = 0;

     for( string::const_iterator it = str.begin(); it != str.end(); ++it, i++ )
     {
          temp[i] = tolower( *it );
     }
      return temp;
}

Hope this helps

Tincho
0
 

Author Comment

by:deekakes
ID: 9743917
Thanx.....I would have never thought of that!  However the compiler complains:

c:\csc311\files\book8.cpp(110) : error C2297: '<' : illegal, right operand has type 'unsigned int (__thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::*'

the compiler did the same thing even when I didn't have "unsigned" befor int.  Any suggestions? ( I was wondering if it had to do with VC++.)
---------------------------------------------------------------------
string make_lower(const string& str)
{
     string temp(str);

     for (unsigned int i = 0; i < str.length; i++)
     {
          temp[i] = tolower(str[i]);
          return temp;
     }
}
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 9

Expert Comment

by:tinchos
ID: 9743934
deekakes,

I would definitely recommend you to use the code I provided.
I believe that it is safer and easier to access the string using an iterator than indexing it.

Try with the code above, I've tested it and it should work as you want to.

Tincho
0
 

Author Comment

by:deekakes
ID: 9744063
I added the following code:

void Book::make_members_lower()
{
      author_name = make_lower(author_name);
        title = make_lower(title);
}


string make_lower(const string& str)
{
     string temp(str);
      int i = 0;

     for( string::const_iterator it = str.begin(); it != str.end(); ++it, i++ )
     {
          temp[i] = tolower( *it );
     }
     return temp;
}

And added this call in the main program:
b2.make_members_lower();

The compiler states:

ook8.obj : error LNK2001: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Book::make_lower(class std::basic_string<char,struct std::char_traits<char>,class std::
allocator<char> > const &)" (?make_lower@Book@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV23@@Z)
Debug/Book8.exe : fatal error LNK1120: 1 unresolved externals
 
0
 
LVL 9

Accepted Solution

by:
tinchos earned 75 total points
ID: 9744090
The declaration of

string make_lower(const string& str)

must be before the call, so you have 2 possibilites

either

string make_lower(const string& str)
{
     string temp(str);
      int i = 0;

     for( string::const_iterator it = str.begin(); it != str.end(); ++it, i++ )
     {
          temp[i] = tolower( *it );
     }
     return temp;
}

void Book::make_members_lower()
{
      author_name = make_lower(author_name);
       title = make_lower(title);
}


or

string make_lower(const string& str);

void Book::make_members_lower()
{
      author_name = make_lower(author_name);
       title = make_lower(title);
}


string make_lower(const string& str)
{
     string temp(str);
      int i = 0;

     for( string::const_iterator it = str.begin(); it != str.end(); ++it, i++ )
     {
          temp[i] = tolower( *it );
     }
     return temp;
}

Hope this helps

Tincho
0
 

Author Comment

by:deekakes
ID: 9744139
Thanx...works fine!!  Thank you for preventing me from jumping off a cliff :)

I increased the points....75 to Dexstar for the function I would have never thought of and 75 to tincho for the other excellent and clearcut solutions.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now