c - Update global variable from inside switch -
perhaps question obvious answer, i'm new c , have spent hours trying work myself , searching on site.
i'm building simple program starts variable holds number (1234 in case), , 1 of options in program allows user change variable (value).
so, user asked enter new number value
hold. asked same numbers again. if numbers entered same, value
should updated hold these new numbers (stored in both newvalue
, newvaluecheck
).
i'm trying figure out how update value
assigning numbers stored in new variables no matter try, when menu loops around , ask program numbers in value
, it's stuck 1234.
again, sorry if obvious, it's driving me insane.
int x = 1; int value = 1234; int enteredvalue = 0; int newvalue = 0; int newvaluecheck = 0; while (x != 0) { printf("1. example"); printf("2. change value"); scanf("%d", &atm); switch (x) { case 1: //code here break; case 2: printf("option 2 selected"); printf("please enter new value:"); scanf("%d", &newvalue); printf("please re-enter new value:"); scanf("%d", &newvaluecheck); if (newvalue == newvaluecheck) { printf("you have changed value."); //code update 'value' goes here? } else { printf("the values provided not match. please try again."); } break;
you dealing "scope" issue. since declare variable in loop, "lives" duration of iteration. declare before loop , should set
Comments
Post a Comment