Omar K. Rostom
Omar K. Rostom

Reputation: 111

Simple Calculator and while loop

Hi I am wrinting a simple calulator using while(true) and its working except that, when the loop ends and begins again, it repeats the first line twice, does anyone have a solution for this ? thanks in advance ...

#include <stdio.h>
#include <stdlib.h>

int main()
{
char a,ch;
int x,y,i,n,e,s;
while(1) {
    printf("\nEnter the operation:");
    scanf("%c",&a);
    e=a;
    if (e=='+') {
        printf("\nEnter the first integer:");
        scanf("%d",&x);
        printf("\nEnter the second integer:");
        scanf("%d",&y);
        s=x+y;
        printf("\nThe result of %d+%d is:%d\n",x,y,s);
    } else {
        if (e=='-') {
        printf("\nEnter the first integer:");
        scanf("%d",&x);
        printf("\nEnter the second integer:");
        scanf("%d",&y);
        s=x-y;
        printf("\nThe result of %d-%d is:%d\n",x,y,s);
    } else {
        if (e=='*') {
        printf("\nEnter the first integer:");
        scanf("%d",&x);
        printf("\nEnter the second integer:");
        scanf("%d",&y);
        s=x*y;
        printf("\nThe result of %dx%d is:%d\n",x,y,s);
    } else {
        if (e=='/') {
        printf("\nEnter the first integer:");
        scanf("%d",&x);
        printf("\nEnter the second integer:");
        scanf("%d",&y);
        s=x/y;
        printf("\nThe result of %d/%d is:%d\n",x,y,s);
    } else {
        if (e=='%') {
        printf("\nEnter the first integer:");
        scanf("%d",&x);
        printf("\nEnter the second integer:");
        scanf("%d",&y);
        s=x%y;
        printf("\nThe result of %d%%d is:%d\n",x,y,s);
    } else {
        if (e=='^') {
        printf("\nEnter the first integer:");
        scanf("%d",&x);
        printf("\nEnter the second integer:");
        scanf("%d",&y);
        s=pow(x,y);
        printf("\nThe result of %d^%d is:%d\n",x,y,s);
    }}}}}}
}}

Upvotes: 1

Views: 4677

Answers (4)

VoidPointer
VoidPointer

Reputation: 3097

This should be due to scanf. Leave-out scanf and try with getchar() to receive operation command. Its worth a try.

Also use switch..case for this kind of tasks. Include 'default' clause also to notify the user..

Upvotes: 1

Whit Kemmey
Whit Kemmey

Reputation: 2230

Currently, if you provide anything to the loop that you are not expecting, it will just execute again, printing the initial prompt multiple times. Here's a solution with minimal changes to your program that will only reprint the initial prompt after successfully completing one of the calculations that you handle:

#include <stdio.h>
#include <stdlib.h>

int main()
{
char a,ch;
int x,y,i,n,e,s;
int new_operation = 1;
while(1) {
    if (new_operation) {
        printf("\nEnter the operation:");
        new_operation = 0;
    }
    scanf("%c",&a);
    e=a;
    if (e=='+') {
        printf("\nEnter the first integer:");
        scanf("%d",&x);
        printf("\nEnter the second integer:");
        scanf("%d",&y);
        s=x+y;
        printf("\nThe result of %d+%d is:%d\n",x,y,s);
        new_operation = 1;
    } else {
        if (e=='-') {
        printf("\nEnter the first integer:");
        scanf("%d",&x);
        printf("\nEnter the second integer:");
        scanf("%d",&y);
        s=x-y;
        printf("\nThe result of %d-%d is:%d\n",x,y,s);
        new_operation = 1;
    } else {
        if (e=='*') {
        printf("\nEnter the first integer:");
        scanf("%d",&x);
        printf("\nEnter the second integer:");
        scanf("%d",&y);
        s=x*y;
        printf("\nThe result of %dx%d is:%d\n",x,y,s);
        new_operation = 1;
    } else {
        if (e=='/') {
        printf("\nEnter the first integer:");
        scanf("%d",&x);
        printf("\nEnter the second integer:");
        scanf("%d",&y);
        s=x/y;
        printf("\nThe result of %d/%d is:%d\n",x,y,s);
        new_operation = 1;
    } else {
        if (e=='%') {
        printf("\nEnter the first integer:");
        scanf("%d",&x);
        printf("\nEnter the second integer:");
        scanf("%d",&y);
        s=x%y;
        printf("\nThe result of %d%%d is:%d\n",x,y,s);
        new_operation = 1;
    } else {
        if (e=='^') {
        printf("\nEnter the first integer:");
        scanf("%d",&x);
        printf("\nEnter the second integer:");
        scanf("%d",&y);
        s=pow(x,y);
        printf("\nThe result of %d^%d is:%d\n",x,y,s);
        new_operation = 1;
    }}}}}}
}}

Notice the changes include a new variable declaration, the if statement around the initial prompt, and resetting the new_operation variable after each operation completes.

This will handle the extraneous newline and any other unhandled response to the initial prompt.

Upvotes: 0

Thilina Chamath Hewagama
Thilina Chamath Hewagama

Reputation: 9040

When you read keyboard input with scanf(), the input is read after enter is pressed but the newline generated by the enter key is not consumed by the call to scanf(). That means the next time you read from standard input there will be a newline waiting for you (which will make the next scanf() call return instantly with no data).

To avoid this, you can modify your code to something like:

while(1) {
    printf("\nEnter the operation:");
    scanf("%c%*c", &a);

Upvotes: 1

P.P
P.P

Reputation: 121377

When you input char using %c, there's a newline char left in the input stream. Hence it doesn't ask for input as the newline character left in the input stream is consumed for the subsequent iteration.

Add a whitespace in the in the format string so that scanf() ignores newline (any whitesapce) characters left.

Change:

scanf("%c",&a);

to:

scanf(" %c",&a);

This way scanf() will ignore all whitespaces.

Upvotes: 3

Related Questions