回 帖 发 新 帖 刷新版面

主题:各位大佬不知道为什么无法将order函数结果赋值给storage,而且最后的printf也会乱码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


const char *space=" ";

char A1[36] = "A1   HAM AND EGG                 $20";
char A2[36] = "A2   BACON AND CHEESE            $10";
char A3[36] = "A3   TUNA SALAD                  $15";
char A4[36] = "A4   BEEF SOUP                   $25";

char B1[36] = "B1   SPICY BEEF BARBECUE         $20";
char B2[36] = "B2   PORK BARBECUE               $10";
char B3[36] = "B3   OVEN CHICKEN BARBECUE       $15";
char B4[36] = "B4   PULLED BEEF BARBECUE BURGER $20";

char C1[36] = "C1   SPICY PORK BARBECUE         $20";
char C2[36] = "C2   VEGETABLE PORK BARBECUE     $10";
char C3[36] = "C3   OVEN PORK BARBECUE          $15";
char C4[36] = "C4   PULLED BEEF BARBECUE BURGER $25";

char line[45] = "---------------------------------------------";

char heading[36] = "Code Dish                      Price";


char* order_breakfast(char*n)
{

    if (strcmp(n, "A1") == 0)
        return A1;
    else if (strcmp(n, "A2") == 0)
        return A2;
    else if (strcmp(n, "A3") == 0)
        return A3;
    else if (strcmp(n, "A4") == 0)
        return A4;
    else if (strcmp(n, "B1") == 0||strcmp(n, "B2") == 0||strcmp(n, "B3") == 0||strcmp(n, "B4") == 0||strcmp(n, "C1") == 0||strcmp(n, "C2") == 0||strcmp(n, "C3") == 0||strcmp(n, "C4") == 0)
    {
        printf("This dish is not available now!\n");
        return *space;
    }
    else
    {
        printf("This dish code does not exist! Try again! ");
        return *space;
    }

}



char* order_lunch(char*m)
{

    if (strcmp(m, "B1") == 0)
        return A1;
    else if (strcmp(m, "B2") == 0)
        return A2;
    else if (strcmp(m, "B3") == 0)
        return A3;
    else if (strcmp(m, "B4") == 0)
        return A4;
    else if (strcmp(m, "A1") == 0||strcmp(m, "A2") == 0||strcmp(m, "A3") == 0||strcmp(m, "A4") == 0||strcmp(m, "C1") == 0||strcmp(m, "C2") == 0||strcmp(m, "C3") == 0||strcmp(m, "C4") == 0)
    {
        printf("This dish is not available now!\n");
        return *space;
    }
    else
    {
        printf("This dish code does not exist! Try again! ");
        return *space;
    }


}



char* order_dinner(char*k)
{

    if (strcmp(k, "C1") == 0)
        return A1;
    else if (strcmp(k, "C2") == 0)
        return A2;
    else if (strcmp(k, "C3") == 0)
        return A3;
    else if (strcmp(k, "C4") == 0)
        return A4;
    else if (strcmp(k, "A1") == 0||strcmp(k, "A2") == 0||strcmp(k, "A3") == 0||strcmp(k, "A4") == 0||strcmp(k, "B1") == 0||strcmp(k, "B2") == 0||strcmp(k, "B3") == 0||strcmp(k, "B4") == 0)
    {
        printf("This dish is not available now!\n");
        return *space;
    }
    else
    {
        printf("This dish code does not exist! Try again! ");
        return *space;
    }

}





int main()
{
    int password;
    int customer_number;
    time_t timep;
    struct tm *p;
    int hour;
    int counter;
    int counterx[6]={0,0,0,0,0,0};
    char storage[6][4][40];
    char code[2];
    int total_cost=0;
    int cash;
    int change;
    int a,b,c;




    for (a=0;a<6;a++)
    {
        for (b=0;b<4;b++)
        {
            for (c=0;c<40;c++)
            {
                storage[a][b][c]="";
            }
        }
    }




/*
   printf("Enter password:");
   scanf("%d",&password);

   while (password != 20412178)
   {
       printf("Wrong pass word entered, please try again.\n");
       printf("Enter password:");
       scanf("%d",&password);
   }

*/

    printf("User name: Ruiheng Chen");//display the user name

    printf("\n\n\n %70s","BREAKFAST     (Available: 8am-10am)\n");// menu for breakfast
    printf("%63s","A1  HAM AND EGG                  $20\n");
    printf("%63s","A2  BACON AND CHEESE             $10\n");
    printf("%63s","A3  TUNA SALAD                   $15\n");
    printf("%63s","A4  BEEF SOUP                    $25\n");
    printf("\n\n");

    printf("%67s","LUNCH     (Available: 11am-1pm)\n");//menu for lunch
    printf("%63s","B1  SPICY BEEF BARBECUE          $20\n");
    printf("%63s","B2  PORK BARBECUE                $10\n");
    printf("%63s","B3  OVEN CHICKEN BARBECUE        $15\n");
    printf("%63s","B4  PULLED BEEF BARBECUE BURGER  $20\n");
    printf("\n\n");

    printf("%68s","DINNER     (Available: 11am-1pm)\n");//menu for dinner
    printf("%63s","C1  SPICY PORK BARBECUE          $20\n");
    printf("%63s","C2  VEGETABLE PORK BARBECUE      $10\n");
    printf("%63s","C3  OVEN PORK BARBECUE           $15\n");
    printf("%63s","C4  PULLED BEEF BARBECUE BURGER  $25\n");



    printf("please keep the number of customers at one table to be less than 6.\n");
    printf("please enter the number of customers at your table.\n");
    printf("Customer Number:");
    scanf("%d",&customer_number);

    while (customer_number>6||customer_number<1)
    {
        printf("the number is within the range, please enter again.\n");
        printf("Customer Number:");
        scanf("%d",&customer_number);
    }



    //time (&timep);
    //p=gmtime(&timep);
    //hour = 8+p->tm_hour;

    hour = 9;


    if (hour>=8&&hour<=10)
    {
        printf("Breakfast is available now.\n");
        for (counter=0;counter<customer_number;counter++)
        {
            printf("Dish entry for customer %d(enter F to quit):\n",counter+1);
            for (counterx[counter]=0;counterx[counter]<4;counterx[counter]++)
            {
                scanf("%s",code);
                if (strcmp(code, "F") != 0)
                {
                    *storage[counter][counterx[counter]] = order_breakfast(code);
                    if (strcmp(code,"A1")==0)
                        total_cost += 20;
                    else if (strcmp(code,"A2")==0)
                        total_cost += 10;
                    else if (strcmp(code,"A3")==0)
                        total_cost += 15;
                    else if (strcmp(code,"A4")==0)
                        total_cost += 25;
                }
                else
                    break;
            }

        }
    }


    else if (hour>=11&&hour<=13)
    {
        printf("Lunch is available now.\n");
        for (counter=0;counter<customer_number;counter++)
        {
            printf("Dish entry for customer %d(enter F to quit):\n",counter+1);
            for (counterx[counter]=0;counterx[counter]<4;counterx[counter]++)
            {
                scanf("%s",code);
                if (strcmp(code, "F") != 0)
                {
                    *storage[counter][counterx[counter]] = order_lunch(code);
                    if (strcmp(code,"B1")==0)
                        total_cost += 20;
                    else if (strcmp(code,"B2")==0)
                        total_cost += 10;
                    else if (strcmp(code,"B3")==0)
                        total_cost += 15;
                    else if (strcmp(code,"B4")==0)
                        total_cost += 25;
                }
                else
                    break;
            }

        }
    }



    else if (hour>=17&&hour<=20)
    {
        printf("Dinner is available now.\n");
        for (counter=0;counter<customer_number;counter++)
        {
            printf("Dish entry for customer %d(enter F to quit):\n",counter+1);
            for (counterx[counter]=0;counterx[counter]<4;counterx[counter]++)
            {
                scanf("%s",code);
                if (strcmp(code, "F") != 0)
                {
                    *storage[counter][counterx[counter]] = order_dinner(code);
                    if (strcmp(code,"C1")==0)
                        total_cost += 20;
                    else if (strcmp(code,"C2")==0)
                        total_cost += 10;
                    else if (strcmp(code,"C3")==0)
                        total_cost += 15;
                    else if (strcmp(code,"C4")==0)
                        total_cost += 25;
                }
                else
                    break;
            }

        }
    }


    else
    {
        printf("Sorry, it is not in bussiness hours right now.\n");
    }

    printf("%s\n",line);
    printf("%s",storage[0][0]);







}

回复列表 (共1个回复)

沙发

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

char A1[36] = "A1   HAM AND EGG                 $20";
char A2[36] = "A2   BACON AND CHEESE            $10";
char A3[36] = "A3   TUNA SALAD                  $15";
char A4[36] = "A4   BEEF SOUP                   $25";

// Other dish strings...

char line[45] = "---------------------------------------------";
char heading[36] = "Code Dish                      Price";
char notAvailableStr[40] = "This dish is not available now!";
char invalidCodeStr[40] = "This dish code does not exist! Try again!";

char* order_breakfast(char* n) {
    if (strcmp(n, "A1") == 0)
        return A1;
    // Other dish comparisons...
    else if (...) // Check for unavailable dishes for this time
        return notAvailableStr;
    else
        return invalidCodeStr;
}

// Similarly modify order_lunch and order_dinner functions...

int main() {
    // ... Previous code ...

    char storage[6][4][40] = {{"", "", "", ""}, // Initializing all strings to be empty
                              {"", "", "", ""},
                              {"", "", "", ""},
                              {"", "", "", ""},
                              {"", "", "", ""},
                              {"", "", "", ""}};

    // ... Other code ...

    if (hour >= 8 && hour <= 10) {
        printf("Breakfast is available now.\n");
        for (counter = 0; counter < customer_number; counter++) {
            printf("Dish entry for customer %d(enter F to quit):\n", counter + 1);
            for (counterx[counter] = 0; counterx[counter] < 4; counterx[counter]++) {
                scanf("%s", code);
                if (strcmp(code, "F") != 0) {
                    strcpy(storage[counter][counterx[counter]], order_breakfast(code)); // Using strcpy
                    // Logic to update total cost...
                } else
                    break;
            }
        }
    }

    // Similar modifications for lunch and dinner...

    printf("%s\n", line);
    printf("%s\n", storage[0][0]);  // Now, this should correctly print the string
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册