I have the following program which I wrote in C. For simplicity, the preprocessor directives have been omitted from the code snipped shown below.
int entry(int people){ //function to add people to the event
int entered;
printf("Please enter the number of people you wish to add to the list.");
scanf("%d", &entered);
int newpeople = people + entered;
return newpeople;
}
int left(int people){
printf("How many people have left?");
int left;
scanf("%d", &left);
int newpeople = people - left;
return newpeople;
}
int main(void){
int people = 0; //to initialise the people variable
while (1){
int choice;
printf("Choose between 1. New people enter or 2. People exit");
scanf("%d", &choice);
if (choice == 1){
printf("You chose to add people.");
printf("The new amount of people is: %d", entry(people));
}
else if (choice == 2){
printf("You chose to remove people.");
printf("The new amount of people is: %d", left(people));
}
}
return 0;
}
My problem with this program is that whenever the while(1){}
loop initializes, it removes the value that was previously returned by the called functions. I would like it to keep going (ie. showing me the menu) until I choose to manually terminate it. Ideally, it should carry the value of the variable "int people" from one instance of the loop to the next.
I found that the value of people has to be initialized somewhere in main (otherwise junk values are present in it), but I have tried this in multiple places throughout the code without luck. It just gets reinitialized to the value 0 each time.
Any help would be much appreciated, even if it's a hint in the right direction.