尝试编写一个以选项菜单开始的汽车租赁计划。选择后,应该执行所选的选项,但是,程序会卡在菜单循环中 - >用户输入 - >菜单 - >用户输入 - >重复。
我认为它与main函数中的'while(choice = true)'有关。但是,我尝试过的任何东西都没有改变它。 我也无法在任何要执行的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
}
答案 0 :(得分:0)
=
是赋值运算符。 ==
是等式检查。
while ( choice = true ) {
应该是
while (choice == true) {
或更好
while(choice) {
另请注意,您使用choice
时没有给它任何值。在C中,这意味着它可以具有任何值(无论发生在该内存位置) - 即这是未定义的行为。
答案 1 :(得分:0)
问题出在while ( choice = true )
声明中。
choice = true
是一项可以选择为真的作业。结果将是赋值的结果,因此得到while(true)
,这是一个无限循环。
在这种情况下你真正需要的是while while循环,因为它至少运行一次然后检查choice
的值
do {
printMenu();
printf ("Choose option: ");
scanf ("%d", &choice);
handleSelection;
} while ( choice == true );