Мои переменные:
struct car
{
char registration[7];
char model[30];
char colour[30];
int num;
bool reserved;
long amount;
};
Следующим образом я собираю данные про машину:
for (i = 0; i < 1; i++) {
printf("**************Car %d:**************\n", i + 1);
while (valid_input == 0) {
printf("Enter registration number (format: yyDnnnn): \n");
scanf("%s", aRegistration);
int len = strlen(aRegistration);
if (len == 7){
if (((aRegistration[0] == '0' && aRegistration[1] >= '1' && aRegistration[1] <= '9') ||
(aRegistration[0] == '1' && aRegistration[1] >= '0' && aRegistration[1] <= '4')) && (aRegistration[2] == 'D')
&& ((isdigit(aRegistration[3]) >0 ) && (isdigit(aRegistration[4]) > 0) && (isdigit(aRegistration[5])>0) &&
(isdigit(aRegistration[6]) >0)))
valid_input = 1;
else printf("\007Error: Do not match the format!\n");
}
}
printf("Enter model: \n");
scanf("%s", aModel);
printf("Enter colour:\n");
scanf("%s", aColour);
do {
printf("Enter number: \n");
scanf("%d", &aNum);
} while (aNum > 3);
printf("Car is not reserved at this momemnt\n");
aReserved = false;
printf("Amount is 0, because car is unreserved.\n");
anAmount = 0;
}
Так же у меня есть два метода, которые помогают зарезервировать машину и наоборот отказаться от резервации. И они работают!
bool reserve(char* reg) {
long anAmount;
struct LinearNode *current, *previous;
bool notFound = true;
if (isEmpty())
printf("Error - there are no cars in the list\n");
else {
for (current = front; current != NULL; current = current->next)
{
if (strcmp(reg, current->element->registration) == 0) {
if (current->element->reserved == false) {
printf("Write a deposit amount of your choice (more than 500 and less than 1500)\n");
scanf("%li", &anAmount);
if (anAmount >= 500 && anAmount <= 1500) {
current->element->amount = anAmount;
current->element->reserved = true;
}
printf("Now you are reserved this car!\n");
return true;
}
else {
printf("Car is already reserved!\n");
return false;
}
}
else {
printf("This car does not exist\n");
}
}//end for loop
}
}
bool unreserve(char* reg) {
int answer;
struct LinearNode *current, *previous;
if (isEmpty())
printf("Error - there are no cars in the list\n");
else {
for (current = front; current != NULL; current = current->next)
{
if (strcmp(reg, current->element->registration) == 0) {
if (current->element->reserved == true) {
printf("Do you want to unreserve this car?\n");
printf("Press [1] if yes, and [0] if no.\n");
scanf("%d", &answer);
if (answer == 1) {
current->element->reserved = false;
current->element->amount = 0;
// return false;
}
else {
printf("Car remains reserved!");
// return true;
}
}
}
//else {
//printf("This car does not exist");
//}
}
}
}
И теперь наступает момент, когда if statement не срабатывает.
Я пытаюсь продать машину. Но продать машину можно только ту, что зарезервирована. А в итоге продается любая машина, независимо от моего ограничения.
вот данный метод:
void deleteCar(char* reg) {
struct LinearNode *current, *previous;
bool found = false;
if (isEmpty())
printf("Error - there are no nodes in the list\n");
else {
current = previous = front;
while (!found && current != NULL) {
if ((strcmp(reg, current->element->registration) == 0) && (current->element->reserved = true)){
found = true;
} else {
previous = current;
current = current->next;
}//end else
} //end while
if (!found)
printf("Error - there is not such car with registration number %s\n", reg);
else {
if (current == front) {
front = front->next;
free(current);
} //end else
else {
previous->next = current->next;
free(current);
} //end else
printf("Car with registration number %s has been deleted\n", reg);
}//end else
}//end else
}// end deleteNode
не выполняется следующее условие:
current->element->reserved = true
в while loop