Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用struct和switch的非预期循环_C_Loops_Struct_Switch Statement - Fatal编程技术网

使用struct和switch的非预期循环

使用struct和switch的非预期循环,c,loops,struct,switch-statement,C,Loops,Struct,Switch Statement,试着写一个租车程序,从一个选项菜单开始。拾取后,应执行所选选项,但程序卡在菜单->用户输入->菜单->用户输入->重复的循环中 我假设它与主函数中的“while(choice=true)”有关。不过,我尝试过的任何东西都无法将其更改为false。 我也很难执行任何case语句中的任何内容 #include <stdio.h> #include <string.h> #define true 1 #define false 0 void handleSelection

试着写一个租车程序,从一个选项菜单开始。拾取后,应执行所选选项,但程序卡在菜单->用户输入->菜单->用户输入->重复的循环中

我假设它与主函数中的“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;
}
#包括
#包括
#定义真1
#定义false 0
无效handleSelection(CompanyT*company,int-choice);
无效打印菜单()
{
printf(“1将新车添加到库存中。\n”);
printf(“2进行预订。\n”);
printf(“3使用承租人名称查找预订并将其打印到屏幕上。\n”);
printf(“4查看给定租车人姓名的汽车。\n”);
printf(“5打印所有可用车辆。\n”);
printf(“6计算并打印平均租用天数。\n”);
printf(“7退出程序。\n”);
}
无效handleSelection(CompanyT*company,int-choice)
{
双重价值;
开关(选择){
案例1:
数值=0;
选择=价值;
printf(“%s”,选项);
break;//可选(?)
案例2:
printf(“2”);
打破
案例3:
printf(“3”);
打破
案例4:
printf(“4”);
打破
案例5:
printf(“5”);
打破
案例6:
printf(“6”);
打破
案例7:
选择=0;
printf(“7”);
打破
默认值:printf(“无效条目”);
}
printf(“\n”);
}
内部主(空)
{
我公司;
智力选择;
//将预定义的车辆列表添加到列表中
createInventory(&myCompany);
while(choice=true){
打印菜单();
printf(“选择选项:”);
scanf(“%d”,选择(&C);
无柄选举;
};
printf(“\n”);
返回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
}
#包括
#包括
#定义真1
#定义false 0
typedef短int BoolT//随机真/假值
typedef struct{//struct,记录汽车信息
内龋;
charmake[20];
char模型[20];
int numDoors;
双倍费率;
//完成这个
}推车;
typedef结构{//struct,用于标识可能的租用者
char renterName[20];
//完成这个
}伦塔特;
typedef struct{//struct,为预定的租用者进行预订
char renterName[20];
//完成这个
}储备;
typedef struct{//struct,用于跟踪汽车数量和租金
推车所有汽车[20];
租金所有租金[20];
//完成这个
}公司;
/**
*将菜单打印到屏幕上。
*/
作废打印菜单();
/**
*创建公司拥有的汽车的初始库存。
*公司-将初始化其库存的公司
*/
作废存货(公司*公司);
/**
*将新车添加到库存中。
*
*公司将添加新资源的公司
*/
void addNewCar(CompanyT*公司);
/**
*创建新预订,提示用户输入信息
*
*公司-将添加新预订的公司
*/
作废预约(公司*公司);
/**
*查找一个预订,提示用户输入租赁名称以定位res记录。
*
*公司-将搜索其预订的公司
*/
国际金融服务(公司*公司);
无效handleSelection(CompanyT*company,int-choice);
无效打印菜单()
{
printf(“1将新车添加到库存中。\n”);
printf(“2进行预订。\n”);
printf(“3使用承租人名称查找预订并将其打印到屏幕上。\n”);
printf(“4查看给定租车人姓名的汽车。\n”);
printf(“5打印所有可用车辆。\n”);
printf(“6计算并打印平均租用天数。\n”);
printf(“7退出程序。\n”);
}
无效handleSelection(CompanyT*company,int-choice)
{
双重价值;
开关(选择){
案例1:
选择=0;
printf(“%s”,选项);
break;//可选(?)
案例2:
printf(“2”);
打破
案例3:
printf(“3”);
打破
案例4:
printf(“4”);
打破
案例5:
printf(“5”);
打破
案例6:
printf(“6”);
打破
案例7:
选择=0;
printf(“7”);
打破
默认值:printf(“无效条目”);
}
printf(“\n”);
}
内部主(空)
{
我公司;
智力选择;
//将预定义的车辆列表添加到列表中
createInventory(&myCompany);
while(choice=true){
打印菜单();
printf(“选择选项:”);
scanf(“%d”,选择(&C);
无柄选举;
};
printf(“\n”);
返回0;
}
作废createInventory(公司Y*公司)
{
(*公司).allCars[0].carId=1234;
strcpy((*company).allCars[0]。make,“Vw”);
strcpy((*company).allCars[0].模型,“Golf”);
(*公司).allCars[0].numDoors=2;
(*公司)。所有车辆[0]。费率=66.0f;
//完成这个
}
void addNewCar(CompanyT*company)
{
//完成这个
}

=
是赋值运算符<代码>=是相等检查

while ( choice = true ) { 
应该是

while (choice == true) {
或者更好

while(choice) {
还请注意,您使用的
选项
没有赋予任何值。在C语言中,这意味着它可以有任何值(无论发生在该内存位置的是什么),也就是说,这是未定义的行为
do {
    printMenu();
    printf ("Choose option: ");
   scanf ("%d", &choice);  
   handleSelection;
} while ( choice == true );