You are here:

C++/pls hellp me with this code..

Advertisement


Question
hi vijayan,

i want a concise code that does this.. i already tried it but not everything and want to see how i can compare mine with yours.. i want to use this to learn  just one thing..
how to reuse a class (class counter). so, i will appreciate it if you have two classes. the counter class and the Read class.

here is the question:

================================================================
Construct a class Counter that counts integers which can only take on values in a certain interval. When a counter is initialized, the start value should be indicated, as well as the least and greatest values the counter can take on. (if no boundary values are indicated, the counter should take on all the values of type int) there should be member functions with which we can count up and down a counter with the number 1. If a counter should then get a value which is not allowed, an error printout should result. We should be able to read a counter’s value.
Then use the class Counter in a program to read in a text and calculate how many blank characters occur in the text. Divide the text into appropriate files.
=================================================================

thanks.

henry

Answer
Here is how I would do it. You may not have as yet studied all the C++ features used in the program.

#include <string>
#include <iostream>
#include <limits>

struct counter
{
  explicit counter( int v, int lb = std::numeric_limits<int>::min(),
                   int ub = std::numeric_limits<int>::max() )
                : value(v), min(lb), max(ub) {}

  operator int() const { return value ; }

  counter& operator++ ()
  {
     if( value == max ) std::cerr << "upper bound exceeded\n" ;
     else ++value ;
     return *this ;
  }

  counter operator++ (int)
  {
     counter previous_value(*this) ;
     ++*this ;
     return previous_value ;
  }

  counter& operator-- ()
  {
     if( value == min ) std::cerr << "lower bound exceeded\n" ;
     else --value ;
     return *this ;
  }

  counter operator-- (int)
  {
     counter previous_value(*this) ;
     --*this ;
     return previous_value ;
  }
  private :
     int value ;
     int min ;
     int max ;
};

struct reader
{
      void read() { std::getline( std::cin, txt ) ; }

     int count_spaces() const
     {
        counter cntr(0) ;
        for( std::string::size_type i = 0 ; i<txt.size() ; ++i )
           if( txt[i] == ' ' ) ++cntr ;
        return cntr ;
     }

     const std::string& text() const { return txt ; }   

  private : std::string txt ;
};

int main()
{
   reader r ;
  r.read() ;
  std::cout << "'" << r.text() << "' has " << r.count_spaces() << " spaces.\n" ;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++09. i would not answer questions about gui and web programming.

Experience

over 15 years

Education/Credentials
post graduate engineer

©2012 About.com, a part of The New York Times Company. All rights reserved.