Home > Mobile >  how to replace goto part in c
how to replace goto part in c

Time:03-07

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.

CodePudding user response:

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);

CodePudding user response:

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;
}

CodePudding user response:

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");
}
  • Related