c++ - 字符数组的Initializer-String太长

标签 c++ arrays

请帮助我完成我的编程最终项目。我的任务是制作餐厅POS系统。该程序上周运行良好,但是当我今天进行检查时,出现了错误,

在此文章中,“字符数组的初始化字符串太长”

char OptionLabels [][3] = {
    "Back to Menu",
    "Take Order",
    "Exit"
};

这是我的完整代码
char username [20];
char password [20];

char TakeOrderC[5];
char OptionC[5];
char ReceiptC[5];
char DiscountC[5];

char money[5];
char choice;
char qty[20];

float payment;
float afterdiscount;
float change;

int a;
int c;
int check = 0;
int idcount = 3;
int num;
int count;
int Total;
int Subtotal;

\\discount
void Discount (int Total)
{
printf ("\n==================================================================================");
printf ("\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  Le Pastasciutta Discounts  ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ");
printf ("\n==================================================================================");

printf ("\n\n CODE          OPTION");
printf ("\n [01]     No Discount");
printf ("\n [02]    Senior Discount (20 percent)");
printf ("\n [03]    Others Discount (30 percent)");

while (1) {
    printf ("\n\nEnter your Discount Code: ");
    scanf ("%s", &DiscountC);
    if ((atoi(DiscountC) == 1) || (atoi(DiscountC) == 2) || (atoi(DiscountC) == 3)) {
        break;
    }
    else {
        printf ("\n\Wrong Discount Code Entered");
    }
}

if (atoi(DiscountC) == 1) {
    afterdiscount = Total;
}
if (atoi(DiscountC) == 2) {
    afterdiscount = 0.8 * Total;
}
if (atoi(DiscountC) == 3) {
    afterdiscount = 0.7 * Total;
}

while (1) {
    printf ("\nCash: € ");
    scanf ("%s", &money);

    if (atof(money) > 0) {
        if (atof(money) < afterdiscount) {
            printf ("\nInsufficient Funds");
        }
        else {
            payment = atof(money);
            break;
        }
    }
    else {
        printf ("\nIncorrect Input is Entered");
    }
}
change = payment - afterdiscount;
printf ("\nChange: €%.2f", change);
printf ("\n\nYour order is being prepared!");
printf ("\nThank you for ordering at La Pastasciutta");
getch ();
}

\\receipt
void Receipt ( char j[][30], int k[], int num[], int count, int Total) 
{
printf ("\n\n CODE          OPTION");
printf ("\n [01]            Receipt");
printf ("\n [02]                Restart Order");
printf ("\n [03]            Exit");

while (1){
    printf ("\nSelect Option: ");
    scanf ("%s", &ReceiptC);
    if ((atoi(ReceiptC) == 1) || (atoi(ReceiptC) == 2) || (atoi(ReceiptC) == 3)) {
        break;
    }
    else {
        printf ("\nWrong Code Entered ");
    }
}

if (atoi(ReceiptC) == 1) {      
    printf ("\n==================================================================================");
    printf ("\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Le Pastasciutta Receipt ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ");
    printf ("\n==================================================================================");
    printf ("\n%-7s%-45s%-15s%s\n", "CODE", "ITEM", "QTY", "PRICE");

    for (a=0; a<count; a=a+3) {
        printf ("\n%02d%-5s%-45s%-15d€%d", num[a]+1, " ", j[num[a]], num[a+1], num[a+2]);
    }
    printf ("\n_________________________________________________________________________________");
    printf ("\n%-67s€%d\n", "TOTAL", Total);
    Discount (Total);
}
else if (atoi(ReceiptC) == 2) {
    void Options (int);
    Options (1);
}
else if (atoi(ReceiptC) == 3) {
    printf ("Order is Cancelled");
}
}

\\takeorder
void TakeOrder (char j[][30], int k[], int num[], int count, int Total)
{
printf ("\nEnter Item Code: ");
scanf ("%s", &TakeOrderC);

if ((atoi(TakeOrderC) >= 1) && (atoi(TakeOrderC) <= 20 )) {
    printf ("Enter Order Quantity: ");
    scanf ("%s", qty);

    if (atoi(qty) != 0 ) {
        Subtotal = k [atoi(TakeOrderC) - 1] * atoi(qty);

        Total = Total + Subtotal;
        printf ("Total: €%i\n", Total);

        num [count] = atoi(TakeOrderC) - 1;
        count++;

        num[count] = atoi(qty);
        count++;

        num[count] = Subtotal;
        count++;


        while(1)    {
            printf ("\nDo you have any other orders? [ Y / N ]: ");
            scanf ("%s", &choice);

            if (choice == 'Y' || choice == 'N' || choice == 'n' || choice == 'y') {
                break;
            }
            else {
                printf ("\nPlease Try Again.");
            }
        }

        if (choice == 'Y' || choice == 'y') {
            TakeOrder (j, k, num, count, Total);
        }

        else if (choice == 'N' || choice == 'n') {
            Receipt (j, k, num, count, Total);
        }       
    }
    else {
        printf ("\Invalid, try again");
        printf ("\n============================");
        TakeOrder (j, k, num, count, Total);
    } 
}
else {
    printf ("\nIncorrect, try again");
    printf ("\n============================");
    TakeOrder (j, k, num, count, Total);
    }
}


\\menudisplay
void MenuDisplay ( char j [][30], int k [])
{
printf ("\n====================================================");
printf ("\n\t~ ~ ~ Le Pastasciutta Menu ~ ~ ~");
printf ("\n====================================================");

printf ("\n%-9s%-40s%-9s\n", "CODE", "PASTA SPECIALE", "PRICE");
for (a=0; a<20; a++){
    printf ("%02d%-7s%-40s€%d\n", a+1, " ", j[a], k[a]);
}

printf ("\nPress Any Key to Continue to Options ");
getch ();
}


\\options
void Options (int firstPass)
{
char pasta [][30] = {
    "Pasta Al Pomodoro",
    "Pasta Alla Carbonara",
    "Pasta Al Pesto",
    "Pasta Di Grancio",
    "Pasta Di Calamari",
    "Aglio E Olio",
    "Spaghetti Alle Vongole",
    "Shrimp Puttanesca",
    "Fettucine Alfredo",
    "Nonna's Lasagna",
    "Ravioli Di Ricotta",
    "Quattro Formaggio Ziti",
    "Squid Tortellini",
    "Farfelle Di Spinaci",
    "Garlic-Lemon Linguine",
    "Peperoni Ravioli",
    "Wagyu Beef Macaroni",
    "Lobster Al Linguine",
    "Penne Al Tartufo",
    "Pasta Al Caviar D'Oro" 
};

int prices[] = {
    250,
    265,
    280,
    350,
    380,
    385,
    400,
    430,
    450,
    500,
    550,
    670,
    690,
    750,
    800,
    820,
    1350,
    1500,
    2700,
    6500    
};

char OptionLabels [][3] = {
    "Back to Menu",
    "Take Order",
    "Exit"
};
int num [] = {};

if (firstPass == 0) {
    MenuDisplay (pasta, prices);
    Options (1);
}

else {
    printf ("\n====================================================");
    printf ("\n\t~ ~ ~ Le Pastasciutta Options ~ ~ ~");
    printf ("\n====================================================");

    printf ("\n%-9s%-40s\n\n", "CODE", "OPTIONS");
    for (a=0; a<3; a++){
        printf ("%02d%-7s%-15s\n", a+1, " ", OptionLabels[a]);
    }

    printf ("\nEnter Option Code: ");
    scanf ("%s", &OptionC);

    //option 1 BACK TO MENU
    if (atoi(OptionC) == 1) {
        MenuDisplay (pasta, prices);
        Options (1);
    }

    //option 2 TAKE ORDER
    else if (atoi(OptionC) == 2) {
        printf ("\n====================================================");
        printf ("\n\t~ ~ ~ Le Pastasciutta Orders ~ ~ ~");
        printf ("\n====================================================");
        TakeOrder(pasta, prices, num, 0, 0);
    }

    //option 3 EXIT
    else if (atoi(OptionC) == 3){
        printf ("\nProgram Aborted");
    }

    // if wrong option code is entered
    else {
        printf ("\nIncorrect Code");
        printf ("\nPress Any Key to Continue... ");
        getch ();
        Options (1);        
        }
    }
}

\\login
int LoginCheck ()
{

char CorrectUsername [3][20] = {"frank", "ayesha", "buyer"};
char CorrectPassword [3][20] = {"pasta", "cheese", "dlsu"};

printf ("\nUsername: ");
scanf ("%s", &username);
printf ("Password: ");
a=0;
do
{
    int temp = getch ();
    if (temp == '\b') {
        if (a !=0 ) {
            printf ("\rPassword: ");
            for (c=0; c<a; c++) {
                printf ("   ");
            }
            printf ("\rPassword: ");
            for (c=1; c<a; c++) {
                printf ("*");
            }
            a--;
        }   
    }   
    else if (temp != 224 && temp != 75) {
        if (temp != 224 && temp != 77) {
            password [a] = temp;
            if (password [a] != '\r') {
                printf ("*");
            }
            a++;
        }
    }
}
while (password [a-1] != '\r');
password [a-1] = '\0';

for (a=0; a<idcount; a++) {
    if (strcmp (username, CorrectUsername[a]) == 0) {
        check = 1;
        break;
    }
}

if (check == 0) {
    printf ("\nIncorrect Username");
    printf ("\nPlease try again.");
    printf ("\n==================================");
    LoginCheck();
}

else if (check == 1) {
    if (strcmp(password, CorrectPassword[a]) == 0) {
        printf ("\nSuccessfully Logged In!");
        return 1;
    }
    else {
        printf ("\nIncorrect Passsword");
        printf ("\nPlease try again.");
        printf ("\n==================================");
        LoginCheck();
    }
}
}

\\welcomebanner
int Banner ()
{
printf ("====================================================");
printf ("\n\t ~ ~ ~ Welcome to Le Pastasciutta ~ ~ ~");
printf ("\n\t ~~~ Serving Authentic ~~~");
printf ("\n\t ~~~ Italian Pasta ~~~  ");
printf ("\n\t ~~~ Since 2019 ~~~ ");
printf ("\n====================================================");
}

\\main program
int main (void) {
Banner ();
if (LoginCheck()) {
    Options (0);
}
}

最佳答案

看起来您通过尝试使用锯齿状数组(为什么?)使代码过于复杂,只需声明字符串数组即可:

string OptionLabels[] = {
    "Back to Menu",
    "Take Order",
    "Exit"
};

关于c++ - 字符数组的Initializer-String太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61674303/

相关文章:

c++ - C++ 中 and() 函数的用途

c++ - C++ 上二维数组的大小

java - 使用 JNI 从 JMS 调用 C++

arrays - 为什么会出现 "Not an ARRAY reference"错误?

c++ - 生成多维数组而不是 cout

c++ - 从 C++ 输出中删除逗号

C++ 实例初始化语法

c - 如何在节点中插入字符串(由用户输入)? (二叉搜索树)

arrays - 数组<编号> : get and set Int values without casting

php - 选择元素时将数组视为循环数组 - PHP