Classes

I am having issues with this program. I have to use a constrictor TallyCounter::TallyCounter(), to initialize the tally to 0. Then I have to write a program, function main, that
utilizes this class and demonstrates how it works by counting people and displaying the current value
This is my code right now could anyone help? I know my main issues are in int get_value and void clear I think. Thanks for any and all help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  #include <iostream>
using namespace std;



class TallyCounter
{
 private:
 int tally; // private variable used in the class to hold the count 
 
 public:
 void count(int a)
 {
 	tally=a;
 	tally++;
 	
 } // void function to increments the counter by one
 void clear(); // void function to clear the tally to zero
 {
 tally.clear();	
 }

 
 int get_value(int counter)
 {
 return counter; // function to return the current tally value
}
 TallyCounter::TallyCounter(); // Constructor
};

int main()
{
	TallyCounter tallyObject;
	tallyObject.clear();
	tallyObject.count();
	tallyObject.get_value();
	

cout<<"Total: "<<get_value();
return 0;
}
I am pretty sure int does not have clear function...
besides you forgot to initialize your constructor you only declare it
also rather than
 
TallyCounter::TallyCounter();


it is

 
TallyCounter();


ant to directly initialize it
1
2
3
TallyCounter()
{
}
Last edited on
Last edited on
Okay so now I am getting errors in my compiler telling me invalid use of TallyCounter::TallyCounter(); and get_value was not declared// okay i see that this in function main thats why i am getting the error so how would i get the total to be displayed in function main if I cant use get value?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#include <iostream>
using namespace std;



class TallyCounter
{
 private:
 int tally; // private variable used in the class to hold the count 
 
 public:
 void count()
 {

 	++tally;
 	
 } // void function to increments the counter by one
  // void function to clear the tally to zero
 

 
 int get_value(int counter)
 {
 return counter; // function to return the current tally value
}
TallyCounter();
{

 TallyCounter::TallyCounter(); // Constructor
}
};

int main()
{
	TallyCounter tallyObject;
	tallyObject.count();
	tallyObject.get_value();
	tallyObject.TallyCounter();
	

cout<<"Total: "<<get_value();
return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

struct tally_counter
{
    // default constructor: initialises tally to zero
    tally_counter() : tally(0) {}

    int get() const { return tally ; } // return the current tally


    void set( int value ) { tally = value ; } // set the tally to value
    void clear() { set(0) ; } // reset the tally to zero

    // add n to the tally. default value for n is one
    void count( int n = 1 ) { tally += n ; }

    private: int tally ;
};

int main()
{
    tally_counter counter ; // initialises tally to 0

    counter.count() ; // 0 + 1 == 1
    std::cout << counter.get() << '\n' ; // 1

    counter.count(5) ; // 1 + 5 == 6
    std::cout << counter.get() << '\n' ; // 6

    counter.clear() ; // reset tally to zero
    std::cout << counter.get() << '\n' ; // 0

    counter.count() ; // 0 + 1 == 1
    std::cout << counter.get() << '\n' ; // 1
}
Okay so i see how that works. How would I get this if the operator would push any key and advance the counter by 1 every time?
What do you mean by "push any key"?
So basically the program asks me to implement a class that models a tally counter, a device that is used to count people like to find out how many people attend a concert or board a bus. Whenever the operator pushes a button, the counter value advances by one
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;



class TallyCounter
{
 private:
 int tally; // private variable used in the class to hold the count 
 
 public:
 void count()
 {

 	++tally;
 	
 } // void function to increments the counter by one
  // void function to clear the tally to zero
 void set( int total) 
{ 
tally = total ; 
}
 
void clear()
{
	set(0);
}
 
 int get_value()const
 {
 return tally; // function to return the current tally value
}


 TallyCounter:tally(0)
 {
 } // Constructor

};

int main()
{
	
	TallyCounter counter;
	
	counter.get_value();
	cout<<counter.get_value()<<"/n";
	 


return 0;
}

Okay I also did not want to copy your code so I implemented it into mine but it is still telling me I have errors. What is wrong with these implements? oops I missed the set value. implemented that but it is still telling me that i have an improper use of the TallyCount(0)
Last edited on
1
2
3
4
// TallyCounter:tally(0)
TallyCounter() : tally(0)
{
} // Constructor 
Oh crap I am sorry that was a terrible error on my part lol. So do you understand what I am asking would I put a cout statement inside the get value function to get user input? I don't understand how you could get any button to count people without hitting enter. Or maybe even a while loop!? I appreciate all of your help.
Last edited on
there is a way but it is not standard C++
either this way or use an external library
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <conio.h> //for getch()

int main()
{
   char input;
   do
   {
      input = getch(); //getch() directly is possible
   }while (input != 'e');
   std::cout << "yay, you pressed e\n'; 
} 


example of getch() directly
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <conio.h>

int main()
{
   do 
   {
   }while (getch() != 'e);
} 


either that or use other librarier that provides key checking such as sfml
Last edited on
no, you dont need to put cout inside
How would that count the tally's though? When I input that all it lets me do is hit e and then prints yay you hit e. So basically I would need a way of hitting any key and the program print 1 2 3 4 and keep counting up until I stop it. I could see using a for loop possibly to set (tally=0, tally++) something similar?
I did something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <conio.h>
using namespace std;



class TallyCounter
{
	
 private:
 int tally; // private variable used in the class to hold the count 
 
 public:
 void count()
 {
while(tally)
{

 	++tally;
}
 	
 } // void function to increments the counter by one
  // void function to clear the tally to zero
   void set( int total )
    {
    	
    	
		
	 tally = total;

	  }
 
void clear()
{
	set(0);
}
 
 int get_value()const
 {

	 
 return tally; // function to return the current tally value

}

TallyCounter() : tally(0)
{
} // Constructor 
 // Constructor

};

int main()
{
	TallyCounter counter;
 char input;
 
   do
   {
   	cout<<"Hit any letter to count people then hit e"<<endl;
      input =counter.get_value();
	  cin>>input; //getch() directly is possible
   }while (input!= 'e');
   cout << "yay, you pressed \n'"; 
	 
system("pause");

return 0;
}

But that just repeats the statement
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

struct tally_counter
{
    // default constructor: initialises tally to zero
    tally_counter() : tally(0) {}

    int get() const { return tally ; } // return the current tally


    void set( int value ) { tally = value ; } // set the tally to value
    void clear() { set(0) ; } // reset the tally to zero

    // add n to the tally. default value for n is one
    void count( int n = 1 ) { tally += n ; }

    private: int tally ;
};

int main()
{
    tally_counter counter ; // initialises tally to 0


    std::cout << "hit enter when a person boards the bus\n"
              << "end input with ctrl+D (unix/unix-clone) / ctrl+Z (windows)\n"
              << "or end program with ctrl+C (anywhere)\n" ;

    // while the user has hit enter and there stdin is not at eof
    while( std::cout << "? " && std::cin.get() && std::cin )
    {
        counter.count() ; // increment the tally
        std::cout << "current tally is " << counter.get() << '\n' ;
    }

}
I appreciate all the help you guys have given. I've never seen that ctrl+C method. Awesome thank you again!
Topic archived. No new replies allowed.