I have an array of 10 integers, and k is an integer that loops through the array.
Whenever I do ‘k = k+1’ , one is added to ‘k’, (if ‘k’ is 7, then it becomes 8). But if ‘k’ reaches till 9, I dont want ‘k+1’ evaluate to 10, I want it to become 1;
I considered using a function like this:
void add_one(int &k){
if(k == 9){
k = 1;
}else{
k = k+1;
}
}
and whenever i want to add one to ‘k’:
add_one(k);
Then I found out about operator overloading, but it was very confusing and think there must be a better way. Can we tell c++ that whenever it sees k+1 where k = 9, it must return 1 and not 10?
You could use an expression like: