字符数组的Initializer字符串太长



请帮我完成编程最终项目。我的任务是制作一个餐厅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 ("nn CODE          OPTION");
printf ("n [01]     No Discount");
printf ("n [02]    Senior Discount (20 percent)");
printf ("n [03]    Others Discount (30 percent)");
while (1) {
printf ("nnEnter your Discount Code: ");
scanf ("%s", &DiscountC);
if ((atoi(DiscountC) == 1) || (atoi(DiscountC) == 2) || (atoi(DiscountC) == 3)) {
break;
}
else {
printf ("nWrong 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 ("nnYour 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 ("nn 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%sn", "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€%dn", "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: €%in", 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 ("nt~ ~ ~ Le Pastasciutta Menu ~ ~ ~");
printf ("n====================================================");
printf ("n%-9s%-40s%-9sn", "CODE", "PASTA SPECIALE", "PRICE");
for (a=0; a<20; a++){
printf ("%02d%-7s%-40s€%dn", 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 ("nt~ ~ ~ Le Pastasciutta Options ~ ~ ~");
printf ("n====================================================");
printf ("n%-9s%-40snn", "CODE", "OPTIONS");
for (a=0; a<3; a++){
printf ("%02d%-7s%-15sn", 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 ("nt~ ~ ~ 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] = '';
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 ("nt ~ ~ ~ Welcome to Le Pastasciutta ~ ~ ~");
printf ("nt ~~~ Serving Authentic ~~~");
printf ("nt ~~~ Italian Pasta ~~~  ");
printf ("nt ~~~ Since 2019 ~~~ ");
printf ("n====================================================");
}
\main program
int main (void) {
Banner ();
if (LoginCheck()) {
Options (0);
}
}

您试图使用锯齿状数组,似乎使代码过于复杂(为什么?(,只需声明字符串的数组:

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

最新更新