User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,373 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,108 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1689 | Replies: 9
Reply
Join Date: Sep 2006
Posts: 67
Reputation: matrimforever is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
matrimforever matrimforever is offline Offline
Junior Poster in Training

Need help creating a simple Counter program using classes

  #1  
Oct 29th, 2006
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()
{
Last edited by matrimforever : Oct 29th, 2006 at 11:28 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 98
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Need help creating a simple Counter program using classes

  #2  
Oct 30th, 2006
Originally Posted by matrimforever View Post
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



Originally Posted by matrimforever View Post
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.
  1.  
  2. Counter counter;
  3. counter.increment();
  4. counter.decrement();
  5. counter.reset();
  6. ...
like that.
バルサミコ酢やっぱいらへんで
Reply With Quote  
Join Date: Sep 2006
Posts: 67
Reputation: matrimforever is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
matrimforever matrimforever is offline Offline
Junior Poster in Training

Re: Need help creating a simple Counter program using classes

  #3  
Oct 30th, 2006
Also, to use increment() and the such, you should declare a object of class [inlinecode]Counter, and then call those functions.
[code=c]
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();
};
Last edited by matrimforever : Oct 30th, 2006 at 8:38 pm.
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 98
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Need help creating a simple Counter program using classes

  #4  
Oct 30th, 2006
  1. #include "counter.h"
  2.  
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int num1;
  9. Counter counter;
  10. cout << "\nSelect from the following options: Type" << endl;
  11. cout << "\n1 to increment the counter" << endl;
  12. cout << "\n2 to decrement the counter" << endl;
  13. cout << "\n3 to display the contents of the counter" << endl;
  14. cout << "\n4 to reset the counter to zero" << endl;
  15. cout << "\n5 to exit this program." << endl;
  16. cout << "\nValid operators are 1 - 4 " << endl;
  17. cin >> num1;
  18.  
  19. switch( num1 )
  20.  
  21.  
  22. {
  23. case 1:
  24. counter.increment(); break;
  25. case 2:
  26. counter.decrement(); break;
  27. case 3:
  28. counter.display(); break;
  29. case 4:
  30. counter.reset(); break;
  31. case 5:
  32. exit(1);
  33. default:
  34. cout <<"Error, not a valid operator."<<endl;
  35.  
  36. }
  37.  
  38. return 0;
  39. }


counter.h
  1. class Counter
  2. {
  3. public:
  4. int Count; // If you want you can use long.
  5. // Counter(); Your assignment says no to define a contructor.
  6.  
  7. void increment()
  8. {
  9. Count++;
  10. }
  11. void decrement()
  12. {
  13. Count--;
  14. }
  15. void display(){ std::cout << Count << std::endl; };
  16. void resetCounter(){Count = 0;};
  17. };
バルサミコ酢やっぱいらへんで
Reply With Quote  
Join Date: Sep 2006
Posts: 67
Reputation: matrimforever is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
matrimforever matrimforever is offline Offline
Junior Poster in Training

Re: Need help creating a simple Counter program using classes

  #5  
Oct 30th, 2006
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.
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 98
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Need help creating a simple Counter program using classes

  #6  
Oct 31st, 2006
Originally Posted by matrimforever View Post
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.
  1. case 1: // You had written [color=red]'[/color]1[color=red]'[/color]

Originally Posted by matrimforever View Post
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]
  1. class Counter
  2. {
  3. public:
  4. int Count; // If you want you can use long.
  5. // Counter(); Your assignment says not to define a contructor.
  6.  
  7. void increment();
  8. void decrement();
  9. void display();
  10. void reset();
  11. };
[counter.cpp]
  1. #include "counter.h"
  2. #include <iostream>
  3. using namespace std;
  4. void Counter::increment()
  5. {
  6. Count++;
  7. }
  8. void Counter::decrement()
  9. {
  10. Count--;
  11. }
  12. void Counter::display()
  13. {
  14. std::cout << Count << std::endl;
  15. }
  16. void Counter::reset()
  17. {
  18. Count = 0;
  19. }
[driver.cpp]
  1. #include "counter.h"
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. int num1 = 0;
  7. Counter counter;
  8. counter.reset();
  9. while (1)// Added a infinite loop so that the program runs until you press 5.
  10. {
  11. cout << "\nSelect from the following options: Type" << endl;
  12. cout << "\n1 to increment the counter" << endl;
  13. cout << "\n2 to decrement the counter" << endl;
  14. cout << "\n3 to display the contents of the counter" << endl;
  15. cout << "\n4 to reset the counter to zero" << endl;
  16. cout << "\n5 to exit this program." << endl;
  17. cout << "\nValid operators are 1 - 4 " << endl;
  18. cin >> num1;
  19.  
  20. switch( num1 )
  21. {
  22. case 1:// If num1 is int, this should be 1. If num1 is char, this should be '1'.
  23. counter.increment();
  24. counter.display();
  25. break;
  26. case 2:
  27. counter.decrement();
  28. counter.display();
  29. break;
  30. case 3:
  31. counter.display();
  32. break;
  33. case 4:
  34. counter.reset();
  35. counter.display();
  36. break;
  37. case 5:
  38. exit(1);
  39. default:
  40. cout <<"Error, not a valid operator."<<endl;
  41. }
  42. }
  43. return 0;
  44. }
バルサミコ酢やっぱいらへんで
Reply With Quote  
Join Date: Sep 2006
Posts: 67
Reputation: matrimforever is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
matrimforever matrimforever is offline Offline
Junior Poster in Training

Re: Need help creating a simple Counter program using classes

  #7  
Oct 31st, 2006
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?
Last edited by matrimforever : Oct 31st, 2006 at 12:54 am.
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 98
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Need help creating a simple Counter program using classes

  #8  
Oct 31st, 2006
Originally Posted by matrimforever View Post
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.
バルサミコ酢やっぱいらへんで
Reply With Quote  
Join Date: Sep 2006
Posts: 67
Reputation: matrimforever is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
matrimforever matrimforever is offline Offline
Junior Poster in Training

Re: Need help creating a simple Counter program using classes

  #9  
Oct 31st, 2006
Thanks for the help! I learned a lot!
Reply With Quote  
Join Date: Oct 2006
Posts: 57
Reputation: may4life is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
may4life may4life is offline Offline
Junior Poster in Training

Re: Need help creating a simple Counter program using classes

  #10  
Nov 1st, 2006
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;
}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:32 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC