0

When the "Would you like to enter another spare part" argument comes up, i cannot enter a value. It automatically starts loop from beginning. Do I actually need to define it, when the user enters a y?

   void spareParts()
    {
        char date[11];
        char Part[ENTRIES];
        char Value[ENTRIES];
        int i;
        char choice;

    for(i=0; i<100; i++)
        {
        printf("Spare parts value calculator!\n");
        printf("--------------------------------------------------\n");
        printf("Date (dd/mm/year):");
        scanf("%c", date);


        printf("Part No:");
        scanf("%s", Part);


        printf("Value:");
        scanf("%s", Value);

        printf("Would you like to enter another spare part:");
        scanf("%c", &choice);

        if(choice=='n')
        {
            /*double total=0;
            total += Value[i];
            void printResults(double total, char date);*/
            break;
        }
        }
    }

    void printResults(double total, char date)
    {
        printf("The Wheeler Autoparts Company\n");
        printf("----------------------------------------------\n");
        printf("Inventory details\n");
        printf("Date %c\n", date);
        printf("------------------------------------------\n");
        printf("%.2s%.2s\n", "Part" , "Value");
        printf("--------------------------------------\n");
        printf("Total: %.2f\n", total);
    }


int main(void)
{

    spareParts();
    return 0;

}

Mat
  • 45
  • 2
  • 10
  • if `choice` equals `n` then you `break` out of the `for` loop. Any other key will result in the `for` loop iterating again. – KSdev Feb 25 '14 at 20:08
  • Not clearing your input buffer. Another `scanf` gotcha. – Fred Larson Feb 25 '14 at 20:09
  • I am not sure if you need a max of 100 - if you do not a while loop may be a better choice. – BillyBigPotatoes Feb 25 '14 at 20:09
  • Can you give us more details on what you mean by "you cannot enter a value"? Does it mean the console doesn't output anything after you hit enter, or does it mean the console does not register your keypresses at all? – Ki Chjang Feb 25 '14 at 20:10
  • @KiChjang: My bet is it doesn't wait for input, it just eats what remains in the buffer (probably the newline). That's not a 'n', so it continues in the loop. – Fred Larson Feb 25 '14 at 20:11
  • It just prints out the line:"Would you like to enter another spare part" and then runs loop again. I dont enter any value at that argument (skips the scanf choice)/ – Mat Feb 25 '14 at 20:12
  • 1
    Solved it. Needed an space before %c – Mat Feb 25 '14 at 20:13

1 Answers1

0

Change

scanf("%c", date);

to

scanf("%s", date);
user376507
  • 2,004
  • 1
  • 19
  • 31