我试图将char
数组传递给我创建的函数,以便我可以看到两个汽车ID是否相等。我创建的函数称为mpg
,它接受char *id
。如果传递给函数的ID等于任何汽车的ID,它会打印出1.顺便提一下ID也是一个字符串。我在C语言编程方面很陌生,所以任何帮助都会很好。有任何问题请询问。
汽车信息栏分别是数字,ID,里程表,加仑。
CAR信息
0 987654 201200 4.000000
1 red 89114 0.000000
2 red 89712 13.500000
3 red 90229 15.300000
4 987654 201001 0.000000
5 987654 201111 5.200000
6 987654 201612 25.299999
7 red 89300 7.100000
8 green 16 0.000000
9 green 216 20.000000
10 green 518 61.000000
11 green 879 50.000000
CODE
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#define numberOfRecords 12
struct record
{
char *id;
int odometer;
float gallons;
};
typedef struct record gasRecord;
gasRecord gasDataArray[numberOfRecords];
void mpg(char*);
int main(int argc, char *argv[])
{
FILE *fileInput;
float gallons;
int element, odometer;
char id[10];
char test[10] = {"987654"};
fileInput = fopen("gasData.txt", "r");
if (fileInput == NULL)
{
printf("cannot open `gasData.txt'\n");
return -1;
}
for(element = 0; element < numberOfRecords; element++)
{
if(feof(fileInput))
{
printf("end-of-file detected on file\n");
exit(1);
}
if (fscanf(fileInput, "%s %d %f", id, &odometer, &gallons) == 3)
{
gasDataArray[element].id = malloc (strlen(id)+1);
strcpy(gasDataArray[element].id, id);
gasDataArray[element].odometer = odometer;
gasDataArray[element].gallons = gallons;
printf("Record #: %d id = %s, odometer = %d, gallons = %f\n",
element, gasDataArray[element].id, gasDataArray[element].odometer, gasDataArray[element].gallons);
}
}
printf("Test ID = %s\n", test);
mpg(test);
return 0;
}
void mpg(char *id)
{
int i;
for(i = 0; i < numberOfRecords; i++)
{
if (gasDataArray[i].id == id)
{
printf("1");
}
}
}
已编辑的代码
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#define numberOfRecords 12
struct record
{
char *id;
int odometer;
float gallons;
};
typedef struct record gasRecord;
gasRecord gasDataArray[numberOfRecords];
void mpg(char*);
int main(int argc, char *argv[])
{
FILE *fileInput;
float gallons;
int element, odometer;
char id[10];
char test[10] = {"red"};
fileInput = fopen("gasData.txt", "r");
if (fileInput == NULL)
{
printf("cannot open `gasData.txt'\n");
return -1;
}
for(element = 0; element < numberOfRecords; element++)
{
if(feof(fileInput))
{
printf("end-of-file detected on file\n");
exit(1);
}
if (fscanf(fileInput, "%s %d %f", id, &odometer, &gallons) == 3)
{
gasDataArray[element].id = malloc (strlen(id)+1);
strcpy(gasDataArray[element].id, id);
gasDataArray[element].odometer = odometer;
gasDataArray[element].gallons = gallons;
printf("Record #: %d id = %s, odometer = %d, gallons = %f\n",
element, gasDataArray[element].id, gasDataArray[element].odometer, gasDataArray[element].gallons);
}
}
printf("Test ID = %s\n", test);
mpg(test);
return 0;
}
void mpg(char *id)
{
int i, minMile = 0, maxMile = 0;
for(i = 0; i < numberOfRecords; i++)
{
if (strcmp(id, gasDataArray[i].id))
{
printf("1");
}
}
}
答案 0 :(得分:0)
您无法在C语言中将字符串与==
进行比较。 - alain
我刚刚使用了strcmp,它运行得很好。我只是习惯于C ++,这种编程对我来说是新的。谢谢阿兰 - 约翰尼安德里亚