Incrementing vector value

I'm trying to increment the values in a vector, not the vector size, based on variable input. Basically I have a vector of size 10, and all of its values are initialized at zero. The program counts the frequency of numbers 0-9 in a four digit user input. This is what I have (I want it to work so badly but the compiler says that I'm using a pointer to a function used in arithmetic):

for (int i=0; i < num_slots; ++i) {
++guess_frequency[guess[i]];
}

I just want to know if you can increment values within a vector:
e.g. change
0 0 0 0 0 0 0 0 0 0
to
1 0 0 0 2 0 0 1 0 0
> I just want to know if you can increment values within a vector

Yes. For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> frequency(10) ;

    unsigned int user_input = 3703537 ;

    while( user_input != 0  )
    {
        ++frequency[ user_input % 10 ] ;

        user_input /= 10 ;
    }

    for( int i : frequency ) std::cout << i << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/53f0d365109c22fe
I keep getting a compiler error that I'm using a pointer to a function in arithmetic. And the user input is stored in a vector, not an int.
Last edited on
> I keep getting a compiler error that I'm using a pointer to a function in arithmetic.
> And the user input is stored in a vector, not an int.

Post the code, and the error message, and we may be able to help.
Here's the code I have:

for (int i=0; i < range_top; ++i){ // initialize vector values at 0
guess_frequency.push_back(zero);
solution_frequency.push_back(zero);
}
for (int i=0; i < num_slots; ++i){ // insert values for guess freq.
int x;
x = guess[i];
++guess_frequency[x];
}

for (int i=0; i < num_slots; ++i){ // insert values for solution freq.
int y;
y = solution[i];
++solution_frequency[y];
}

And the errors I get are:
proj3.cc:202:20: warning: pointer to a function used in arithmetic [-Wpointer-arith]
x == guess[i];
^
proj3.cc:202:20: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
proj3.cc:208:23: warning: pointer to a function used in arithmetic [-Wpointer-arith]
y == solution[i];
^
proj3.cc:208:23: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
make: *** [proj3] Error 1
what exactly are guess and solution I don't see you define them anywhere.
> x == guess[i];
> ^
> proj3.cc:202:20: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
> proj3.cc:208:23: warning: pointer to a function used in arithmetic [-Wpointer-arith]
> y == solution[i]; 
> ^
> proj3.cc:208:23: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]


And what are the declarations for guess and solution?

From the diagnostic, they are names of functions.
Did you intend x == guess(i); and y == solution(i);
Topic archived. No new replies allowed.