Тема: Виправте помилку
Даний код:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
struct Car {
char producer[1024];
int model;
double price;
char power[1024];
int year;
};
struct Car createCar(char producer[1024], int model, double price, char power[1024], int year) {
struct Car c;
strcpy(c.producer, producer);
c.model = model;
c.price = price;
strcpy(c.power, power);
c.year = year;
return c;
}
struct Car *readData() {
FILE *fp = fopen("Cars.txt", "r");
if(fp == NULL) {
printf("Cannot open file.\n");
exit(1);
}
int SIZE;
fscanf(fp, "%i\n", &SIZE);
struct Car *cars = (struct Car*)malloc(sizeof(struct Car) * SIZE);
for(int i = 0; i < SIZE; i++) {
char c[1024];
fscanf(fp, "%s", &c);
char producer[1024];
if(c[0] == 'P') {
fscanf(fp, "%s", &producer);
}
fscanf(fp, "%s", &c);
int model;
if(c[0] == 'M') {
fscanf(fp, "%i", &model);
}
fscanf(fp, "%s", &c);
double price;
if(c[0] == 'P') {
fscanf(fp, "%d", &price);
}
fscanf(fp, "%s", &c);
char power[1024];
if(c[0] == 'P') {
fscanf(fp, "%s", &power);
}
fscanf(fp, "%s", &c);
int year;
if(c[0] == 'Y') {
fscanf(fp, "%i", &year);
}
cars[i] = createCar(producer, model, price, power, year);
}
fclose(fp);
return cars;
}
void printFl(struct Car c) {
printf("Producer: %s \tModel: %i \tPrice: %i$ \tPower: %s \tYear: %d \n", c.producer, c.model, c.price, c.power, c.year);
}
void printData(struct Car *cars, int SIZE) {
for(int i = 0; i < SIZE; i++) {
struct Car c = cars[i];
printf("%i) ", (i+1));
printFl(c);
}
printf("\n");
}
int addElement(struct Film *films, int SIZE) {
printf("Enter a producer: ");
char producer[1024];
scanf("%s", &producer);
printf("Enter a model: ");
int model;
scanf("%i", &model);
printf("Enter a price: ");
double price;
scanf("%d", &price);
printf("Enter a power: ");
char power[1024];
scanf("%s", &power);
printf("Enter a year: ");
int year;
scanf("%i", &year);
cars[SIZE] = createCar(producer, model, price, power, year);
SIZE++;
printf("Element added to array!\n");
return SIZE;
}
struct Car *removeElement(struct Car *cars, int SIZE) {
int position = 0;
while(position == 0 position >= SIZE+1) {
printf("Enter a number: ");
scanf("%i", &position);
if(position >= SIZE+1)
printf("Deletion not possible.\n");
}
for(int c = position - 1; c < SIZE - 1; c++)
cars[c] = cars[c+1];
printf("Element deleted from array!\n");
return cars;
}
struct Car *changeElement(struct Car *cars, int SIZE) {
int position = 0;
while(position == 0 position >= SIZE+1) {
printf("Enter a number: ");
scanf("%i", &position);
if(position >= SIZE+1)
printf("Change not possible.\n");
}
struct Car c = cars[position-1];
printf("Change indexes: \n");
printf("1 - Price \n");
printf("2 - Power \n");
printf("3 - Year \n");
int index = -1;
while(true) {
printf("Enter a change index: ");
scanf("%i", &index);
if(index == 1) {
printf("Enter a price: ");
double price;
scanf("%d", &price);
c.price = price;
continue;
}
if(index == 2) {
printf("Enter a model: ");
char model[1024];
scanf("%s", &model);
strcpy(c.model, model);
continue;
}
if(index == 3) {
printf("Enter a year: ");
int year;
scanf("%i", &year);
c.year = year;
continue;
}
printf("Change index not found\n");
}
cars[position-1] = c;
printf("Element changed!\n\n");
return cars;
}
void Sort(struct Car *data, int size, int sort) {
int i, j;
for(i = 0; i < size; ++i) {
for(j = size - 1; j > i; ++j) {
if((sort == 1 && data[j].year < data[j+1].year) (sort == 2 && data[j].model < data[j+1].model) (sort == 3
&& data[j].price < data[j+1].price)) {
struct Car t = data[j];
data[j] = data[j+1];
data[j+1] = t;
}
}
}
}
void sortElement(struct Car *cars, int SIZE) {
printf("Sort indexes: \n");
printf("1 - By year \n");
printf("2 - By model \n");
printf("3 - By price \n");
int index = 0;
while(true) {
printf("Enter a sort index: ");
scanf("%i", &index);
if(index >= 1 && index <= 3) {
break;
} else {
printf("Sort index not found\n");
}
}
Sort(cars, SIZE, index);
printf("Sorted!\n\n");
printData(cars, SIZE);
}
bool startsWith(const char *pre, const char *str) {
size_t lenpre = strlen(pre),
lenstr = strlen(str);
return lenstr < lenpre ? false : strncmp(pre, str, lenpre) == 0;
}
void findElement(struct Car *cars, int SIZE) {
printf("Find indexes: \n");
printf("1 - Price \n");
printf("2 - Model \n");
printf("3 - Power \n");
int index = 0;
while(true) {
printf("Enter a find index: ");
scanf("%i", &index);
if(index >= 1 && index <= 4) {
break;
} else {
printf("Find index not found\n");
}
}
if(index == 1) {
printf("Enter a price: ");
double price;
scanf("%d", &price);
int finds = 0;
for(int i = 0; i < SIZE; i++) {
struct Car c = cars[i];
if(c.price == price) {
finds++;
printFl(c);
}
}
if(finds == 0) {
printf("No items found\n");
}
}
if(index == 2) {
printf("Enter a model: ");
int model;
scanf("%i", &model);
int finds = 0;
for(int i = 0; i < SIZE; i++) {
struct Car c = cars[i];
if(c.model == model) {
finds++;
printFl(c);
}
}
if(finds == 0) {
printf("No items found\n");
}
}
if(index == 3) {
printf("Enter a producer: ");
char producer[1024];
scanf("%s", &producer);
int finds = 0;
for(int i = 0; i < SIZE; i++) {
struct Car c = cars[i];
if(startsWith(producer,c.producer)) {
finds++;
printFl(c);
}
}
if(finds == 0) {
printf("No items found\n");
}
}
}
int main() {
int SIZE;
struct Car *cars;
SIZE = 4;
cars = (struct Car*)malloc(sizeof(struct Car) * SIZE);
cars = readData();
printData(cars, SIZE);
addElement(cars, SIZE);
removeElement(cars, SIZE);
changeElement(cars, SIZE);
sortElement(cars, SIZE);
findElement(cars, SIZE);
return 0;
}
На даний момент видає ось таку помилку:
error: 'cars' undeclared (first use in this function); did you mean 'Car'?
Допоможіть будь ласка виправити