I am having a problem setting a variable entered by the user. I entered something in and it stores another value. Pretty frustrating. So any guidance would be great.
Be advised, there are quite a few printf()
, as I was trying to pinpoint the problem. Also I am still trying to get a hold of C.
#include <stdio.h>
int vehicletype, in, out; // User Entered Info
int vehicleID[5000];
float vehicleCharge[5000];
int i;
// Variables for Main Function
int q; // Loop Control
float z;
int main (){
for(q = 1; q != 1518944; q++) {
printf("Enter your position in the parking queue: ");
// Take the queue entered by the user and assign it to i
scanf("%d\n", &i);
// Take the user input(which is being held in the variable i) and place it into the 'i'
//position of the ID array
vehicleID[q] = i;
printf("Enter the time(past 0600) you wish to start parking: \n");
//take the time and pass it to the time function to determine roundup
scanf("%d\n", &in);
printf("Enter the time(before 2200) you wish to leave: \n");
scanf("%d\n", &out);
printf("Time in: %d\nTime out: %d\n", in, out);
}
return 0;
}
@M.M I should be able to enter 0617 into the "in" variable and 1547 for the "out" (I use this later to find out how much they parked) but the output I get when checking the variables by printing "in" and "out" is 1 and 399 respectively.
Here is some more or less working code. I don't understand the 1518944 limit, but the code takes steps to ensure that regardless of how many entries you make, the code won't write out of bounds of the array vehicleID
. It also checks some numbers for validity, and echoes its inputs. The leading newlines on some of the outputs makes the output appear semi-sane when the data is written via another program (a random number generator was what I used to generate numbers 1-5000 and two times 0600-2200).
#include <stdio.h>
static int vehicleID[5000];
int main(void)
{
for (int q = 1; q != 1518944; q++)
{
int in, out;
int i;
printf("\nEnter your position in the parking queue: ");
if (scanf("%d", &i) != 1)
break;
vehicleID[q % 5000] = i;
printf("Enter the time (past 0600) you wish to start parking: ");
if (scanf("%d", &in) != 1)
break;
printf("Enter the time (before 2200) you wish to leave: ");
if (scanf("%d", &out) != 1)
break;
if (in < 600 || in > 2200 || out < 600 || out > 2200 || in >= out)
printf("Invalid times (in %.4d, out %.4d)\n", in, out);
else
printf("\nPosition: %d; Time in: %.4d; Time out: %.4d\n", i, in, out);
}
putchar('\n');
return 0;
}
Note that inputs are checked, and echoed. Checking is a crucial programming technique; printing (echoing) is a basic debugging technique.