I need help with Hit Counter

Hi everyone I have done this so far and it run perfectly.

WebCounter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #ifndef WEBCOUNTER_H
#define WEBCOUNTER_H

  class WebCounter {

    public:
      int get();
      void hit();
	  void display();
      void reset();
      void set(int input);
	 

    private:
      int counter;
};
#endif


WebCounter.cpp
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
#include<iostream>
#include"WebCounter.h"

using namespace std;

void WebCounter::set(int input)
{
	cin >> counter;
}

void WebCounter::hit()
{
  counter=counter+1;
}

void WebCounter::display()
{
	cout << counter << endl << endl;
}

void WebCounter::reset()
{
	counter=0;
}


Running.cpp
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 "WebCounter.h"
#include <iostream>
using namespace 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;


How can I fix it?
Did you mean to cin to counter instead of number? It doesn't look like this code can compile anyway...
Last edited on
yeah I know I am messing around with it. the original is
1
2
3
	int number=0;
	WebCounter counter;
    counter.reset();


But I want to "cin" the start number because it always starts at 0
Last edited on
I just want to be able to set the value of the counter. Anyone help @@
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 
Last edited on
giblit wrote:
Also why do you have a parameter on your set function? I'm confused on it you aren't even use it..
Because the function name says "I'll take what you give me and use it" and the implementation says "what's a parameter"?

Basically he just implemented it wrong ;)
Yeah I really dont know how to compare the set function to the programs

That parameter support to be a "user input number"

1
2
3
4
5
void WebCounter::set(int input)
{
	cin >> input;
	input=counter;
}


It gives me an error
Your set function should not contain cin in any way.
so how can I set up a user input number to start the program anyway?

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 ){}
Last edited on
yeah it is what i want to do but now it gives me a huge error the output is like

Enter the number : " I enter a random number like 5"

1 to increase
2 to display
3 rest
4 quit

" I enter 1"

your current number -858993459

zakelong81 wrote:
your current number -858993459
This means you have an uninitialized variable.
Anyway if I enter 1 it will have infinity running programs
Works fine for me

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
#include <iostream>

class WebCounter
{
    public:
        void set( int value );
        void hit( void );
        void display( void );
    private:
        int counter;
};

void WebCounter::set( int value )
{
    counter = value;
}

void WebCounter::hit( void )
{
    ++counter;
}

void WebCounter::display( void )
{
    std::cout << counter << std::endl;
}

int main()
{
    WebCounter counter;
    int input = 0;
    std::cout << "Please enter initial value: ";
    std::cin >> input;
    counter.set( input );
    counter.hit();
    counter.display();
}
Please enter initial value: 5
6
Topic archived. No new replies allowed.