N
N
Nikolai Schneider2019-02-10 10:53:19
C++ / C#
Nikolai Schneider, 2019-02-10 10:53:19

Rate the code. How can goto be replaced?

Wrote a simple program to calculate file download time. Confusing instructions goto. What can replace them? Are they appropriate?

//time_download.c вычисляет время загрузки файлов
#include<stdio.h>
#define bits 8/*количество битов в байте*/
#define PER_IN_SEC 60/*значение секунд в минуте*/
int main(void)
{
  int a, b;//цельночислительные переменные
  float size, time, speed;//вещественные переменные
  printf("Enter file size in megabyts and download speed in megabits:");
  scanf_s("%f %f", &size, &speed);//ввод значений "размер" и "скорость"
mark:while (size > 0.00&&speed>0.00)/*цикл проверки ввода. Метка mark для goto в 25 строке*/
  {
    size = size*bits;//преобразование мегабайт в мегабиты
    time = size / speed;//вычисление времени загрузки
    size = size / bits;//обратное преобразование мегабит в мегабайты
    if (time < 60.00)//проверка результата на временной диапазон
    {
      printf("File size %.2f megabytes will be loaded at speed"
        " %.2f megabits\nin sec behind %.2f seconds\n", size, speed, time);
      printf("Enter new data or press 0 for exit\n");
      scanf_s("%f", &size);//ввод нового значения
      while (size <=0)//проверка ввода
        goto check;//в случае неудачи, переход по метке check на 40 строку
      scanf_s("%f", &speed); goto mark;//в случае успеха, переход по метке(строка 11 на проверку ввода
    }
    else//если значение time больше 60 секунд
    {
      a = time / PER_IN_SEC;//вычисление количества минут
      b = (int)time%PER_IN_SEC;//преобразование float в int для деления по модулю
    }
    printf("File size %.2f megabytes will be loaded at speed"
      " %.2f megabits\nin sec behind %d minuts and %d seconds\n", size, speed, a, b);
    printf("Enter new data or press 0 for exit\n");
    scanf_s("%f", &size);//ввод новых значений
    while (size <= 0)//проверка ввода 
      goto check;//переход на 39 строчку для выхода
    scanf_s("%f", &speed); goto mark;//в случае успеха, переход на строку 11 для проверки значения speed
  }
  check:return 0;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
jcmvbkbc, 2019-02-10
@jcmvbkbc

while (size <=0)//проверка ввода
        goto check;//в случае неудачи, переход по метке check на 40 строку

This
if (size <= 0)
    break;

it's just continue;in the first case and just nothing in the second.

G
GavriKos, 2019-02-10
@GavriKos

Instead of goto check, you can immediately write return 0.
Instead of mark, there are several options. If I understand correctly, you can put the rest of the cycle code into else and that's it.
Try to draw a block diagram, maybe it will be clearer)

R
res2001, 2019-02-10
@res2001

Read something about structured programming.
To better understand structured programming techniques, simply forbid yourself from using goto. You will quickly get used to it when you try to think of a way to do without goto.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question