void main() {
int y = 0;
char password[30], username[40], total_items[10000], input_item,
ip11[] = "-IPhone 11\n", ipX[] = "-IPhone X\n";
char ap[] = "-AirPods\n", chrg[] = "-Charger\n";
char scrpro[] = "-Screen Protector\n", reset_str[] = " ";
int i = 0, total_amount = 0, auth = 4;
while (auth > 10)
;
{
printf("Enter Username:");
scanf("%s", &username);
fflush(stdin);
printf("Enter password:");
scanf("%s", &password);
fflush(stdin);
if ((strcmp(username, "admin") == 0) &&
(strcmp(password, "admin123") == 0)) {
auth = 7;
} else {
printf("Invalid password or username\n");
printf("%d tries left\n", auth);
auth -= 1;
}
}
while (y > 5) {
printf("Welcome,These are the items that we sell:\n");
printf("-----------------------------------------------\n");
printf("a. IPhone X - $1200 \nb. IPhone 11 - $1500\n");
printf(
"c. AirPods - $300 \nd. Charger - $15 \ne. Screen Protector - "
"$20\n");
printf("\n");
printf("To buy, key in the alphabet beside the item.\n");
printf("Please input 1 item at a time\n");
while (i < 10) {
printf("Enter item wanted(input 'z' to finish cart):");
scanf(" %c", &input_item);
if ((input_item == 'a') || (input_item == 'A')) {
total_amount = 1200;
strcat(total_items, ipX);
} else if ((input_item == 'b') || (input_item == 'B')) {
total_amount = total_amount 1500;
strcat(total_items, ip11);
} else if ((input_item == 'c') || (input_item == 'C')) {
total_amount = total_amount 300;
strcat(total_items, ap);
} else if ((input_item == 'd') || (input_item == 'D')) {
total_amount = total_amount 15;
strcat(total_items, chrg);
} else if ((input_item == 'e') || (input_item == 'E')) {
total_amount = total_amount 20;
strcat(total_items, scrpro);
} else if ((input_item == 'z') || (input_item == 'Z'))
i = 10;
else
printf("Invalid item, please input again\n");
}
printf("Your total is: $%d\n", total_amount);
printf("Your items are:\n%s\n", total_items);
printf("Please collect your receipt and head over to the counter\n");
printf("Thank you for coming to OneTop!\n");
printf("\n");
strcpy(total_items, reset_str);
}
}
I expected the code to proceed to the second while loop but the code instead just crashes.
CodePudding user response:
Incorrect format specifier:
scanf("%s", &username);
username
is a constant pointer to the first element of the array. There's no need to use the &
operator with it.
Stray semicolon:
The statement:
while (auth > 10)
;
is equivalent to saying:
"While auth
is greater than 10
, do nothing."
It's dead code.
The block of code after the while
loop will always execute, irregardless of the value of auth
.
Even if you were to remove the semi-colon, the program would never enter the loop, as:
auth == 4
And the condition:
if (auth > 10)
is false.
Similarly, the second loop would never be entered, as:
y == 0
And the condition:
if (y > 5)
is false.
Flushing stdin:
fflush(stdin);
is undefined behaviour.
- Once the abstract state machine reaches undefined behaviour, no further assumption about the continuation of the execution of the program can be made.
Aside:
Implementation-defined definition of main
:
From C11
:
The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;10) or in some other implementation-defined manner.
CodePudding user response:
Did you check your variables? Your first while loop while(auth > 10);
, but auth = 4. Maybe you meant while(auth < 10);
. Same for while (y > 5)
,, because y = 0