gpt4 book ai didi

c - checkout函数中g_tot计算带来垃圾值

转载 作者:行者123 更新时间:2023-11-30 19:28:19 24 4
gpt4 key购买 nike

在此代码中,结账函数g_tot计算带来了垃圾值。我认为这是因为我正在从另外两个函数计算两个变量,但我不知道如何修复它。 if 条件下的餐厅函数还有另一个错误,如果我输入的值超过 8,它会给 tot 带来垃圾值。但最重要的是

#include<stdio.h>
#include<conio.h>

// Global variables

int room,answr,days=0;
char name[20],choose;
int i=0,tot=0,p_tot=0,g_tot=0,z=0;
int p_price[2][5]={4000,10000,20000};


void screenheader()
{
printf("\n ::::::::::::::::::::::::::::::::::::::");
printf("\n :: ::");
printf("\n :: @@@@@@@@@@@@@@@@@@@@@@@@@@@@ ::");
printf("\n :: @ @ ::");
printf("\n :: @ WELCOME @ ::");
printf("\n :: @ TO @ ::");
printf("\n :: @ SURF HOTEL @ ::");
printf("\n :: @ @ ::");
printf("\n :: @@@@@@@@@@@@@@@@@@@@@@@@@@@@ ::");
printf("\n :: ::");
printf("\n ::::::::::::::::::::::::::::::::::::::");

}

void check_in()
{


int contact_No[20],NIC[10];
char first_name[10],last_name[10],Country[10];


system("cls");
screenheader();
printf("\n1. Packages");
printf("\n2. Room Allocation");
printf("\n3. Back\n\n");


scanf(" %d",&answr);
switch(answr)
{
case 1:{
system("cls");
printf("\n\n\nPer 2 Persons");
printf("\n\t\tPackage Name >>>> Couple");
printf("\n\t\tRs.4000/= per day");
printf("\n\t\tBed >>>> 1");
printf("\n\t\t *Tv Available");

printf("\n\n\n\n\t\t\nPer 4 Persons\n\t\tPackage Name >>>>
Family");
printf("\n\t\tRs.10,000/= per day");
printf("\n\t\tBed >>>> 2");
printf("\n\t\t*Tv Available \n\t\t*A/C \n\t\t*WIFI");

printf("\n\n\n\nPer 8 Persons\n\t\tPackage Name >>>> Deluxe");
printf("\n\t\tRs.20,000/= per day");
printf("\n\t\tBed >>>> 3 Large ");
printf("\n\t\t *Tv Available \n\t\t*A/C \n\t\t*WIFI\n\t\t*Local
Travel Guide\n\t\t*Balcony with a view\n\t\t*Writing desk");
printf("\n\n*Press 1 to go back");
getch();
check_in();


break;
}
case 2:{

printf("What package do you want?");
scanf(" %d",&i);
p_tot=p_tot+p_price[i-1];

if(i == 1)
{
printf("You have selected Couple package");
}
else if (i == 2)
{
printf("You have selected Family Package ");
}
else if (i == 3)
{
printf("You have selected Deluxe package");
}
else
{
printf("\n\nWrong input, please refer to packages and try
again.\nPress Enter to select another package");

getch();
check_in();
}


printf("\nEnter First Name:\n");
scanf(" %s",&first_name);
printf("Last Name:\n");
scanf(" %s",&last_name);
printf("How many days do you want to stay?");
scanf(" %d",&days);
printf("Enter your Country:");
scanf(" %s",&Country);
printf("Enter your NIC No:");
scanf(" %d",&NIC);
printf("Enter your Contact No:");
scanf(" %d",&contact_No);

printf("Hello %s %s you have booked a Room for
%d",&first_name,&last_name,days);
getch();
system("cls");
int main();
}

case 3: main();
}


}

void restaurant()
{
int fc[6];
char ans;
char food[8][30]={"Bread","Noodles","Salad","Popcorn","Chocolate ice
cream","Vanilla ice cream","Cold Coffee","Milk Shake"};
int price[8]={180,120,65,55,70,70,110,200};

printf("Press Enter To Continue To The Restaurant ");
getchar();
system("cls");

printf("\n\n\n\n\n\t *********");
printf("\n\t MENU CARD");
printf("\n\t *********\n\n\n");
printf("\n Food Code\t\tprice\t\t Food Name\n");

for(i=0;i<8;i++)
{
printf("\n\t\t%d",i+1);
printf("\t\tRs. %d",price[i]);
printf("\t\t%s",food[i]);
}
printf("\n\n\n\t *PRESS 0 TO GO TO THE MAIN MENU\n\t *PRESS 1 TO ORDER FOOD
: ");
scanf(" %d",&answr);
switch(answr)
{
case 0:
{
main();
break;
}
case 1:do
{
printf("\n\nENTER THE FOOD CODE YOU WANT TO HAVE :: ");
scanf("%d", &z);

if (z < 1 || z > 8)
{
printf("Invalid food code\n");

}

tot=tot+price[z-1];
printf("total so far is Rs.%d\n",tot);
printf("DO YOU WANT MORE(Y/N) ::");
scanf(" %c", &ans);



} while(ans=='y'|| ans=='Y');
printf("\n\nYour bill is Rs.%d",tot);
printf("\nYour bill will be added to the total bill at checkout");
printf("\n\nPress Enter to go back to main menu");
getch();
system("cls");
main();

}
}


void check_out()
{
system("cls");
screenheader();
printf("\n\nAre you sure you want checkout (Y/N)");
scanf(" %c",&choose);

if(choose=='n' || choose=='N')

{
main();
}
else if(choose== 'Y' || choose=='y')
{
system("cls");
screenheader();
g_tot=p_tot+tot;
printf("Total");
printf("%d",g_tot);

}

}

int main()
{


screenheader();
printf("\n\n\nPress Enter To Continue");
getchar();
system("cls");
screenheader();
printf("\n\n\n\n\t\t ************* \n");
printf("\t\t * MAIN MENU * \n");
printf("\t\t ************* \n\n\n");
printf("\t\t\t01. Check In \n");
printf("\t\t\t02.Restaurant\n");
printf("\t\t\t03.Checkout \n");
printf("\n\t\t\t04.Exit");

scanf(" %d",&answr);
switch(answr)
{
case 1:{
check_in();
break; }

case 2:{
restaurant();
break; }

case 3: {
check_out();
}

}


return 0;

}

最佳答案

if condition if I enter value more than 8 it'll bring garbage value to tot.

这符合预期。当z > 8 ,代码尝试访问外部 price[]范围。结果:未定义的行为 (UB)。之前的if (z < 1 || z > 8)没有使代码远离price[z - 1] 。其余代码包括 g_tot = p_tot + tot;现在有疑问。

int price[8] = {180, 120, 65, 55, 70, 70, 110, 200};
...
if (z < 1 || z > 8) {
printf("Invalid food code\n");
}

tot = tot + price[z - 1]; // UB here
...
g_tot = p_tot + tot;

请勿访问price[z - 1]除非z在 [1...8] 范围内。

<小时/>

存在其他问题:最好启用所有编译器警告并亲自查看它们(12+)。

关于c - checkout函数中g_tot计算带来垃圾值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54191907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com