•
•
•
•
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
![]() |
•
•
Join Date: Sep 2006
Posts: 67
Reputation:
Rep Power: 2
Solved Threads: 0
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? What I have so far, which is incomplete, but I want to see if I'm on the right track:
•
•
•
•
Do not write a constructor for this class.
- A function to add one to the current count.
- A function to subtract one from the current count.
- A function to reset the count to zero.
- A function that returns the current count.
Write a main( ) function that uses this class
Your program should work as follows:
- Display your student information
- Create a Counter object.
- 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
#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.
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation:
Rep Power: 8
Solved Threads: 98
•
•
•
•
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:
Count of class Counter.Also, to use increment() and the such, you should declare a object of class
Counter, and then call those functions. c Syntax (Toggle Plain Text)
Counter counter; counter.increment(); counter.decrement(); counter.reset(); ...
バルサミコ酢やっぱいらへんで
•
•
Join Date: Sep 2006
Posts: 67
Reputation:
Rep Power: 2
Solved Threads: 0
•
•
•
•
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.
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation:
Rep Power: 8
Solved Threads: 98
c Syntax (Toggle Plain Text)
#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
c Syntax (Toggle Plain Text)
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;}; };
バルサミコ酢やっぱいらへんで
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation:
Rep Power: 8
Solved Threads: 98
•
•
•
•
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.
c Syntax (Toggle Plain Text)
case 1: // You had written [color=red]'[/color]1[color=red]'[/color]
•
•
•
•
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]
c Syntax (Toggle Plain Text)
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(); };
c Syntax (Toggle Plain Text)
#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; }
c Syntax (Toggle Plain Text)
#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; }
バルサミコ酢やっぱいらへんで
•
•
Join Date: Sep 2006
Posts: 67
Reputation:
Rep Power: 2
Solved Threads: 0
Okay almost done. According to the sample output:
I put this in the Counter::display() function but it still only outputs Count...
do I put that sentence in each function, or just there? why wont it work?
The value of the counter is
void Counter::display()
{
std::cout << "The value of the counter is " << Count << std::endl;
} Last edited by matrimforever : Oct 31st, 2006 at 12:54 am.
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation:
Rep Power: 8
Solved Threads: 98
•
•
•
•
why wont it work?void Counter::display() { std::cout << "The value of the counter is " << Count << std::endl; }
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.
バルサミコ酢やっぱいらへんで
•
•
Join Date: Oct 2006
Posts: 57
Reputation:
Rep Power: 2
Solved Threads: 2
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;
}![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the C++ Forum
- Previous Thread: How to load dll files in Microsoft Visual C++?
- Next Thread: How do you reverse string elements



Linear Mode