Thread: Help with Repetition/Switch program

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    37

    Help with Repetition/Switch program

    Yes, it's me again. (hello)

    My program will not stop using -1 even though it is the sentinel value. Please help me locate my mistakes. Otherwise, I believe everything else is correct. We just covered switch programs (and I must use it for this).

    After the user enters the five numbers, I need the menu to show up.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define firstChoice 1
    #define secondChoice 2
    #define thirdChoice 3
    #define fourthChoice 4
    
    /* The program allows a user to enter five numbers and then asks the user to select a choice from a menu. 
    The menu should offer four options. Use a switch function.
    */
    main() {
     
     double number, smallestNumber, largestNumber, sum, average;
     int count = 0; 
     int choice = 0;
     
     smallestNumber = number;
     largestNumber = number;
     
     do {
     printf("Enter five numbers. Type -1 to stop. \n");
     scanf("%lf", &number); 
     }
     
     while (number != -1);
      printf("\nChoose a selection 1-4 from the menu:\n");
      printf("%i. Display the smallest number entered. \n", firstChoice);
      printf("%i. Display the largest number entered. \n", secondChoice);
      printf("%i. Display the sum of the five numbers entered. \n", thirdChoice);
      printf("%i. Display the average of the five numbers entered. \n", fourthChoice);
      scanf("%i", &choice);
     
     switch(choice){
      case 1:
       if(number > smallestNumber && number > largestNumber)
       largestNumber = number;
       else 
       if(number < smallestNumber)
        smallestNumber = number;
       printf("The smallest number is: %lf", smallestNumber);
       break;
      case 2:
       printf("The largest number is: %lf", largestNumber);
       break;
      case 3:
       sum = sum + number;
       count = count + 1;
       printf("The sum is: %lf", sum);
       break;
      case 4:
       average = (double)sum/count;
       printf("The average is: %lf", average);
       break;
      default:
       printf("That is not a valid number. Please try again.");
       break;
     } 
     return 0;
    }

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    First, browse on over to Comparing Floating Point Numbers, 2012 Edition | Random ASCII and scroll down to comparing for equality (in your program 'number' is a floating point and you're using == to compare it to -1).

    Second, why is there a semi-colon at the end of your while (line 25)?

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Are you aware that this while loop does nothing?
    Code:
     while (number != -1);
    And I think case 1 can be more simple.
    Code:
       if(number > smallestNumber && number > largestNumber)
    You shouldn't need to test if number > smallestNumber. The largest number is always bigger than the smallest number, because you would only change the largest number to some other number if it was larger than largestNumber.

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I don't get it
    results;
    Code:
    userx@slackwhere:~/bin
    $ ./temp
    Enter five numbers. Type -1 to stop. 
    1 2 3 4 5 
    Enter five numbers. Type -1 to stop. 
    Enter five numbers. Type -1 to stop. 
    Enter five numbers. Type -1 to stop. 
    Enter five numbers. Type -1 to stop. 
    Enter five numbers. Type -1 to stop. 
    // stops here and waits
    it is doing what you told it to do.

    (studing it a little more. because you're asking to enter 5 numbers, so I do, then I read your code and it looks like you're hanging up here in your do while loop.

    Code:
      
     do {
     printf("Enter five numbers. Type -1 to stop. \n");
     scanf("%lf", &number); 
     }  while (number != -1);
    My program will not stop using -1 even though it is the sentinel value.
    it is not using the -1 it is printing it again and again because of your do while loop is written to keep asking for five numbers, taking in one at a time, so it loops 5 times getting them 5 numbers asked for then waits for input again.

    then when I enter -1 I get this.
    Code:
    -1
    
    Choose a selection 1-4 from the menu:
    1. Display the smallest number entered. 
    2. Display the largest number entered. 
    3. Display the sum of the five numbers entered. 
    4. Display the average of the five numbers entered.
    your logic is off is all. rethink it. rewrite re post if issues.
    Last edited by userxbw; 11-12-2017 at 05:43 PM.

  5. #5
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    Quote Originally Posted by userxbw View Post
    I don't get it
    results;
    Code:
    userx@slackwhere:~/bin
    $ ./temp
    Enter five numbers. Type -1 to stop. 
    1 2 3 4 5 
    Enter five numbers. Type -1 to stop. 
    Enter five numbers. Type -1 to stop. 
    Enter five numbers. Type -1 to stop. 
    Enter five numbers. Type -1 to stop. 
    Enter five numbers. Type -1 to stop. 
    // stops here and waits
    it is doing what you told it to do.

    (studing it a little more. because you're asking to enter 5 numbers, so I do, then I read your code and it looks like you're hanging up here in your do while loop.

    Code:
      
     do {
     printf("Enter five numbers. Type -1 to stop. \n");
     scanf("%lf", &number); 
     }  while (number != -1);
    it is not using the -1 it is printing it again and again because of your do while loop is written to keep asking for five numbers, taking in one at a time, so it loops 5 times getting them 5 numbers asked for then waits for input again.

    then when I enter -1 I get this.
    Code:
    -1
    
    Choose a selection 1-4 from the menu:
    1. Display the smallest number entered. 
    2. Display the largest number entered. 
    3. Display the sum of the five numbers entered. 
    4. Display the average of the five numbers entered.
    your logic is off is all. rethink it. rewrite re post if issues.
    Hi and ty for replying. I intended for it to happen this way. The user enters number and presses enter. They repeat this five times before typing -1 to stop. Then the menu shows up.

  6. #6
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    Quote Originally Posted by whiteflags View Post
    Are you aware that this while loop does nothing?
    Code:
     while (number != -1);
    And I think case 1 can be more simple.
    Code:
       if(number > smallestNumber && number > largestNumber)
    You shouldn't need to test if number > smallestNumber. The largest number is always bigger than the smallest number, because you would only change the largest number to some other number if it was larger than largestNumber.
    Ah right the semicolon! Plus you are right, it does nothing. So I was thinking of removing it for an if statement?
    Code:
    if(number == -1){
     printf("\nChoose a selection 1-4 from the menu:\n");
     printf("%i. Display the smallest number entered. \n", firstChoice);
     printf("%i. Display the largest number entered. \n", secondChoice);
     printf("%i. Display the sum of the five numbers entered. \n", thirdChoice);
     printf("%i. Display the average of the five numbers entered. \n", fourthChoice);
     scanf("%i", &choice);
    else
     printf("Enter five numbers. Type -1 to stop. \n");
     scanf("%lf", &number);
    }
    And about case 1, I do not understand what you mean to simplify it

  7. #7
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    Quote Originally Posted by Hodor View Post
    First, browse on over to Comparing Floating Point Numbers, 2012 Edition | Random ASCII and scroll down to comparing for equality (in your program 'number' is a floating point and you're using == to compare it to -1).

    Second, why is there a semi-colon at the end of your while (line 25)?
    Thank you for the resources.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Is using a sentinel a requirement of your assignment?

    It seems to me that since you always want the user to enter 5 numbers that a for() loop would probably be a better option.

    It also doesn't make much sense to get several numbers into a single variable, perhaps you meant to use an array?


    Code:
    int value[5];
    for(int i = 0; i < 5; ++i)
    {
         printf("Please enter value %d: ", i + 1);
         scanf("%d", &value[i]);
    }

  9. #9
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    Quote Originally Posted by jimblumberg View Post
    Is using a sentinel a requirement of your assignment?

    It seems to me that since you always want the user to enter 5 numbers that a for() loop would probably be a better option.

    It also doesn't make much sense to get several numbers into a single variable, perhaps you meant to use an array?


    Code:
    int value[5];
    for(int i = 0; i < 5; ++i)
    {
         printf("Please enter value %d: ", i + 1);
         scanf("%d", &value[i]);
    }
    Here is the assignment: Youneed to write a menu driven program.
    Theprogram allows a user to enter five numbers and then asks the user to select achoice from a menu. The menu should offer following four options –

    1. Display thesmallest number entered
    2. Displaythe largest number entered
    3. Displaythe sum of the five numbers entered
    4. Displaythe average of the five numbers entered

    Youmust use a "switch"statement in your code to determine what action to take. Provide an errormessage if an invalid choice is entered.
    Runthe program five times, once with each option and once with an invalid option.Each run is to use the following set of data:
    18,21, 17, 44, 9.

    -

    I see what you are saying. No, a sentinel value is not required. We haven't learned the int value[5] part. (I get this error too 'C:\Users\hoyte\Desktop\School Stuff\Programming\dev c++\main.c [Error] 'for' loop initial declarations are only allowed in C99 or C11 mode")

    So I am a bit confused. Should I use a for loop instead of a while loop>?

  10. #10
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    Hello quick update. I decided to re-haul my code. I now have trouble finding how to calculate the smallest and largest number from stored variables.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define firstChoice 1
    #define secondChoice 2
    #define thirdChoice 3
    #define fourthChoice 4
    
    /* The program allows a user to enter five numbers and then asks the user to select a choice from a menu. 
    The menu should offer four options. Use a switch function.
    */
    main() {
     
     int firstNumber, secondNumber, thirdNumber, sum; 
     int fourthNumber, fifthNumber, choice, count, average;
     
     printf("Enter the first number: ");
     scanf("%i", &firstNumber);
     printf("Enter the second number: ");
     scanf("%i", &secondNumber);
     printf("Enter the third number: ");
     scanf("%i", &thirdNumber);
     printf("Enter the fourth number: ");
     scanf("%i", &fourthNumber);
     printf("Enter the last number: ");
     scanf("%i", &fifthNumber);
     
     printf("\nSelect the number of choice from the menu: \n");
     printf("%i. Display the smallest number entered. \n", firstChoice);
     printf("%i. Display the largest number entered. \n", secondChoice);
     printf("%i. Display the sum of the five numbers entered. \n", thirdChoice);
     printf("%i. Display the average of the five numbers entered. \n", fourthChoice);
     scanf("%i", &choice);
     sum = firstNumber + secondNumber + thirdNumber + fourthNumber + fifthNumber;
     average = sum / 5;
     
     switch(choice) {
      case 1:
       printf("The smallest number entered is: ");
       break;
      case 2:
       printf("The largest number entered is: ");
       break;
      case 3:
       printf("The sum of the five numbers is: %i", sum);
       break;
      case 4:
       printf("The average of the five numbers entered is: %i", average);
       break;
      default:
       printf("That is an invalid number. Please try again.");
     }
     return 0;
    }

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    (I get this error too 'C:\Users\hoyte\Desktop\School Stuff\Programming\dev c++\main.c [Error] 'for' loop initial declarations are only allowed in C99 or C11 mode")
    You really should be using either C99 or preferably C11 instead of the outdated C90. I would recommend that you check the documentation for your IDE and properly configure the IDE to compile for C11.

    Since you state you haven't covered arrays then you really don't need an array or any loop. Instead just find the minimum and maximum values where you're getting the user input. Get the first two values, then "compute" the minimum and maximum. Get the next value, then "compute the minimum and maximum using the results of previous computations, repeat for every "new" value you retrieve.

  12. #12
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    Quote Originally Posted by jimblumberg View Post
    You really should be using either C99 or preferably C11 instead of the outdated C90. I would recommend that you check the documentation for your IDE and properly configure the IDE to compile for C11.

    Since you state you haven't covered arrays then you really don't need an array or any loop. Instead just find the minimum and maximum values where you're getting the user input. Get the first two values, then "compute" the minimum and maximum. Get the next value, then "compute the minimum and maximum using the results of previous computations, repeat for every "new" value you retrieve.
    That did it. Thank you.

    How does one change the documentation to C11 or C9? I am using Dev C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-19-2014, 04:06 AM
  2. Probability of repetition
    By quzah in forum Game Programming
    Replies: 3
    Last Post: 08-31-2011, 11:50 PM
  3. Looping/Repetition help
    By ArcadeEdge in forum C Programming
    Replies: 4
    Last Post: 09-17-2010, 07:01 PM
  4. Need help for repetition method!
    By rangerys in forum C Programming
    Replies: 9
    Last Post: 09-09-2010, 08:26 AM
  5. Repetition in do{}while
    By Beast() in forum C Programming
    Replies: 25
    Last Post: 06-16-2004, 10:47 PM

Tags for this Thread