2 svar
45 visningar
mattegeni2000 är nöjd med hjälpen
mattegeni2000 154
Postad: 19 dec 2022 20:59

Det går inte att lägga in input i en av mina radkoder

Lite lätt förklarat vilken typ av uppgift jag gör.

Jag ska bygga ett litet program som låter användaren hantera en inköpslista.

1 - lägg till en vara till din inköpslista

2 - skriv ut hela inköpslistan

3 - ta bort en vara från inköpslistan

4 - ändra mängden av en vara i inköpslistan

7 - avsluta programmet.¨

Det enda problemet jag har är att i funktionen void addItem(struct ShoppingList* lista). Raden printf("\nEnter an item"); 

fgets(list->itemList[lista->längd].produktnamn, MAX, stdin);

Vad problemet är att den printar ut "Enter an item",men jag kan inte ange någon inmatning. Men resterande koder/program fungerar perfekt, jag har inte fått något fel- eller varningsmeddelande. Jag undrade om någon av er kunde hjälpa mig att hitta problemet. För att göra det lite lättare för er har jag skickat hela min kod.

Vad jag har försökt åtgärda problemet:

Jag lägger till ett anrop till fflush(stdin) innan jag anropar fgets.

jag har ökade värdet på MAX för att tillåta längre inmatningssträngar. Men ingen av dessa fungerade.

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


`

void addItem(struct ShoppingList* list)
{
    
    
        if (list->length < 5)
        {
            // Clear the stdin stream
            fflush(stdin);

            printf("\nEnter an item:");
            fgets(list->itemList[list->length].productName, MAX, stdin);

            printf("\nEnter amount:");
            scanf_s("%f", &list->itemList[list->length].amount);
            while (getchar() != '\n');

            printf("\nEnter a unit:");
            fgets(list->itemList[list->length].unit, MAX, stdin);

            list->length++;
        }
        else
        {
            printf("List is full");
        }
    
}

void printList(struct ShoppingList* list)
{
    if (list->length > 0) {
        int i = 0;
        char line = '-';
        int count = 1;
        printf("Your list contains %d items \n", list->length);

        for (i = 0; i < list->length; i++)
        {
            printf("%d %-c %-10s %.2f \t %s", count, line, list->itemList[i].productName, list->itemList[i].amount, list->itemList[i].unit);
            count++;
        }
    }
    else
    {
        printf("\nYour list is empty!!");
    }
}

`void editItem(struct ShoppingList* list)
{
  
    float amount = 0;
    int opt = 0;

    printf("Enter the item index: ");
    scanf_s("%d", &opt);

    // kollar om index är  giltig
    while (opt < 1 || opt > list->length)
    {
        printf("Invalid index. Enter a valid index: ");
        scanf_s("%d", &opt);
    }

    printf("Enter the new quantity: ");
    scanf_s("%f", &amount);

    // Uppdatera beloppsfältet för artikeln vid det angivna indexet
    list->itemList[opt - 1].amount = amount;
}
void removeItem(struct ShoppingList* list)
{
    int remove;

    printf("Which item do you want to remove?:");
    scanf_s("%d", &remove);
    fflush(stdin);

    if (remove <= 1 || remove > list->length) {
        printf("Invalid choice");
    }
    else {
        for (int i = remove; i <= list->length; i++)
        {
            strcpy(list->itemList[i - 1].productName, list->itemList[i].productName);
            list->itemList[i - 1].amount = list->itemList[i].amount;
            strcpy(list->itemList[i - 1].unit, list->itemList[i].unit);
        }
        list->length--;
        printf("Items %d is removed", remove);
    }
}


void saveList(struct ShoppingList *list)
{
    //Open the file in write mode
        FILE* fp = fopen("shopping_list.txt", "w");

    // Check if the file was successfully opened
    if (fp == NULL)
    {
        printf("Error opening file\n");
        return;
    }

    // Write the number of items in the list to the file
    fprintf(fp, "%d\n", list->length);

    // Write the details of each item to the file
    for (int i = 0; i < list->length; i++)
    {
        fprintf(fp, "%s %.2f %s", list->itemList[i].productName, list->itemList[i].amount, list->itemList[i].unit);
    }

    // Close the file
    fclose(fp);
    
}

void loadList(struct ShoppingList* list)
{
    // Open the file in read mode
    FILE* fp = fopen("shopping_list.txt", "r");

    // Check if the file was successfully opened
    if (fp == NULL)
    {
        printf("Error opening file\n");
        return;
    }

    // Read the number of items in the list from the file
    fscanf(fp, "%d\n", &list->length);

    // Read the details of each item from the file
    for (int i = 0; i < list->length; i++)
    {
        fscanf(fp, "%s %f %s", list->itemList[i].productName, &list->itemList[i].amount, list->itemList[i].unit);
    }

    // Close the file
    fclose(fp);



}`
mattegeni2000 154
Postad: 19 dec 2022 21:16
mattegeni2000 skrev:

Lite lätt förklarat vilken typ av uppgift jag gör.

Jag ska bygga ett litet program som låter användaren hantera en inköpslista.

1 - lägg till en vara till din inköpslista

2 - skriv ut hela inköpslistan

3 - ta bort en vara från inköpslistan

4 - ändra mängden av en vara i inköpslistan

7 - avsluta programmet.¨

Det enda problemet jag har är att i funktionen void addItem(struct ShoppingList* lista). Raden printf("\nEnter an item"); 

fgets(list->itemList[lista->längd].produktnamn, MAX, stdin);

Vad problemet är att den printar ut "Enter an item",men jag kan inte ange någon inmatning. Men resterande koder/program fungerar perfekt, jag har inte fått något fel- eller varningsmeddelande. Jag undrade om någon av er kunde hjälpa mig att hitta problemet. För att göra det lite lättare för er har jag skickat hela min kod.

Vad jag har försökt åtgärda problemet:

Jag lägger till ett anrop till fflush(stdin) innan jag anropar fgets.

jag har ökade värdet på MAX för att tillåta längre inmatningssträngar. Men ingen av dessa fungerade.

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


`

void addItem(struct ShoppingList* list)
{
    
    
        if (list->length < 5)
        {
            // Clear the stdin stream
            fflush(stdin);

            printf("\nEnter an item:");
            fgets(list->itemList[list->length].productName, MAX, stdin);

            printf("\nEnter amount:");
            scanf_s("%f", &list->itemList[list->length].amount);
            while (getchar() != '\n');

            printf("\nEnter a unit:");
            fgets(list->itemList[list->length].unit, MAX, stdin);

            list->length++;
        }
        else
        {
            printf("List is full");
        }
    
}

void printList(struct ShoppingList* list)
{
    if (list->length > 0) {
        int i = 0;
        char line = '-';
        int count = 1;
        printf("Your list contains %d items \n", list->length);

        for (i = 0; i < list->length; i++)
        {
            printf("%d %-c %-10s %.2f \t %s", count, line, list->itemList[i].productName, list->itemList[i].amount, list->itemList[i].unit);
            count++;
        }
    }
    else
    {
        printf("\nYour list is empty!!");
    }
}

`void editItem(struct ShoppingList* list)
{
  
    float amount = 0;
    int opt = 0;

    printf("Enter the item index: ");
    scanf_s("%d", &opt);

    // kollar om index är  giltig
    while (opt < 1 || opt > list->length)
    {
        printf("Invalid index. Enter a valid index: ");
        scanf_s("%d", &opt);
    }

    printf("Enter the new quantity: ");
    scanf_s("%f", &amount);

    // Uppdatera beloppsfältet för artikeln vid det angivna indexet
    list->itemList[opt - 1].amount = amount;
}
void removeItem(struct ShoppingList* list)
{
    int remove;

    printf("Which item do you want to remove?:");
    scanf_s("%d", &remove);
    fflush(stdin);

    if (remove <= 1 || remove > list->length) {
        printf("Invalid choice");
    }
    else {
        for (int i = remove; i <= list->length; i++)
        {
            strcpy(list->itemList[i - 1].productName, list->itemList[i].productName);
            list->itemList[i - 1].amount = list->itemList[i].amount;
            strcpy(list->itemList[i - 1].unit, list->itemList[i].unit);
        }
        list->length--;
        printf("Items %d is removed", remove);
    }
}


void saveList(struct ShoppingList *list)
{
    //Open the file in write mode
        FILE* fp = fopen("shopping_list.txt", "w");

    // Check if the file was successfully opened
    if (fp == NULL)
    {
        printf("Error opening file\n");
        return;
    }

    // Write the number of items in the list to the file
    fprintf(fp, "%d\n", list->length);

    // Write the details of each item to the file
    for (int i = 0; i < list->length; i++)
    {
        fprintf(fp, "%s %.2f %s", list->itemList[i].productName, list->itemList[i].amount, list->itemList[i].unit);
    }

    // Close the file
    fclose(fp);
    
}

void loadList(struct ShoppingList* list)
{
    // Open the file in read mode
    FILE* fp = fopen("shopping_list.txt", "r");

    // Check if the file was successfully opened
    if (fp == NULL)
    {
        printf("Error opening file\n");
        return;
    }

    // Read the number of items in the list from the file
    fscanf(fp, "%d\n", &list->length);

    // Read the details of each item from the file
    for (int i = 0; i < list->length; i++)
    {
        fscanf(fp, "%s %f %s", list->itemList[i].productName, &list->itemList[i].amount, list->itemList[i].unit);
    }

    // Close the file
    fclose(fp);



}`

Behöver ingen hjälp, fixade problemet genom att byta fgets till scanf.

anders_k 234
Postad: 20 dec 2022 00:30 Redigerad: 20 dec 2022 00:32

fflush(stdin) är "undefined behavior" det skall du inte göra. Om du f.ö. inte använder scanf, behöver du inte bry dig om gamla rester i stdin.

om productName är deklarerad som en array, gör istället sizeof på den istället för att använda MAX för att vara säker på att du får rätt storlek.

använd alltid fgets istället för scanf när du läser från tangentbordet, använd sen sscanf på det du läste med fgets för att plocka ut det du behöver. detta för att slippa hantera rester i stdin om du t.ex. har inte matchande format specifier som tar bort allt från stdin.

Svara Avbryt
Close