Thread: while

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    15

    while

    Why does it read a character if i didnt put anything in after printing a shape for the first time. i need to to stop until i pick another shap
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define HIGH 50
    #define LOW 0
    
    int getInput(int userInput);
    void choosePic(int userInput);
    void inRecPar(int *pinLength,
                   int *pinWidth);
    void inSqTri(int *pinSide);
    void Square(int Side);
    void Triangle(int Side);
    void Rectangle(int Length,
                   int Width);
    void Parall(int Length,
                int Width);
    
    int main()
    {
        int userInput;
        int i = 1;
        
        while (i == 1)
      {
        userInput = getInput(userInput);
        if(userInput == 'X')
        i++;
        choosePic(userInput);
      }
      
        system ("pause");
        return 0;
    }
    
    void Rectangle(int Length, int Width)
    {
         int inLength;
         int inWidth;
         int w, h;
         
         inRecPar(&inLength, &inWidth);  
         for(w = 0;w < inLength;++w)
     {
         for(h = 0;h < inWidth;++h)
         putchar('*');
         putchar('\n');
     }
    }
    
    void Parall(int Length, int Width)
    {
         int inLength;
         int inWidth;
         int i;
         int j;
         
         inRecPar(&inLength, &inWidth);
         for(i = 0; i < inLength; i++) 
      {
         printf("%*s",i,"");
         for(j = 0 ; j < inWidth; j++)
         putchar('*');
         putchar('\n');
      }
    }
    void inRecPar(int *pinLength, int *pinWidth)
    {
         printf("Enter length and width (sep. by a space): ");
         scanf("%d %d", pinLength, pinWidth);
         
         while(*pinLength <= LOW || *pinWidth > HIGH || *pinLength > HIGH || *pinWidth <= LOW)
      {
         printf("Number must be between 0 and 50! Enter again: ");
         scanf("%d %d", pinLength, pinWidth);
      }
    }
    
    void Triangle(int Side)
    {
         int inSide;
         int i;
         int j;
         int num_stars;
         
         inSqTri(&inSide);
         num_stars = (inSide *2) -1;
         for(int i = inSide; i >0; i-=1)	
      {		
          for (int j = i; j > 0; j--)
         	printf("*");		
        	printf("\n");
      }
    }
    
    void Square(int Side)
    {
         int inSide;
         int i;
         int j;
         
         inSqTri(&inSide);
         for (i = 0; i < inSide; ++i)
     {
         for (j = 0; j < inSide; ++j)
         putchar('*');
         putchar('\n');
      }
    }
    
    void inSqTri(int *pinSide)
    {
    
         printf("Enter side: ");
         scanf("%d", pinSide);
         
         while(*pinSide <= LOW || *pinSide > HIGH)
      {
         printf("Number must be between 0 and 50! Enter again: ");
         scanf("%d", pinSide);
      }
    }
    
    void choosePic(int userInput)
    {
         int Length;
         int Width;
         int Side;
         
         switch(userInput)
      {
         case 1: Parall(Length, Width); break;
         case 2: Rectangle(Length, Width); break;
         case 3: Square(Side); break;
         case 4: Triangle(Side); break;
         case 5: printf("Exiting Program\n"); break;
         case 6: printf("Invalid Selection\n"); break;
      }
    }
    
    int getInput(int userInput)
    {
         char whichShape;
         
         printf("Menu of Choices\n");
         printf("P: Display a parallelogram\n");
         printf("R: Display a rectangle\n");
         printf("S: Display a square\n");
         printf("T: Display a right-trangle\n");
         printf("X: Exit program\n");
         printf("Enter your choice:");
         scanf("%c", &whichShape);
         whichShape = toupper(whichShape);
         if (whichShape == 'P')
     {
         userInput = 1;
     }
         else if (whichShape == 'R')
     {
         userInput = 2;
     }
         else if (whichShape == 'S')
     {
         userInput = 3;
     }
         else if (whichShape == 'T')
     {
         userInput = 4;
     }
         else if (whichShape == 'X')
     {
         userInput = 5;
     }
         else if (whichShape != 'R' || whichShape != 'S' || whichShape != 'T' || whichShape != 'X')
     {
         userInput = 6;
     }
     return userInput;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try this....
    Code:
         scanf(" %c", &whichShape);
    Note the space before the %c....

Popular pages Recent additions subscribe to a feed