0

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.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Lorenzo Battilocchi
  • 862
  • 1
  • 10
  • 26
  • 1
    You never assign anything to `people` except `0`, so the value of the variable never changes. Is this your first programming language? – Siguza Apr 16 '17 at 19:24
  • yes it is @Siguza. I am fairly new to C. – Lorenzo Battilocchi Apr 16 '17 at 19:26
  • 3
    `entry(people)` --> `people = entry(people)` – BLUEPIXY Apr 16 '17 at 19:27
  • Then you should probably read this: [What's the difference between passing by reference vs. passing by value?](http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) – Siguza Apr 16 '17 at 19:28
  • @BLUEPIXY thank you! Solved the problem. I realised I wasn't updating the variable in main...! – Lorenzo Battilocchi Apr 16 '17 at 19:30
  • you should have proper idea about what is scope while working on functions. – Shivam Sharma Apr 16 '17 at 20:09
  • in the `entry()` function, should check that the number of people' will not overflow the 'people' counter. in the `left()` function, should check that the number of people will be >= 0 (difficult to have a <0 number of people in a room.) – user3629249 Apr 18 '17 at 16:35

4 Answers4

2

You should reassign people instead of just calling the function when you print the value.

Change this:

printf("The new amount of people is: %d\n", entry(people));

to this:

people = entry(people);
printf("The new amount of people is: %d\n", people);
neoland
  • 36
  • 4
1

The value of people is never modified, so it will always be 0. You need to assign the return value of each of your functions to people.

people = entry(people);
dbush
  • 205,898
  • 23
  • 218
  • 273
1

I'm considering that because you're working on functions in c then, you're very familiar about "SCOPE" & "STORAGE CLASSES" in c. if, don't know much about it then explore it yourself first,it's your homework:) ;)

There are two solution to your problem..

sol 1. you can make the variables as global by declaring them after just preprocessor directives.

  #include ...
        int entered;        //scope is maintained in whole program.
        int left;           // both globals initialized by 0.
       int entry(int people)
    {
           //same body
    }
    int left(int people)
    {
         //same body
    }
    void main()
    {
        // same body
    }

sol 2. Using static variables. ( static is also a storage class). it initilized by 0.

Ques. what is static variables?

Ans. The static storage class keep a "Local Variable" in existence during the life-time of the program instead of creating and destroying it each time. Therefore, making local variables static allows them to maintain their values between function calls.

Note:- local var. are variables which declared inside function's body.(scope is only inside function).

 #include ...

       int entry(int people)
    {
          static  int entered;            //initialized by 0
           //same body
    }
    int left(int people)
    {
         static int left;                 //initialized by 0
         //same body
    }
    void main()
    {
        // same body
    }

I hope now you're enough able to solve these scope problems by using these 2 solutions. :)

Shivam Sharma
  • 1,015
  • 11
  • 19
  • 1
    execute first, then judge my answer. and according to results you can treat my answer if you really feel it helpful for you and will helpful to others . :) – Shivam Sharma Apr 16 '17 at 20:28
0

Assign the value of people in the code in two places as

people = entry(people)
people = left(people)
sreepurna
  • 1,562
  • 2
  • 17
  • 32
  • Hey, "it's not the solution of problem which is asked". because question is asked about the scope of these variables which is not maintained... – Shivam Sharma Apr 16 '17 at 20:02