0

I'm making a program for addition, subtraction, division and multiplication with the help of switch operator. Here I want to make a program that will stop only when user entered any other character instead of 'Y'. The problem is: when I run the program, after the result of respective case it only print 'do you want to continue' and after that the program stops. Please help! Thank you in advance! :)

#include<stdio.h>
int main() {
    int n,a,b;
    char answer;
    while(1)  {
        printf("enter \n1.Addition \n2.subtraction \n3.division \n4.multiplication\t");
        scanf("%d",&n);
        switch(n)  {
            case 1:   {
                printf("enter value of a and b:\t");
                scanf("%d %d",&a,&b);
                printf("addition of %d+%d is %d",a,b,a+b);
                goto ans;
            }
            case 2:  {
                printf("enter the value of a and b:\t");
                scanf("%d %d",&a,&b);
                printf("Subtraction of %d-%d is %d",a,b,a-b);
                goto ans;
            }
            case 3:  {
                printf("enter the value of a and b:\t");
                scanf("%d %d",&a,&b);
                printf("Division of %*% is %d",a,b,a/b);
                goto ans;
            }
            case 4:  {
                printf("enter the value of a nad b:\t");
                scanf("%d %d",&a,&b);
                printf("Multiplication of %d*%d is %d",a,b,a*b);
                goto ans;
            }
            default:  printf("invalid value!");
            goto ans;
        }
        ans:  {
            printf("Do you want to continue?(Y/N):\t");
            scanf("%c",&answer);
            if(answer=='Y')  continue;
            else  break;
        }
    }
    return 0;
}
melpomene
  • 84,125
  • 8
  • 85
  • 148
  • I edited the original formatting using http://prettyprinter.de/module.php?name=PrettyPrinter – Antonio Sep 30 '14 at 09:58
  • 4
    You should not use `goto`. Try to use a `while` loop instead or something else to check your condition. – pzaenger Sep 30 '14 at 09:59
  • Your code is showing warning like "30:6: warning: conversion lacks type at end of format [-Wformat]" – Chandru Sep 30 '14 at 10:10

2 Answers2

3

Instead of this

scanf("%c",&answer);

Try to use this:

scanf(" %c",&answer);

The blank in the format string eats up white space, including newlines, and reads the first non-blank character.

fycth
  • 3,410
  • 25
  • 37
  • thanks alot! :) sir can u please explain about this line "The blank in the format string eats up white space,including newlines,and reads the first non-blank character".It will be really great of u! :) – Lisha GuPta Sep 30 '14 at 11:36
  • When you enter a symbol there is also a new line symbol at the end. In your case it happens that the scanf receives this new line symbol from the previous input (previous scanf command). Adding space symbol before the format string, you ask scanf to skip at the beginning all white space and newline symbols, if any. – fycth Sep 30 '14 at 12:30
  • awesome thanyou so much! – Lisha GuPta Sep 30 '14 at 12:36
  • instead of blank i had used fflush(stdin) and it work and also i had used \n before the format string and it also work .what happen by these two ..? – Lisha GuPta Sep 30 '14 at 12:40
2

You are victim of scanf handling of new lines: scanf() leaves the new line char in buffer?

In particular:
The scanf function removes whitespace automatically before trying to parse things other than characters. The character formats (primarily %c) are the exception (they don't remove whitespaces), and that's your situation in the last scanf.

Community
  • 1
  • 1
Antonio
  • 19,451
  • 13
  • 99
  • 197