Camel
Camel

Reputation: 37

C programming: How to continue input to array from certain element

So, basically, this is just a part of the complete program. What I'm trying to do here (but not really succeeding) is to input 10 values into my array. So, when I press 'e', I'm able to input values into my array until I enter 0. What I want to do is to be able to continue from where I left off in my array. Meaning if I entered values into my first three elements, the next time I press 'e', I want to continue from element four and when it gets up to 10 values, I can't input any more values. I've tried some if statements and different loops but can't seem to get it right. I'm still very new to this so any help would be appreciated!

Here is my code

int main(void)
{

char choice;

printf("Measurement tool 1.2\n");
loadMenu(choice);

return 0;
}

 void loadMenu(char theChoice)
        {
            int values[X]= {0}; 
            float counter = 0.0f;
            int i;
            do
            {
                        printf("VECRQ? ");
                        scanf(" %c",&theChoice);


                            switch(theChoice)
                            {
                                case 'v':
                                printf("[");

                                for(i=0; i<X; i++)
                                    {
                                    printf("%d ",values[i]);
                                    }

                                printf("]\n");
                                break;

                                case 'e': 
                                scanValue(values);
                                break;

                                case 'c':
                                maxmin(values, counter);
                                average(values, counter);
                                normValue(values);
                                break;

                                case 'r':
                                for(i=0; i<X; i++)
                                {
                                    values[i] = 0;
                                }
                                printf("Measurement values cleared!\n");
                                break;

                                case 'q':
                                printf("Exit measurement tool!\n");
                                break;

                                default:
                                printf("Wrong character! Choose between VECRQ.\n");
                            }
            } while(theChoice != 'q');


        }

void scanValue(int theValues[])
{
int i; int j=0;

    {   
    for(i=0;i<X;i++)
    {
        printf("Enter measurement %d (or 0):",i+1);
        scanf("%d",&theValues[i]);
        j=i;
        if(theValues[i]==0)
            break;
    }



            for(j; j<X; j++)
            {
            printf("Enter measurement %d (or 0):\n",j+1);
                scanf(" %d",&theValues[j]);

                if(theValues[j]==0)
                    break;
            }

 }

Upvotes: 1

Views: 757

Answers (2)

Gabriel Magri
Gabriel Magri

Reputation: 182

If you want to remind inside the scanValue function the index of the array which was the last used, you can easily make use of the static modifier for functions members. Like:

void scanValue(int theValues[]) {
  static int i;
  //... the logic of your function goes here
}

When you declare a static variable inside a functions, the value of this variable is hold between all the calls for the function. For example, look at this little code:

void main() {
  getNextIdx(); //Will return 1
  getNextIdx(); //Will return 2
}

void getNextIdx() {
  static int i = 0;
  i++;
  return i;
}

You can take advantage of this C language resource to simplify what you want. Of course, if I understood what you really want.

Ps.: Sometimes you may want to clear the value of the static variable inside the function, so, for this you can, for example, pass a boolean value to let the function know that you want to clear the variable, and make the logic to clear it inside the function.

Hope I had help you.

Upvotes: 0

Daniel H
Daniel H

Reputation: 7433

Your scanValues function starts from 0 each time, so it won’t notice if you’ve already entered anythig in the array.

If you first loop through the array looking for a zero to start at, like this:

for (i = 0; i < X; ++i)
{
    if (theValues[i] == 0)
    {
        // We found where we last left off
        break;
    }
}

then i will be set to the location of the first zero. If you then don’t re-initialize i in the existing loop, it will keep this same value, and go from there up to X (and if it’s already at X because all the values are nonzero, the loop won’t happen).

Having another loop over j just doing the same thing will mean that all the values will be read multiple times; you should get rid of that loop entirely since your i loop handles it.

If you do this you need to make sure to start out with the array elements all being zero; since you already do this at the top of loadMenu, that should be good.

Upvotes: 1

Related Questions