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.
#include <iostream>
usingnamespace 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;
}
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?
#include <iostream>
usingnamespace 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;
}
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
#include <iostream>
usingnamespace 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)
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.
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?
#include <iostream>
#include <conio.h>
usingnamespace 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;
}
#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' ;
}
}