#include "WebCounter.h"
#include <iostream>
usingnamespace std;
int main()
{
int number=0;
WebCounter counter;
cout << " Enter your start number: ";
cin >> counter;
cout << endl;
while (1)
{
cout << "\nChoose the number that you want" << endl;
cout << "\n1 to increment the counter" << endl;
cout << "\n2 to display the contents of the counter" << endl;
cout << "\n3 to reset the counter to zero" << endl;
cout << "\n4 to exit this program." << endl;
cin >> number;
cout << endl;
switch( number)
{
case 1:
counter.hit();
counter.display();
break;
case 2:
counter.display();
break;
case 3:
counter.reset();
counter.display();
break;
case 4:
exit(1);
default:
cout <<"Please enter 1-4."<<endl;
}
}
return 0;
}
So the problem is I am stuck at enter the start number. It is always start at 0 and it is not right
1 2 3 4 5
int number=0;
WebCounter counter;
cout << " Enter your start number: ";
cin >> counter;
cout << endl;
Why not use your set function as the start number? o.O
1 2 3
WebCounter counter;
cout << "Enter your start number: ";
counter.set( 0/*? Why do you have param?*/ );
Also why do you have a parameter on your set function? I'm confused on it you aren't even use it..
*edit on a side not
instead of doing
x = x + 1
You can use x += 1;
check out compound operators
+= , -= , *= , /= , %=
Also when ever you increment a number by 1 you can use the ++ operator postfix or prefix. Same thing when you decrement you can use the -- operator postifx or prefix.
So x = x + 1 -> x += 1 -> ++x;
When in doubt use the prefix ++x instead of the postfix x++
They can have different results depending on the scenario.
Like outputting ++i and i++ are different.
Lets look at an example
1 2 3 4 5
int i = 0;
cout << ++i << endl; // adds 1 to i then outputs. i = 1 output = 1
cout << i++ << endl; //outputs i then adds 1. outputs 1 i = 2
cout << i << endl; //i = 2 outputs 2
Why would you send a variable just to input to it. You are passing by a copy anyways not a reference so it would basically do nothing.
Maybe you mean something like this?
1 2 3 4 5 6 7 8 9 10 11 12
int main()
{
WebCounter counter;
int input = 0;
cin >> input;
counter.set( input );
}
void WebCounter::set( int input )
{
counter = input;
}
Its just a rough sketch of what I think you are trying to do.
*edit honestly I think you should just use a constructor for setting a starting value but that's just me.
1 2 3 4 5 6 7 8 9 10 11
class Obj
{
public:
Obj( void );
Obj( int value = 0 ); //default value of 0
private:
int counter;
};
Obj::Obj( void ) : counter( 0 ){}
Obj::Obj( int value ) : counter( value ){}