What I am trying to do:

  1. A function to add one to the current count.
  2. A function to subtract one from the current count.
  3. A function to reset the count to zero.
  4. A function that returns the current count.

Do not write a constructor for this class.
Write a main( ) function that uses this class
Your program should work as follows:

  1. Display your student information
  2. Create a Counter object.
  3. Write a loop that:
    • Prompts the use with the following menu: 1 - add 1 to Counter
      2 - subtract 1 from Counter
      3 - display contents of Counter
      4 - reset counter
      5 - exit program

Does the class object go in the header file or is it okay here? What do I put in the header file? What I have so far, which is incomplete, but I want to see if I'm on the right track:

#include <iostream>
#include "counter.h"


using namespace std;

class Counter
{
public:
    int myCounter()
    
    void increment()
    {
        Count++;
    }
    void decrement()
    {
        Count--;
    }
     void display();
     void resetCounter();
    };

int main()
{
    
        cout << "\nSelect from the following options: Type" << endl;
        cout << "\n1 to increment the counter" << endl;
        cout << "\n2 to decrement the counter" << endl;
        cout << "\n3 to display the contents of the counter" << endl;
        cout << "\n4 to reset the counter to zero" << endl;
        cout << "\n5 to exit this program." << endl;
        cout << "\nValid operators are 1 - 4 " << endl;
        cin >> num1;
    
    switch( num1 )


            {
            case'1':
                increment(); break;
            case'2':
                decrement(); break;
            case'3':
                display(); break;
            case'4':
                resetCounter(); break;
            case'5':
                exit(1);
            default:
                cout <<"Error, not a valid operator."<<endl;
    
    
    return 0;
}

void incCounter()
{

What I am trying to do:
Does the class object go in the header file or is it okay here? What do I put in the header file?

Well, you can put it here if you want. But the usual practice is to give it in the header file. The implementation is given in a cpp file, which is also usually seperate from the main.cpp file. So you will have 3 files in your program.
counter.h
counter.cpp
main.cpp


What I have so far, which is incomplete, but I want to see if I'm on the right track:

You may find this out yourself when you compile, but you haven't declared the variable Count of class Counter .

Also, to use increment() and the such, you should declare a object of class Counter , and then call those functions.

Counter counter;
counter.increment();
counter.decrement();
counter.reset();
...

like that.

Also, to use increment() and the such, you should declare a object of class [inlinecode]Counter, and then call those functions.

Counter counter;
counter.increment();
counter.decrement();
counter.reset();

Not sure where to put these. I put them here but get errors:

#include "counter.h"


using namespace std;

int main()
{
    Counter();
    Counter.increment();
    Counter.decrement();
    Counter.reset();
    Counter.display();
int num1;
        cout << "\nSelect from the following options: Type" << endl;
        cout << "\n1 to increment the counter" << endl;
        cout << "\n2 to decrement the counter" << endl;
        cout << "\n3 to display the contents of the counter" << endl;
        cout << "\n4 to reset the counter to zero" << endl;
        cout << "\n5 to exit this program." << endl;
        cout << "\nValid operators are 1 - 4 " << endl;
        cin >> num1;

    switch( num1 )


            {
            case'1':
                counter.increment(); break;
            case'2':
                counter.decrement(); break;
            case'3':
                counter.display(); break;
            case'4':
                counter.reset(); break;
            case'5':
                exit(1);
            default:
                cout <<"Error, not a valid operator."<<endl;



    return 0;
}

And in counter.h:

#include <iostream>
double Count;
Counter counter;

class Counter
{
public:
    Counter();

    void increment()
    {
        Count++;
    }
    void decrement()
    {
        Count--;
    }
     void display();
     void resetCounter();
};
#include "counter.h"
 
 
using namespace std;
 
int main()
{
        int num1;
        Counter counter;
        cout << "\nSelect from the following options: Type" << endl;
        cout << "\n1 to increment the counter" << endl;
        cout << "\n2 to decrement the counter" << endl;
        cout << "\n3 to display the contents of the counter" << endl;
        cout << "\n4 to reset the counter to zero" << endl;
        cout << "\n5 to exit this program." << endl;
        cout << "\nValid operators are 1 - 4 " << endl;
        cin >> num1;
 
    switch( num1 )
 
 
            {
            case 1:
                counter.increment(); break;
            case 2:
                counter.decrement(); break;
            case 3:
                counter.display(); break;
            case 4:
                counter.reset(); break;
            case 5:
                exit(1);
            default:
                cout <<"Error, not a valid operator."<<endl;
 
           }
 
    return 0;
}

counter.h

class Counter
{
public:
    int Count; // If you want you can use long.
    // Counter(); Your assignment says no to define a contructor.
 
    void increment()
    {
        Count++;
    }
    void decrement()
    {
        Count--;
    }
     void display(){ std::cout << Count << std::endl; };
     void resetCounter(){Count = 0;};
};

Thanks for the help so far. Something's wrong with my switch statement. It just goes to default , which is invalid selection then exits. What do I need to change?

Also i need to submit a driver.cpp file. Is that what you meant as the main.cpp? Not sure what that is.

Thanks for the help so far. Something's wrong with my switch statement. It just goes to default , which is invalid selection then exits. What do I need to change?.

Most probably you haven't seen that I removed the quotes in the case statements.

case 1: // You had written '1'

Also i need to submit a driver.cpp file. Is that what you meant as the main.cpp? Not sure what that is.

If you have to submit only 2 files, counter.h and driver.cpp, just rename the main.cpp file to driver.cpp. If you have to submit 3 files, counter.h, counter.cpp and driver.cpp, you will have to shift the implementation of the counter class to counter.cpp and the main function to driver.cpp.

[counter.h]

class Counter
{
public:
    int Count; // If you want you can use long.
    // Counter(); Your assignment says not to define a contructor.
 
    void increment();
    void decrement();
    void display();
    void reset();
};

[counter.cpp]

#include "counter.h"
#include <iostream>
using namespace std;
void Counter::increment()
{
    Count++;
}
void Counter::decrement()
{
    Count--;
}
void Counter::display()
{ 
    std::cout << Count << std::endl; 
}
void Counter::reset()
{
    Count = 0;
}

[driver.cpp]

#include "counter.h"
#include <iostream>
using namespace std;
int main()
{
    int num1 = 0;
    Counter counter;
    counter.reset();
    while (1)// Added a infinite loop so that the program runs until you press 5.
    {
        cout << "\nSelect from the following options: Type" << endl;
        cout << "\n1 to increment the counter" << endl;
        cout << "\n2 to decrement the counter" << endl;
        cout << "\n3 to display the contents of the counter" << endl;
        cout << "\n4 to reset the counter to zero" << endl;
        cout << "\n5 to exit this program." << endl;
        cout << "\nValid operators are 1 - 4 " << endl;
        cin >> num1;
     
        switch( num1 )
        {
        case 1:// If num1 is int, this should be 1. If num1 is char, this should be '1'.
            counter.increment(); 
            counter.display();
            break;
        case 2:
            counter.decrement();
            counter.display();
            break;
        case 3:
            counter.display();
            break;
        case 4:
            counter.reset(); 
            counter.display();
            break;
        case 5:
            exit(1);
        default:
            cout <<"Error, not a valid operator."<<endl;
        }
    }
    return 0;
}

Okay almost done. According to the sample output:

The value of the counter is

I put this in the Counter::display() function but it still only outputs Count...

void Counter::display()
{ 
    std::cout << "The value of the counter is " << Count << std::endl; 
}

do I put that sentence in each function, or just there? why wont it work?

void Counter::display()
{ 
    std::cout << "The value of the counter is " << Count << std::endl; 
}

why wont it work?

This is correct. So no idea. Maybe you have not saved the correction. Or you are linking a different file. Anyway it is a problem with the linker, and not the source code. The sourcecode is correct. Double chekc the contents of the files you are compiling.

commented: Awesome advice and help! +1

Thanks for the help! I learned a lot!

Whoops! forward slash got away there... here we go

#include <iostream>

using namespace std;

class Counter
{
    public:
        //Counter(){}
        //~Counter(){}
        void increment() { itsValue++; }
        void decrement() { itsValue--; }
        void display() const { cout << itsValue << endl; }
        void reset() { itsValue = 0; }
    private:
        int itsValue;
};

int DoMenu();

int main()
{
    int choice;
    Counter counter;
 
    do
    {
        choice = DoMenu();

        switch(choice)
        {
            case 1: counter.increment(); break;
            case 2: counter.decrement(); break;
            case 3: counter.display(); break;
            case 4: counter.reset(); break;
        }
    } while (choice != 5);
 
    return 0;
}

int DoMenu()
{
    int MenuChoice;
        cout << "\nSelect from the following options: Type";
        cout << "\n1 to increment the counter";
        cout << "\n2 to decrement the counter";
        cout << "\n3 to display the contents of the counter";
        cout << "\n4 to reset the counter to zero";
        cout << "\n5 to exit this program.";
        cout << "\nValid operators are 1 - 5: ";
    cin >> MenuChoice;
    return MenuChoice;
}
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.