Reputation: 115
I am working on a query program to search for specific vehicle model from a CSV file. The format of CSV file is as follows:
2000,VOLVO,V70 T5 TURBO,STATION WAGON,2.3,A4,Z,12.4,8.0,2084,4793
2000,VOLVO,V70R AWD TURBO,STATION WAGON,2.4,A5,Z,13.1,9.2,2269,5219
2001,ACURA,3.2TL,MID-SIZE,3.2,AS5,Z,12.3,7.4,2019,4644
2001,ACURA,INTEGRA,SUBCOMPACT,1.8,A4,X,10.0,7.1,1739,4000
...
2014,VOLVO,XC90 AWD,SUV - STANDARD,3.2,AS6,X,13.3,8.6,2240,258
The struct of CSV is CarRecord in the code below.
Each year has zero or one record that matches the query. So if I find record in 2000 matches, print out the data, and jump to 2001 to continue searching, if 2001 has no record that match the query, print out "2001 No record", can anyone help me figure out where do I insert "2001 No record" in my code?
struct CarRecord{
int year;
char make[20];
char model[40];
char type[30];
float engineSize;
char transmissionType[4];
char fuelType;
float city;
float hwy;
float fuelPerYear;
float co2;
}data[14500];
struct QueryS{
char make[20];
char model[40];
char transmissionType[4];
float engineSize;
}squery[100];
int x=0;
int ctr=0;
int compareyear=2000;
while (compareyear==data[x].year){ //assume that there are no more than 14500 records.
for(x=0;x<14500;x++){
if (strcmp(squery[ctr].make,data[x].make) == 0 && strcmp(squery[ctr].model, data[x].model) == 0 && strcmp(squery[ctr].transmissionType, data[x].transmissionType) == 0 && squery[ctr].engineSize==data[x].engineSize){
fprintf(otreport," %4d | %14.1f | %17.1f | %21.0f | %10.0f \n", data[x].year, data[x].city, data[x].hwy, data[x].fuelPerYear, data[x].co2);
compareyear++;
x++;
}
}
}
Upvotes: 0
Views: 95
Reputation: 4357
If compareyear != data[x].year
then your code enters an infinite loop, since x
is not incremented in the first loop. I would add x++
to both loops.
Also, compareyear == compareyear == data[x].year
is probably not what you want. compareyear == compareyear
evaluates to 1
, so the entire expression only evaluates to true
if 1 == data[x].year
. Rewrite the expression as compareyear == data[x].year
I'm also not entirely sure why you have a nested loop in the first place, there's no need for it in your code.
Upvotes: 2