2

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;
}
alk
  • 69,737
  • 10
  • 105
  • 255
Session
  • 33
  • 1
  • 5
  • the display of the menu should be in a loop, then when scanf() returns a value, 1) check that the returned value from scanf(), not the parameter, is (in this case) 1. otherwise, getchar() until EOF or '\n' is read the re-display the menu. 2) after a successful scanf(), check that the value read is in the range 1...5. If not in the range, the loop to display the menu again (perhaps after displaying an error message. – user3629249 Jul 12 '15 at 00:22
  • There should be a 'break;' statement at the end of the default case in the switch statement – user3629249 Jul 12 '15 at 00:25

1 Answers1

2

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.

Erik Godard
  • 5,930
  • 6
  • 30
  • 33
  • Thank you! I did have two questions though. 1. How were you able to declare the variable new number? Just by defining it in the void functions? 2. Can tell more about INT_MAX? I haven't seen that used before. I know that if I set lowest=0 instead of using INT MAX, it always says the lowest number is zero. – Session Jul 12 '15 at 02:16
  • newNum is a parameter, which is something you pass into a function in the parentheses when you call it. It exists only in the body of the function. I would recommend you find a tutorial about functions in C to explain this. – Erik Godard Jul 12 '15 at 02:34
  • INT_MAX is a shortcut for the largest value an integer can take on your computer. Think about why setting lowest to 0 wouldn't work. You want to initialize it first to a really big number and then compare any future values to that. – Erik Godard Jul 12 '15 at 02:36