切换情况下的错误,标签只能是语句的一部分,声明不是语句,请帮助调试 [重复]

Error in switch case a label can only be part of a statement and a declaration is not a statement please help in debugging [duplicate]

提问人:Sri Kanth 提问时间:11/6/2023 最后编辑:Jonathan LefflerSri Kanth 更新时间:11/6/2023 访问量:68

问:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

// Define structures for e-books

typedef struct {

    int id;

    char title[100];

    char author[100];

    float price;

} Ebook;

// Define a shopping cart structure

typedef struct {

    Ebook ebooks[100];

    int itemCount;

} ShoppingCart;

// Function to display a list of available e-books

void displayEbooks(Ebook ebooks[], int count) {

    printf("Available E-books:\n");

    for (int i = 0; i < count; i++) {

        printf("%d. %s by %s - $%.2f\n", ebooks[i].id, ebooks[i].title, ebooks[i].author, ebooks[i].price);

    }

}

// Function to add an e-book to the shopping cart

void addToCart(ShoppingCart *cart, Ebook ebook) {

    if (cart->itemCount < 100) {

        cart->ebooks[cart->itemCount] = ebook;

        cart->itemCount++;

        printf("Added %s to the cart.\n", ebook.title);

    } else {

        printf("Your cart is full.\n");

    }

}

// Function to display the contents of the shopping cart

void displayCart(ShoppingCart *cart) {

    printf("Shopping Cart:\n");

    for (int i = 0; i < cart->itemCount; i++) {

        printf("%d. %s - $%.2f\n", i + 1, cart->ebooks[i].title, cart->ebooks[i].price);

    }

    printf("Total Items: %d\n", cart->itemCount);

}

int main() {

    Ebook ebooks[] = {

        {1, "Book 1", "Author 1", 10.99},

        {2, "Book 2", "Author 2", 12.99},

        {3, "Book 3", "Author 3", 9.99}

    };

    ShoppingCart cart;

    cart.itemCount = 0;

    int choice;

    do {

        printf("\nMenu:\n");

        printf("1. Display E-books\n");

        printf("2. Add to Cart\n");

        printf("3. Display Cart\n");

        printf("4. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

        switch (choice) {

            case 1:

                displayEbooks(ebooks, sizeof(ebooks) / sizeof(ebooks[0]));

                break;

            case 2:

                int bookId;

                printf("Enter the ID of the e-book you want to add to the cart: ");

                scanf("%d", &bookId);

                if (bookId >= 1 && bookId <= sizeof(ebooks) / sizeof(ebooks[0])) {

                    addToCart(&cart, ebooks[bookId - 1]);

                } else {

                    printf("Invalid book ID.\n");

                }

                break;

            case 3:

                displayCart(&cart);

                break;

            case 4:

                printf("Thank you for shopping!\n");

                break;

            default:

                printf("Invalid choice. Please try again.\n");

        }

    } while (choice != 4);

    return 0;

}

错误显示在我的开关案例 int bookId 中;72 row.pls 调试帮助

c visual-studio-code 调试 switch-statement

评论

0赞 John Gordon 11/6/2023
不能将变量声明作为标签后面的第一件事(而 case 语句就是标签)。将语句移到上面。printf()int bookId;
0赞 Sri Kanth 11/6/2023
先生,我按照你说的做了,但现在它正在运行并且没有给出输出。
0赞 John Gordon 11/6/2023
完全没有输出?甚至没有第一个菜单?
0赞 Sri Kanth 11/6/2023
不,先生,它只是在运行
0赞 Sri Kanth 11/6/2023
先生,您能否在视觉对象中运行并检查先生??

答: 暂无答案