无法将用户字符串输入到二维字符串数组C++中

nkhmeac6  于 2022-12-15  发布在  其他
关注(0)|答案(1)|浏览(159)

我正在尝试制作一个简单的咖啡店经理,我使用了一个2D字符串数组来创建一个菜单,用户可以编辑它,但是每次我试图替换一个字符串,当我试图打印它时,数组的其余部分就会消失。
我知道这是基本的东西,我的代码可能有很多问题,但我真的不知道为什么它不工作。
代码:

using namespace std;
    string menu[8][3] = {
        {"\n Expresso", "-------------", "$6.50"},
        {"\n Latte", "----------------", "$8.00"},
        {"\n Cappuccino", "-----------", "$8.00"},
        {"\n Chocolate", "------------", "$9.50"},
        {"\n Croque_Monsieur", "------", "$18.50"},
        {"\n Panettone", "------------", "$12.99"},
        {"\n Pao_de_Queijo", "--------", "$4.99"},
        {"\n Petit_Gateau", "---------", "$18.99"},
    };

    void editMenu(){        
        int i, op, count1, count2;
    
        printf("\n1. Edit Menu");
        printf("\n2. Back");
        printf("\n");
        scanf("%d",&op);
        

        switch(op){

//asks user to chose a line and edit the first (product) and third (price) columns          
            case 1:
                while(op==1){
                   system("cls");

                                
                    printf("Which line would you like to edit?\n");
                    scanf("%d", &i);
                    
                    printf("\nInsert new product: ");
                    scanf("%s", &menu[i][1]);
                
                    printf("\nInsert new price: ");
                    scanf("%s", &menu[i][3]);
                    
                    system("cls");

//loop for printing the menu            
                    for(count1 = 0; count1<8; count1++){
                        for(count2 = 0; count2<3; count2++){
                            cout << menu[count1][count2];
                        }
                     }
            printf("\nPress 1 to make more changes or other key to exit :");
            scanf("%d", &op);
            } 
//End of while
            break;
                
//Exit
            case 2:
                system("cls");
                home();
            break;
        }
    }
vfh0ocws

vfh0ocws1#

The indices are wrong. The products are on menu[i][0] and the prices are on menu[i][2] .
The code can be rewritten as follows. Other logics are not fixed.

#include <iostream>

using namespace std;

string menu[8][3] = {
    {"\n Expresso", "-------------", "$6.50"},
    {"\n Latte", "----------------", "$8.00"},
    {"\n Cappuccino", "-----------", "$8.00"},
    {"\n Chocolate", "------------", "$9.50"},
    {"\n Croque_Monsieur", "------", "$18.50"},
    {"\n Panettone", "------------", "$12.99"},
    {"\n Pao_de_Queijo", "--------", "$4.99"},
    {"\n Petit_Gateau", "---------", "$18.99"},
};

void editMenu() {
    int i, op, count1, count2;

    printf("\n1. Edit Menu");
    printf("\n2. Back");
    printf("\n");
    //scanf_s("%d", &op);
    cin >> op;


    switch (op) {

        //asks user to chose a line and edit the first (product) and third (price) columns          
    case 1:
        while (op == 1) {
            system("cls");

            printf("Which line would you like to edit?\n");
            //scanf_s("%d", &i);
            cin >> i;

            printf("\nInsert new product: ");
            //scanf_s("%s", &menu[i][1]);
            cin >> menu[i][0];

            printf("\nInsert new price: ");
            //scanf_s("%s", &menu[i][3]);
            cin >> menu[i][2];

            system("cls");

            //loop for printing the menu            
            for (count1 = 0; count1 < 8; count1++) {
                for (count2 = 0; count2 < 3; count2++) {
                    cout << menu[count1][count2];
                }
            }
            printf("\nPress 1 to make more changes or other key to exit :");
            //scanf_s("%d", &op);
            cin >> op;
        }
        //End of while
        break;

        //Exit
    case 2:
        system("cls");
        //home();
        break;
    }
}

int main()
{
    editMenu();
}

相关问题