-1
int regMedicine(Medicine lakemedel[], int antalMedicine)
{
    char namnMed[WORDLENGTH], tmp[WORDLENGTH], test[WORDLENGTH] = "ja";
    int storlek, saldo;

    while(strcmp(test, "ja") == 0)
    {
        printf("Ange namn: ");
        gets(namnMed);
        printf("Ange storlek: ");
        while(tmp!=0){ ///////////////////////
        gets(tmp);          //////////////////////////////////////////
        }               ///////////////////////////////////
        storlek = atoi(tmp); //atoi - converts string to int
        printf("Ange saldo: ");
        gets(tmp);
        saldo = atoi(tmp); //atoi - converts string to int
        lakemedel[antalMedicine] = createMedicine(namnMed, storlek, saldo);
        antalMedicine++;
        printf("Vill du fortsatta registrera (ja) eller (nej): ");
        gets(test);
    }
    return antalMedicine;
}

I am writing a program where I use a FILE to register the medicine, size of the medicine and balance. I wrote a function where I can take the name of medicine, size and balance. But the problem I got stuck is how to add few sizes of medicine and quit by entering "0". Any Ideas? Should I use an extra loop?

  • 2
    Welcome to SO. Note that without a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of your code it's very hard to suggest anything. Reading [How to ask](https://stackoverflow.com/help/how-to-ask) is also recommended. – anastaciu Feb 26 '20 at 12:04

2 Answers2

0
  • gets is an obsolete function and should never be used. Why is the gets function so dangerous that it should not be used?
  • while(tmp!=0) doesn't make any sense, this checks the target array address against null, which isn't what you want. Instead of this loop, make one checking the result of fgets. Examples: Removing trailing newline character from fgets() input. You can use fgets either on stdin (command line input) or directly from the FILE pointer.
  • Avoid atoi since it doesn't handle errors well. Instead use strtol, for example strtol(str, NULL, 10) where str is the string and 10 is decimal format (base 10).
  • Avoid writing source code in your native language, since C itself is based on English. It gets confusing when you have to ask for help by English-speaking programmers (such as in this site) and it also gets confusing in general for those reading the code.

    I'm Swedish myself so I can read this, yet the "Swenglish" is much harder for me to read than source code written in pure English. There's also the nasty letters åäö which will eventually cause technical problems.

    User messages can of course be in the native language, but that's another story.

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

how to add few sizes of medicine and quit by entering "0". Any Ideas? Should I use an extra loop?

Certainly, if you want to repeat entering size and balance, you'd use a loop, e. g. you could replace

        printf("Ange storlek: ");
        while(tmp!=0){ ///////////////////////
        gets(tmp);          //////////////////////////////////////////
        }               ///////////////////////////////////
        storlek = atoi(tmp); //atoi - converts string to int
        printf("Ange saldo: ");
        gets(tmp);
        saldo = atoi(tmp); //atoi - converts string to int
        lakemedel[antalMedicine] = createMedicine(namnMed, storlek, saldo);
        antalMedicine++;

with

        while (printf("Ange storlek (avsluta genom att ange 0): "),
               fgets(tmp, sizeof tmp, stdin) && (storlek = strtoul(tmp, NULL, 10))
              )
        {
            printf("Ange saldo: ");
            if (!fgets(tmp, sizeof tmp, stdin)) break;  // leave loop on input end
            saldo = strtoul(tmp, NULL, 10);
            lakemedel[antalMedicine++] = createMedicine(namnMed, storlek, saldo);
        }
Armali
  • 18,255
  • 14
  • 57
  • 171