-2

I was asked to write a program about a traveling man. The direction of the program:


  1. Suppose a man (say, A) stands at (0, 0) and waits for the user to give him the direction and distance to go.

  2. The user may enter N E W S for north, east, west, south, and any value for the distance.

  3. When the user enters 0 as direction, stop and print out the location where the man stopped

My Code:

#include <stdio.h>

int main() {
float x=0,y=0;
char dir;
float mile;

while(1){

    printf("Enter input direction as N,S,E,W (0 to exit): ");
    scanf("%c",&dir); 

    if(dir == '0')
        break;
    if(dir != 'N' && dir != 'S' && dir != 'E' && dir != 'W'){

        printf("Invalid Direction, re-enter \n");
        continue;
    }

    printf("Input mile in %c dir: ",dir);
    scanf("%f",&mile);
    if(dir == 'N'){
        y+=mile;
    }
    else if(dir == 'S'){
        y-=mile;
    }
    else if(dir == 'E'){
        x+=mile;
    }
    else if(dir == 'W'){
        x-=mile;
    }
}
 printf("\nCurrent position of A: (%4.2f,%4.2f)\n",x,y);
return 0;
}

But when I run the program after the first two inputs, it prints invalid outputs in the valid inputs.

output: enter image description here

melpomene
  • 84,125
  • 8
  • 85
  • 148

2 Answers2

0

Its a classic mistake of OR vs AND. It should be

if(dir != 'N' && dir != 'S' && dir != 'E' && dir != 'W') . 

For eg if a user inputs 'N', in that case the if statement will still return success because its != 'S' and the if condition has a OR.

Edit: (Following @usr2564301 's advice.)

Add a space before scanf input %c.

scanf(" %c",&dir);

Check link for more explanation.

pOrinG
  • 896
  • 3
  • 13
  • 27
  • Hmm.. You just edited your post to remove *||* and added *&&* ... Now I look like a fool.. lol – pOrinG Nov 10 '18 at 13:46
  • The revisions show it has been that all the time. But you could remove that from your answer so only my advice remains. – Jongware Nov 10 '18 at 14:06
  • 1
    @usr2564301 God yeah!! Anyway when he put the question, I had copied his source code into my editor to check. There was an OR in it.. I even double checked before commenting on the post and then later answering it .. – pOrinG Nov 10 '18 at 14:09
  • @singlepiece000 Kindly do let me know if I am delusional. You started with a code which had || (OR) operator in if condition instead of && (AND) right ? – pOrinG Nov 10 '18 at 14:11
0

So, I found your problem: When you were pressing "Enter" after you entered the "miles", that "Enter" got registered as input, as a '\n' character. I have a solution here for you, but if you don't prefer it, just make a special case in which you ignore the '\n' character. Something like else if (dir == '\n') continue;. I suspected this might be the problem but I confirmed it once I printed that "invalid character".

int main() 
{

    float x = 0, y = 0;
    char dir;
    float mile;

    do 
    {

        printf("Enter input direction as N,S,E,W (0 to exit): ");
        scanf("%c", &dir);

        switch (dir)
        {

        case 'N':
            printf("Input mile in %c dir: ", dir);
            scanf("%f", &mile);
            y += mile;
            break;
        case 'S':
            printf("Input mile in %c dir: ", dir);
            scanf("%f", &mile);
            y -= mile;
            break;
        case 'E':
            printf("Input mile in %c dir: ", dir);
            scanf("%f", &mile);
            x += mile;
            break;
        case 'W':
            printf("Input mile in %c dir: ", dir);
            scanf("%f", &mile);
            x -= mile;
            break;
        case '\n':
            break;
        case '0':
            break;
        default:
            printf("Invalid Direction, re-enter; You entered: %c \n", dir);
            break;
        }

    }while (dir != '0');

    printf("\nCurrent position of A: (%4.2f, %4.2f)\n", x, y);

    return 0;
}
Rakirnd
  • 81
  • 4