Click here to Skip to main content
15,908,674 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Objective-C
[#include <stdio.h>
#include <stdlib.h>
#define MEAN(a,b) (float)(a+b)/2
#define ABS(n) (n>0?n:n*-1)
#define TOLOWER(x) (x+32)
int main()
{

    int i,a,b,j;
    char k;
    printf("Enter 1 for MEAN\n2 for ABS\n3 for TOLOWER\n");
    scanf("%d",&i);
    switch(i)
    {
        case 1: printf("Enter a nd b\n");
                scanf("%d %d",&a,&b);
                printf("%f\n",MEAN(a,b));
                break;
        case 2: printf("Enter a no to find abs\n");
                scanf("%d",&j);
                printf("%d\n",ABS(j));
                break;
        case 3: printf("Enter upper to lower\n");
                scanf("%c",&k);
                printf("%c\n",TOLOWER(k));
                break;
        default: printf("Hey wat u dng ? Its a wrong input\n");
                break;
    }
    return 0;
}
]


What I have tried:

Without Switch case Statement I can execute easily, but why not with switch case ?
on entering 3rd case automatically character * is coming. Why So ? I am using code blocks.
Posted
Updated 5-Apr-16 8:32am
v2

Probably because you are reading a single character from the input, and it's the "leftovers" from the '3' you entered:
C++
case 3: printf("Enter upper to lower\n");
        scanf("%c",&k);
        printf("%c\n",TOLOWER(k));
Probably what you want to do is read a string of characters and select the first one that is alphabetic.

Plus: what happens if the user enters 'g' instead of 'G'? Does your TOLOWER macro work?
 
Share this answer
 
Comments
Anupkumar_vj 5-Apr-16 12:37pm    
It's just entering single Upper-case alphabet that will be converted to lower case alphabet. If g entered g+32= 135, which is a symbol appears.
OriginalGriff 5-Apr-16 13:55pm    
So...that's a no then! :laugh:
C++
#include <stdio.h>
#include <stdlib.h>
#define MEAN(a,b) (float)(a+b)/2#define ABS(n) (n>0?n:n*-1)
#define TOLOWER(x) (x+32)
int main()
{

    int i,a,b,j;
    char k;
    printf("Enter 1 for MEAN\n2 for ABS\n3 for TOLOWER\n");
    scanf("%d",&i);
    switch(i)
    {
        case 1: printf("Enter a nd b\n");
                scanf("%d %d",&a,&b);
                printf("%f\n",MEAN(a,b));
                break;
        case 2: printf("Enter a no to find abs\n");
                scanf("%d",&j);
                printf("%d\n",ABS(j));
                break;
        case 3: printf("Enter upper to lower\n");

                scanf("\n%c",&k);      //add a new line here '\n'

                printf("%c", TOLOWER(k));
                break;
        default: printf("Hey wat u dng ? Its a wrong input\n");
                break;
    }
    return 0;
}
 
Share this answer
 
v4
Comments
Member 12378355we 5-Apr-16 14:56pm    
or use a second space before second scanf in ur code.
scanf("%c",&ch);
scanf(" %c",&ch);
Because scanf creates a newline and this trailing newline caused second scanf to skip...I think its clear now :0:0))

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