context: I have this college assignment I should something like rush hour game.. I have a park lot with cars in it, with a specific car (symboled with *) that should leave the lot (exit is at the middle line extreme left column). I should move the cars and get it out. for example, here is a given lot:
|1||1|| || || |
| || ||2||2||2|
| || ||*||*||*|
|3||3|| || || |
|3||3|| || || |
First thing, I need to ask the user to select a car to move, check if it even do exist in the lot, and if not, to ask him again to select a car. Here is the SelectCar
function:
char SelectCar(char lot[MAX_LOT_LENGTH][MAX_LOT_LENGTH], int Length){
char car;
bool exists = false;
printf("Enter the car you want to move:\n");
scanf("%c",&car);
for (int i = 0; i< Length; i ){
for (int j = 0; j<Length; j ){
if (lot[i][j] == car) {
exists = true;
}
}
}
if(!exists) {
printf("Invalid car id! enter again:\n");
car = SelectCar(lot,Length);
}
printf("car selected\n");
return car;
}
When I run the code, it skips the scanf
function (It doesn't even ask to enter a car), so when it goes the for loop, "car"
doesn't even exist in the lot, so it re-enters the SelectCar
function, and when it reaches the scanf
(the second time) it just crashes and gives me this error":
/home/p10303/.vpl_launcher.sh: line 12: 1904456 Segmentation fault (core dumped) ./vpl_execution
I'm not sure what to do (I don't even understand what the issue is)
note: lot and length are defined and received by the user..
CodePudding user response:
I'm not sure what to do
Save time and enable all compiler warnings to quickly see troubles with char car; ... scanf("%d",car);
"%d"
in scanf()
expects a matching pointer to an int
.