dongja
dongja

Reputation: 1

how to replace goto part in c

First, enter the coordinates of the vertex of the square. and each coordinate can not exceed 1000. Second, enter the current location of the coordinates. location of the coordinate cannot exceed square. and i want to find the minimum distance from coordinates to square. This is the content that i want to make. So i made the code below.

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

void min_comparison(int a, int b, int c, int d); 

typedef struct coordinate {
    unsigned int x; unsigned int y; 
    unsigned int w; unsigned int h; 
}coord;

int main()
{
    int nomi1;
    int nomi2;

    coord cod;

    presentcoord:
    printf("enter the coordinates of the vertex of the square: ");
    scanf("%d %d", &cod.x, &cod.y);

    if (cod.x >= 1001 || cod.y >= 1001)
    {
        printf("The vertex coordinates cannot exceed 1000.\n");
        goto presentcoord;
    }

    presentloca:
    printf("enter the current location of the coordinates: ");
    scanf("%d %d", &cod.w, &cod.h);

    if (cod.w >= cod.x || cod.h >= cod.y)
    {
        printf("location of the coordinates can't excced %d, %d.\n",cod.x-1,cod.y-1);
        goto presentloca;
    }
    printf("\n");

    nomi1 = cod.x - cod.w;
    nomi2 = cod.y - cod.h;

    min_comparison(cod.x, cod.y, nomi1, nomi2);
}

void min_comparison(int a,int b,int c,int d) 
{
    int min; int min1; int result;
    if (a > c)
        min = c;
    else min = a;

    if (b > d)
        min1 = d;
    else min1 = b;

    if (min > min1)
        result = min1;
    else result = min;

    printf("minimum distance from coordinates to square is %d.\n", result);

    return 0;
}

I looked it up on Google and it said "goto" is a bad code.
so i want to know how i replace the "goto" part of my code.

and English is not my first language. So point out the wrong English expression, too.
I'd appreciate it if you let me know.

Upvotes: 0

Views: 374

Answers (4)

Neil
Neil

Reputation: 1922

This answer is kind of a brief code review, but I think it is welcomed by the OP. I'm not going to touch on the loop, which is covered by the other answers.

Initially, I was under the impression that coord is more of an axis-aligned box, but saw that it is actually two separate coördinates. I would expect them to be one point. This is working with two-dimensions,

struct coord { unsigned x, y; };

Then one could write,

unsigned minimum_distance(const struct coord point, const struct coord enclosing_square); 

min_comparison or minimum_distance should ideally be self-contained (nomi1 = cod.x - cod.w should be in the function) and return the minimum distance, not print it. If you want signed distances (vectors?), then I think a signed coödinate would be good.

One has to check the return values of scanf. Here, the return value is: EOF, 0, 1, or 2. Even just,

if(scanf("%d %d", &cod.x, &cod.y) != 2) exit(EXIT_FAILURE);

would be much better when faced with unexpected inputs.

Upvotes: 0

Lundin
Lundin

Reputation: 213740

The do-while version proposed by Gerhardh is fine. Another more compact alternative is this:

while(1)
{
  printf("enter the coordinates of the vertex of the square: ");
  scanf("%d %d", &cod.x, &cod.y);

  if (cod.x <= 1000 && cod.y <= 1000)
    break; // stop if input ok

  printf("The vertex coordinates cannot exceed 1000.\n");
}

Upvotes: 2

paladin
paladin

Reputation: 790

This should work, (not tested). continue will act like a goto back to the beginning of the while loop and break will act like a goto to the end of the while loop.

while(1){
    printf("enter the coordinates of the vertex of the square: ");
    scanf("%d %d", &cod.x, &cod.y);

    if (cod.x >= 1001 || cod.y >= 1001)
    {
        printf("The vertex coordinates cannot exceed 1000.\n");
        continue;
    }

    break;
}

Upvotes: 0

Gerhardh
Gerhardh

Reputation: 12404

You can easily transform your code. This part

    presentcoord:
    printf("enter the coordinates of the vertex of the square: ");
    scanf("%d %d", &cod.x, &cod.y);

    if (cod.x >= 1001 || cod.y >= 1001)
    {
        printf("The vertex coordinates cannot exceed 1000.\n");
        goto presentcoord;
    }

can be reduced to this generic form:

label:
   <do something>
   <Check if we are done>
   <If not, goto label>

That can be transformed to a generic loop:

   bool done = false;
   do 
   {
      <do something>
      <check if we are done>
      <if yes: done=true;>
      <else: Print message>
   }while (!done);

In your case this would be

    bool done = false;
    do 
    {
      printf("enter the coordinates of the vertex of the square: ");
      scanf("%d %d", &cod.x, &cod.y);

      if (cod.x >= 1001 || cod.y >= 1001)
      {
        printf("The vertex coordinates cannot exceed 1000.\n");
      }
      else 
      {
        done = true;
      }
    } while (!done);

Upvotes: 3

Related Questions