Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

why this 3rd case in following program not execute properly?

0 votes
asked Jul 11, 2018 by anonymous
#include <stdio.h>

int main(void) {

int no1,no2,result;

float No1,No2,Result;

char a,b,c;

int choice;

printf("\n.............Menu...........");

printf("\n1.Addition of integers\n2.Addition of floats\n3.Addtion of characters");

printf("\nEnter your choice....");

scanf("%d",&choice);

switch(choice)

{

    case 1:

    printf("\nEnter the nos=");

    scanf("%d %d",&no1,&no2);

    result=a+b;

    printf("\nResult=%d",result);

    break;

    

    case 2:

    printf("\nEnter the nos=");

    scanf("%f %f",&No1,&No2);

    Result=No1+No2;

    printf("\nResult=%f",Result);

    break;

    

    case 3:

    printf("\nEnter the characters=");

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

    c=a+b;

    printf("\nResult=%c",c);

    break;

    

    default:

    printf("\nYou Entered wrong choice.....");

    break;

    

}

return 0;

}

3 Answers

0 votes
answered Jul 12, 2018 by sajid ali (140 points)
because character are not add
commented Jul 12, 2018 by anonymous
No,Addtion characters is possible.following program runs

void main()
{
char a,b,c;
printf("\nEnter the characters=");
scanf("%c %c",&a,&b);
c=a+b;
printf("\nRsult=%c",c);
}
0 votes
answered Jul 13, 2018 by sambhavi devi tadi
addition characters is not the reason for reading the input  every time your clearing the buffer by using fpurge function present in stdlib.h.

scanf("%d",&choice);
__fpurge(stdin);
switch(choice)
commented Jul 17, 2018 by anonymous
edited Jul 17, 2018
Yes, I implement fflush function & it works. Thanks for your suggestions.
0 votes
answered Jul 13, 2018 by Shushrut Gupta (150 points)
Case 3 is wrong, addition of char isnt possible directly, you can add then by adding their ascii values
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...