Hi there i have wrote a program ,The user can only Choose the letter S for square or R for rectangle..
i have done every thing as you can see ... the only problem that am having is if the user puts 's' small letter also 'r' small letter ..
So how can i fix this problem?

#include <stdio.h>
int main (void)
{
 
 
 char choice ;
 int a;
 int b;
 int area , perimeter ;
 
 
 
 printf("Enter 'S' for Square or 'R' for rectangle :");

 scanf(" %c",&choice);   

 

 
 if (choice == 'S')
 {
 printf("enter the length of the side of the sqaure:");
 
 
 scanf(" %d",&a);
 


 area = a * a;
 printf("Area = %d \n",area);           
 
 perimeter = a * 4;
 printf ("perimeter = %d \n",perimeter);
 }

else

if (choice == 'R')
{
   printf("enter the length and the width of the rectangle:");

 
 scanf("%d%d",&a,&b);

 
 area = a * b;
 printf("Area = %d \n",area);           
 
 perimeter = area + 4;
 printf ("perimeter = %d \n",perimeter);
    }
 else 
 printf("You can only choose 'S' for squrae or 'R' for rectangle\n");
     system("system pause");
return 0 ;
}

Recommended Answers

All 5 Replies

Use the toupper() function.

Hello nory22,
You just have to change the condition in the if statement. For example

if(choice=='R' || choice=='r')
{ 
}

Use the toupper() function.

thanks but can you show me how and where ??

Hello nory22,
You just have to change the condition in the if statement. For example

if(choice=='R' || choice=='r')
{ 
}

Oh thank you very much you saved my live

Welcome. As gerard4143 said you can use toupper function also.
choice=toupper(choice) would change 'r' to 'R' (if user enters 'r').

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.