I am new here and this is my first post so I'm sorry if anything in this post is done incorrectly. I have taken a C++ class but did not get into any object oriented parts of it (all linear). Now I am in an object oriented class which is focused through C++ and everything seems totally different and I am lost. I need to do an assignment where a string posts and each letter is counted and put into an array as pairs (char, # of occurrence). Then print the array to show how many times each letter was used. I am so lost I do not have any code yet. I have some starter code the professor gave us. Can anyone help me? It will be greatly appreciated.

//cis 326 (OOP I) - Week 2 Programming Assignment Starter Code
#include <iostream>
#include <iomanip>
using namespace std;

// an object to count occurance of a given character
class char_count
{
public:
    char_count(char val):char_2count(tolower(val)), count(0){}; //ctor
    void add_ifMatch(char c)
    {
        if (tolower(c) == char_2count ) count ++;
    };
    void print_count()
    {
        cout << "chars '" << char_2count << "|" << (char)toupper(char_2count)
            << "' occured "   << std::setw(4) << count << " times.\n";
    };
private:
    int count;
    char char_2count;
};
// "Classes have the property of information hiding. This means that although class objects may know how to communicate with one another across well-defined interfaces, classes normally are not allowed to know how other classes is implemented-implementation details are hidden within the classes themselves. Surely it is possible to drive a car effectively without knowing the details of how engines, transmissions and exhaust systems work internally. We will see why information hiding is so crucial to good software engineering."
int main()
{
    char_count c_cnt( 'a' );

    char c;  // loop until user types end-of-file key sequence
    while ( ( c = cin.get() ) != EOF )
        c_cnt.add_ifMatch(c);

    c_cnt.print_count();

    system("pause");
    return 0;
}

Recommended Answers

All 4 Replies

Welcome to DaniWeb!

Please use code tags when posting code. It makes it much more readable for us.

The first step would be to convert to lowercase. You can use this method:
http://programmingexamples.net/index.php?title=CPP/Strings/Case_Conversion

Then take a look at this example. It shows how to count the occurrences of a character:
http://programmingexamples.net/index.php?title=CPP/Strings/CountCharacters

You can store each count in a std::pair:
http://programmingexamples.net/index.php?title=CPP/STL/Pair

Then store the pairs in a std::vector - you're done!

Let us know if you have any problems once you give it a try.

Dave

commented: your website in action! +5

A solution is actually even simpler than what David suggested above. But I think the OP's instructor wants him to use that c++ class he posted. He will have to use an array, or vector, of those class objects. IMO that's really a dumb way to solve the problem, but the OP probably has no other choice.

Thank you so much, you helped me a lot. This is a great site, lots of helpful people. I hope that maybe I'll be someone that can help others with programming soon. Lol. Sorry about the tags for the code. I tried to use them but I guess it didn't work. What do I need to do next time to do it right. Thanks again.

Some people use the code tags button, but I do it the hard way and just type them in

[code]

// your code goes here

[/code]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.