Annalynn Annalynn - 27 days ago 17
C Question

Unintended loop using struct and switch

Trying to write a car rental program which starts out with a menu of options. After picking, the selected option should execute, however, the program is stuck in a loop of menu -> user input -> menu -> user input -> repeat.

I assume it has something to do with the 'while ( choice = true )' in the main function. Nothing I've tried changes it to false, though.
I'm also having trouble getting anything in any of the case statements to execute.

#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;
}


full program, if you're trying to compile it:

#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
}

Answer

The problem is in the while ( choice = true ) statement.

choice = true is an assignment which will make choice to be true. The result will be the the result of the assignment, So you get while(true), which is an infinite loop.

What you really need in this situation is a do while loop, as this will run at least once and then examine the value of choice

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