C++ 字符数组的初始值设定项字符串太长
请帮我完成我的最终项目。我的任务是制作一个餐厅POS系统。这个程序上周运行得很好,但当我今天检查它时,我发现了错误 “初始化字符数组的字符串太长”C++ 字符数组的初始值设定项字符串太长,c++,arrays,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 Rec
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);
}
}
char用户名[20];
字符密码[20];
char-TakeOrderC[5];
charoptionc[5];
字符接收器[5];
NTC[5];
炭钱[5];
字符选择;
字符数量[20];
浮动支付;
浮动后贴现;
浮动变化;
INTA;
INTC;
整数检查=0;
int-idcount=3;
int-num;
整数计数;
整数合计;
整数小计;
\\折扣
无效折扣(整数合计)
{
printf(“\n========================================================================================================================================”);
printf(“\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~”;
printf(“\n========================================================================================================================================”);
printf(“\n\n代码选项”);
printf(“\n[01]无折扣”);
printf(“\n[02]高级折扣(20%));
printf(“\n[03]其他折扣(30%));
而(1){
printf(“\n\n输入您的折扣代码:”);
scanf(“%s”和NTC);
如果((atoi(折扣C)==1)| |(atoi(折扣C)==2)| |(atoi(折扣C)==3)){
打破
}
否则{
printf(“\n\输入了错误的折扣代码”);
}
}
如果(atoi(折扣C)==1){
后折扣=总折扣;
}
如果(atoi(折扣C)==2){
后折扣=0.8*总计;
}
如果(atoi(折扣C)==3){
后贴现=0.7*总计;
}
而(1){
printf(“\n现金:€”);
scanf(“%s”、&money);
如果(atof(货币)>0){
if(现金)<后折扣){
printf(“\n充足资金”);
}
否则{
付款=现金;
打破
}
}
否则{
printf(“\n输入的输入不正确”);
}
}
变更=付款-后折扣;
printf(“\n变更:0.2f欧元”,变更);
printf(“\n\n您的订单正在准备中!”);
printf(“感谢您在La Pastasciutta订购”);
getch();
}
\\收据
无效收据(字符j[][30],整数k[],整数编号[],整数计数,整数总计)
{
printf(“\n\n代码选项”);
printf(“\n[01]收据”);
printf(“\n[02]重新启动命令”);
printf(“\n[03]退出”);
而(1){
printf(“\n选择选项:”);
扫描频率(“%s”和接收频率);
如果((atoi(ReceiptC)==1)| |(atoi(ReceiptC)==2)| |(atoi(ReceiptC)==3)){
打破
}
否则{
printf(“\n输入了错误代码”);
}
}
如果(atoi(ReceiptC)==1){
printf(“\n========================================================================================================================================”);
printf(“\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~”;
printf(“\n========================================================================================================================================”);
printf(“\n%-7s%-45s%-15s%s\n”、“代码”、“项目”、“数量”、“价格”);
对于(a=0;a=1)&&(atoi(TakeOrderC),您似乎通过尝试使用交错数组(为什么?)使代码过于复杂,只需声明字符串数组:
string OptionLabels[]={
“返回菜单”,
“接受命令”,
“退出”
};