gpt4 book ai didi

c - 使用 struct 和 switch 的意外循环

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

尝试编写一个以选项菜单开始的汽车租赁程序。选择后,应该执行所选选项,但是程序陷入了菜单->用户输入->菜单->用户输入->重复的循环。

我认为它与主函数中的“while ( choice = true )”有关。不过,我尝试过的任何方法都无法将其更改为 false。我也无法执行任何 case 语句中的任何内容。

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

#define true 1
#define false 0

void handleSelection (CompanyT *company, int choice);

void printMenu()
{
printf ("1 Add new car to the inventory.\n");
printf ("2 Make a reservation.\n");
printf ("3 Find a reservation using a renter name and print it to the screen.\n");
printf ("4 Check out a car given a renter name.\n");
printf ("5 Print all available cars.\n");
printf ("6 Calclate and print the average number of days rented.\n");
printf ("7 Exit program.\n");
}
void handleSelection ( CompanyT *company, int choice)
{
double value;
switch ( choice) {
case 1 :
value = 0;
choice = value;
printf ("%s", choice);
break; //optional(?)
case 2 :
printf ("2");
break;
case 3 :
printf ("3");
break;
case 4 :
printf ("4");
break;
case 5 :
printf ("5");
break;
case 6 :
printf ("6");
break;
case 7 :
choice = 0;
printf ("7");
break;

default : printf ("Invalid entry.");
}
printf ("\n");
}

int main ( void )
{
CompanyT myCompany;
int choice;
//add pre-defined list of cars to the list
createInventory ( &myCompany );

while ( choice = true ) {
printMenu();
printf ("Choose option: ");
scanf ("%d", &choice);
handleSelection;
};
printf ("\n");

return 0;
}

完整的程序,如果你想编译它:

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

#define true 1
#define false 0

typedef short int BoolT; //random true/false value

typedef struct { //struct that records a car's information
int carId;
char make[20];
char model[20];
int numDoors;
double rate;
//complete this
} CarT;

typedef struct { //struct that identifies a possible renter
char renterName[20];
//complete this
} RentalT;

typedef struct { //struct that makes a reservation for a predetermined renter
char renterName[20];
//complete this
} ReservT;

typedef struct { //struct that tracks the number of cars and rentals
CarT allCars[20];
RentalT allRentals[20];
//complete this
} CompanyT;

/**
* Prints a menu to the screen.
*/
void printMenu();

/**
* Creates the intial inventory of cars the company owns.
* company - the company whose inventory will be initialized
*/
void createInventory ( CompanyT *company );

/**
* Adds new car to the inventory.
*
* company the company that will add a new res
*/
void addNewCar ( CompanyT *company );

/**
* Creates a new reservation prompting the user for information
*
* company - the company that will add a new reservation
*/
void makeReservation ( CompanyT *company );

/**
* Finds a reservation prompting the user for a rental name to locate the res record.
*
* company - the company whose reservations will be searched
*/
int findReservation ( CompanyT *company );

void handleSelection (CompanyT *company, int choice);



void printMenu()
{
printf ("1 Add new car to the inventory.\n");
printf ("2 Make a reservation.\n");
printf ("3 Find a reservation using a renter name and print it to the screen.\n");
printf ("4 Check out a car given a renter name.\n");
printf ("5 Print all available cars.\n");
printf ("6 Calclate and print the average number of days rented.\n");
printf ("7 Exit program.\n");
}

void handleSelection ( CompanyT *company, int choice)
{
double value;

switch ( choice ) {
case 1 :
choice = 0;
printf ("%s", choice);
break; //optional(?)
case 2 :
printf ("2");
break;
case 3 :
printf ("3");
break;
case 4 :
printf ("4");
break;
case 5 :
printf ("5");
break;
case 6 :
printf ("6");
break;
case 7 :
choice = 0;
printf ("7");
break;

default : printf ("Invalid entry.");

}
printf ("\n");
}

int main ( void )
{
CompanyT myCompany;
int choice;

//add pre-defined list of cars to the list
createInventory ( &myCompany );

while ( choice = true ) {
printMenu();
printf ("Choose option: ");
scanf ("%d", &choice);
handleSelection;
};


printf ("\n");

return 0;
}
void createInventory ( CompanyT *company )
{
(*company).allCars[0].carId = 1234;
strcpy ((*company).allCars[0].make, "Vw");
strcpy ((*company).allCars[0].model, "Golf");
(*company).allCars[0].numDoors = 2;
(*company).allCars[0].rate = 66.0f;

//complete this
}

void addNewCar ( CompanyT *company )
{
//complete this
}

最佳答案

问题出在 while ( choice = true ) 语句中。

choice = true 是一个使 choice 为 true 的赋值。结果将是赋值的结果,所以你得到while(true),这是一个无限循环。

在这种情况下,您真正​​需要的是 do while 循环,因为它将至少运行一次,然后检查 choice 的值

do {
printMenu();
printf ("Choose option: ");
scanf ("%d", &choice);
handleSelection;
} while ( choice == true );

关于c - 使用 struct 和 switch 的意外循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40519841/

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