Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This program has two parts to it. The first part was prompting the user to enter a min and a max and generate random numbers between them. I'm having trouble with the second part where I display three options for the user and I am required to use switch statement and do while loop.

What I have tried:

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


int main(){

  //declare variables
  int max, min, amount, num; 
  int sum = 0; 
  char opt;
  float mean;

  //prompt user to enter the minimum possible value 
  printf("Enter the minimum random number to be generated: "); 
  scanf("%d",&min);

  //prompt user to enter the maximum possibe value
  printf("Enter the maximum random number to be generated: ");
  scanf("%d",&max);
    
  //promt user to enter the number of random numbers
  printf("Enter the number of random numbers: ");
  scanf("%d",&amount);
  
  //time function to generate random numbers
  srand(time(NULL));
  
  printf("----------\n");

  //prints table with the serial number and the value of the generated random number
  for(int i=001;i<=amount;i++){

    num = (rand() % (max - min + 1)) + min; //generates random numbers
      
    if(i<=9){ //prints the first number to the ninth number
      printf("|00");   
      printf("%d | %d|\n",i,num);
    }
    
    else{ //prints numbers after the ninth number
      printf("|0");
      printf("%d | %d|\n",i,num); 
    }
  }
    
  printf("----------");

  //performs operations multiple times
  do{
    //displays options
    printf("\nSelect the option\n");
    printf("a - mean of all random numbers\n");
    printf("b - sum of all random numbers\n");
    printf("c - Exit\n");
    scanf("%c",&opt);
      
    //switch case for multiple choices
    switch(opt){
            
      case 'a': 
        for(int i=0;i<amount;i++){
          sum += num;
        }
        mean=sum/amount;
        printf("Mean is %f\n", mean);
        break;
            
      case 'b': 
        for(int i=0;i<amount;i++){
          sum += num;
        }
        printf("Sum is %d\n", sum);
        break;
          
      case 'c': 
        printf("Thank you!\n");
        break;
          
      default:
        printf("No such option\n");
    }
        
  }while(opt);
Posted
Updated 5-Nov-22 2:01am

Your problem is that you are using scanf(). In the case of scanf("%d", &var), scanf will read in all the ASCII chars that could make up an integer(including +/-), and stops reading at the first non-number character. In this case that would be the line-ending newline character '\n'. So now, when you do a scanf("%c", &opt), scanf() sees the newline, and puts that in the opt variable. This is further confused by the fact that for a non-character format (e.g. %d, %f, %s), scanf skips over leading whitespace (' ', '\t', '\n', etc), but does not do that for %c.

But see what happens if you accidentally hit the 'q' instead of hitting the 1 key at the begining of your program. What will happen is that all the scanf("%d", &var) calls will examine the input, see that its not whitespace or a valid char for a number, so push it back. Eventually your scanf("%c", &opt) will be called, and the 'q' character will be read in an put into the opt variable. That's not a good design. What you should be doing is reading a line of text from the user, then parse it to see if its valid input (or perhaps valid input can be extracted from it), and then re-prompt as necessary on bad input.
 
Share this answer
 
Comments
Richard MacCutchan 4-Nov-22 4:49am    
I never understood why scanf was ever created. I am sure it causes more problems than it has ever solved.
You read in your option with
C
scanf("%c", &opt);

On the first call, scanf reads in an end-of-line character (\n), which is not a valid option.

The simplest solution would be to ignore end-of-line characters
C
case '\n':
   break;

The do - while loop should actually terminate after 'c', which it does not.

Furthermore it is noticeable that you do not remember the calculated random values and therefore neither the sum nor the mean value later.

This code is very inefficient and the if-query is superfluous:
C
for (int i = 001; i <= amount; i++) {

   num = (rand() % (max - min + 1)) + min; //generates random numbers

   if (i <= 9) { //prints the first number to the ninth number
      printf("|00");
      printf("%d | %d|\n", i, num);
   }

   else { //prints numbers after the ninth number
      printf("|0");
      printf("%d | %d|\n", i, num);
   }
}

Here a simple
C
printf("%03d | %3d|\n", i, num);

would produce a much better and simpler output.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900