Why is my variable not updating when I use a switch statement?

空扰寡人 提交于 2021-01-29 09:03:39

问题


I am a student, and I am trying to make an adventure text game, without using arrays. This code is a function, called by main (within another switch case statement). The problem is, the day doesn't update. It only updates when the code is included inline in main.

int getArea (int nArea) //area and day
{
    int nShisha, nBall,  nLolipop, nPepsi ;
    int nUser; //test variables
    int nPrice; // testing variable for price
    int naUser ;


    int nDay;
    nDay = 2;
    printf("Choose your Area\n");
    printf("1. Riyadh\n2. Jeddah\n3. Albaik\n4. Tazaj\n");
    printf("5. Go back\n");
    scanf("%d", &nArea);
    //nArea


    do{ //This is for the place
        switch (nArea)
        {
          case 1 : 
            printf("Riyadh\n");
            printf("Day %d of 15\n", nDay); 
            nDay ++;
            if (nDay == 16) {
                return 0;
            }   
            getPrice (&nShisha , &nBall, &nLolipop, &nPepsi ) ;// add charge soon
            break;
            
          case 2 :
            printf("Jeddah\n");
            printf("Day %d of 15\n", nDay);
            nDay ++;
            if (nDay == 16) {
                return 0;
            }   
            getPrice (&nShisha , &nBall, &nLolipop, &nPepsi ); // add charge soon
            break;
            
          case 3:
            printf("AlBaik\n");// add charge soon
            printf("Day %d of 15\n", nDay);
            nDay ++;
            if (nDay == 16) {
                return 0;
            }
            getPrice (&nShisha , &nBall, &nLolipop, &nPepsi );;
            break;
          case 4:
            printf("Tazaj\n");// add charge soon
            printf("Day %d of 15\n", nDay);
            nDay ++;
            if (nDay == 16) {
                return 0;
            }
            getPrice (&nShisha , &nBall, &nLolipop, &nPepsi );;
            break;


          default : printf("You have entered invalid!");
                    break;
        }
        //      printf("do you want to go to another place?\n");  //test code change later into better alternative
        scanf("%d", &nArea);

    }while (nArea!=0); //for area while
}

Can you guys please explain why is it not updating?


回答1:


Per your clarification, you are talking about variable nDay. In the code presented in the question, this variable is local to function getArea(). I am confident that it is successfully updated in the function, but that will have no effect on any like-named variable in main() or elsewhere. Those are different variables with the same name. Since you describe factoring this code out of main into its own function, I am fairly confident that this is the nature of the problem.

If you want the function to modify a variable that is local to the caller, then the caller must pass a pointer to that variable, and the function must update the caller's variable indirectly, via the pointer. For example, you might declare getArea() like this, instead:

int getArea(int area, int *nDay) {
    // ...

You would then also need to get rid of the other declaration of nDay inside getArea():

int nDay;

, and all other appearances of nDay inside the function body would need to be replaced with (*nDay).*

For its part, the caller would need to pass the appropriate pointer, something like:

int main(void) {
    int area, rval, nDay;
    // ...
    rval = getArea(area, &nDay);
    // ...
 }

* Technically, the parentheses are needed only in some cases, but they are harmless in the rest.




回答2:


The value of a variable in a user-defined function won't be updated in main() function as long as you are not returning it to to the main() function or the variable is a pointer that had been passed from the main function. In your code, even though the variable nDay has been updated in the function getArea, the variable won't be updated in main() function if you don't return it. you can use

int getArea (int nArea)
{
    ...
    ...
    return nDay; // Only this value will be passed to the main() function. nothing else updated here will be passed.
}

int main()
{
    int x,y,z, nArea, nDay;//some variable that you declared. Assuming nDay is one of them
    ...
    ...
    nDay=getArea(nArea); //value returned from the function(which is "nDay" from getArea()) will be assigned to nDay
    ...
}

Also, I don't see any reason for passing nArea to getArea() as you are overwriting/taking new input(value) of nArea inside the function. Note the nDay from main() and nDay from getArea() are actually different variable even though they have the same name.



来源:https://stackoverflow.com/questions/65798233/why-is-my-variable-not-updating-when-i-use-a-switch-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!