Passing variable through switch statement with fun

2019-07-12 16:00发布

问题:

I am attempting to learn how to pass variables through a menu using functions. Problem is, at no point was taught how to do so. As you can imagine, any variables I would enter in the first menu function to normally utilize in my cases/other functions such as

  if (count==0)
  {
    low = number;

    high = number;

    count++;

    sum = number;
  }
  else
  {
    if (number < low)
      number = low;

    if (number > high)
      high = number;

    count ++;

    sum += number;
  }

Will not pass through function 2, as anyone more knowledgeable with C would realize. It will not work in int main either. How would one go about defining user entered numbers,highest,lowest,etc. To the other functions? Here is what I have so far, the loop and menu works fine.

#include<stdlib.h>
#include<stdio.h>

int menuChoice()
{
  int choice;

  printf("1.Enter a number\n");
  printf("2.Display Highest Number Entered\n");
  printf("3.Display Lowest Number entered\n");
  printf("4.Display Average of Numbers Entered\n");
  printf("5.Quit\n");

  printf("Enter your choice:   ");
  scanf("%i", &choice);

  return choice;
}

int function1()
{
  int number;

  printf("Enter a number:\n");
  scanf("%i", &number);

  return number;
}

int function2()
{

}

int function3()
{

}

int function4()
{

}

int main()
{
  int quit = 0;
  while (quit != 1)
  {
    int menu;

    menu = menuChoice();
    switch (menu)
    {
    case 1:
      function1();
      break;
    case 2:
      function2();
      break;
    case 3:
      function3();
      break;
    case 4:
      function4();
      break;
    case 5:
      quit = 1;
      break;
    default:
      printf("Please enter 1 through 5\n");

    }
  }
  return 0;
}

回答1:

Let's look at some ways we can improve this code.

  1. Naming functions - Functions should be given clear, descriptive names that allow someone not familiar with the program to easily understand what they do. Names like function1 and function2 don't achieve this.

  2. Use appropriate data structures - It sounds like you're trying to keep track of all of the numbers the user entered, but you don't currently have any way of doing that. The best way to do this would be with an array, which is a container that holds other values.To keep track of the numbers you have so far, let's make two variables - an array that stores the numbers, and an integer that keeps track of how many numbers have been entered. For now, let's let the user enter up to 100 numbers. We do this by writing int arr[100]; and int numCount = 0; at the top of the program. Quick side note - this is called a global variable - usually it's not a good idea, but we won't worry about it for now.

  3. Separate code into appropriate functions - You do a good job of this in your function1. It performs its task well, which is to get a number from the user and return it. Now let's use that number. After the case 1, let's write

    arr[numCount] = function1(); numCount += 1;

    This sets the first unused entry in the array to the entered number, then increased the counter for the number of elements that we have.

  4. Don't perform unnecessary computation - Let's think about how we can implement our highest and lowest functions. One way to do it would be to go through the entire array each time the function is called and keep track of the highest number we have seen. This would work, but we would end up repeating a lot of work. What if each time we get a new number, we update the current highest and lowest number we have seen?

  5. Use loops to scan through arrays - To calculate the average, we will use a for loop. Look up the documentation for this if you don't understand it.

I think you can see how to extend this thinking to calculating the average of the entered values. Here's what I have after modifying - http://pastie.org/10285795.