HERE is a C PROGRAM BELOW EVERYTIME I TRY TO RUN IT ON MATRI
HERE is a C PROGRAM BELOW: EVERYTIME I TRY TO RUN IT ON MATRIX IT GIVES ME \" error: ‘for’ loop initial declarations are only allowed in C99 mode\" ERROR. PLEASE HELP ME TO FIX THIS ERROR (I do not want to run with -std=c99 ,want to edit my program)
#include <stdio.h>
#include <stdlib.h>
struct Box {
int id;
double size[3];
double weight;
};
void listBoxes(const char filename[]);
void printBox(struct Box b);
int searchBox(FILE *fp, int id2Find);
void displayBox(const char filename[], int id2Find);
int addBox(const char filename[], const struct Box * b2Add);
int numberBoxes(const char filename[]); // number of boxes in a file
int getRandomInt(int lower, int higher);
void displayBoxN(char filename[], int n); // displaying a box and it\'s given its record number
void listBoxes(const char filename[])
{
FILE *fp = NULL;
int a;
double b, c, d, e;
printf(\"List of boxes\ \");
printf(\"=============\ \ \");
printf(\"ID Length Width Height Weight\ \");
printf(\"-----------------------------\ \");
fp = fopen(\"storage.txt\", \"r\");
if (fp != NULL)
{
for (int i = 1; i <= 5; i++)
{
fscanf(fp, \"%d %lf %lf %lf %lf\", &a, &b, &c, &d, &e);
printf(\"\ %2d %6.2lf %5.2lf %6.2lf %6.2lf\", a, b, c, d, e);
}
printf(\"\ \");
fclose(fp);
}
else
{
printf(\"Error\ \");
}
}
int searchBox(FILE *fp, int id2Find)
{
int a;
double b, c, d, e;
if (fp == NULL)
{
return -1;
}
else
{
rewind(fp);
for (int i = 1; i < 5; i++)
{
fscanf(fp, \"%d %lf %lf %lf %lf\", &a, &b, &c, &d, &e);
if (id2Find == a)
{
return i;
}
}
return -1;
}
}
void displayBox(const char filename[], int id2Find)
{
FILE *fp = NULL;
struct Box b;
int a = 0;
fp = fopen(filename, \"r\");
if (fp != NULL)
{
if (a == -1)
{
printf(\"This box is not recorded.\ \");
}
else
{
a = searchBox(fp, id2Find);
printf(\"Found box %d as record #%d:\ \", id2Find, a);
rewind(fp);
for (int i = 0; i < a; i++) {
fscanf(fp, \"%d %lf %lf %lf %lf\ \", &b.id, &b.size[0], &b.size[1], &b.size[2], &b.weight);
}
printBox(b);
}
}
else
{
printf(\"Fail\");
}
fclose(fp);
}
void displayRandomBox(const char filename[])
{
FILE *fp = NULL;
struct Box b;
int a = 0;
fp = fopen(filename, \"r\");
int id2Find=rand() % (5 + 1 - 1) + 1;
if (fp != NULL)
{
a = searchBox(fp, id2Find);
rewind(fp);
for (int i = 0; i < a; i++)
{
fscanf(fp, \"%d %lf %lf %lf %lf\ \", &b.id, &b.size[0], &b.size[1], &b.size[2], &b.weight);
}
printBox(b);
}
else
{
printf(\"Fail\");
}
fclose(fp);
}
void printBox(struct Box b) {
printf(\"\ ID: %6d\ \"
\"Length: %6.2lf\ \"
\"Width: %6.2lf\ \"
\"Height: %6.2lf\ \"
\"Weight: %6.2lf\ \ \", b.id, b.size[0], b.size[1], b.size[2], b.weight);
}
int addBox(const char filename[], const struct Box * b2Add)
{
FILE *fp = NULL;
fp = fopen(filename, \"a\");
struct Box box;
box = *b2Add;
int c = 0;
if (fp == NULL)
{
printf(\"Error\ \");
return 1;
}
else
{
int a = box.id;
int b = searchBox(fp, a);
if (b != -1)
{
printf(\"A box with this ID is already recorded.\ \");
}
else
{
fprintf(fp, \"\ %d %lf %lf %lf %lf\", box.id, box.size[0], box.size[1], box.size[2], box.weight);
c = 1;
}
fclose(fp);
}
return c;
}
int menu(void) {
int select = -1;
printf(\"1- List all boxes\ \");
printf(\"2- Find a box\ \");
printf(\"3- Add a box\ \");
printf(\"4- Randomly pick a lucky box!\ \");
printf(\"0- Exit program\ \");
printf(\"Select an option: \");
do {
scanf(\"%d\", &select);
if (select < 0 || select > 4)
printf(\"Please enter a number between 0 and 4: \");
} while (select < 0 || select > 4);
return select;
}
int main(void) {
int choice, boxID, a;
char filename[] = \"storage.txt\";
struct Box b;
printf(\"Welcome to My Storage Room\ \");
printf(\"==========================\ \");
do {
choice = menu();
switch (choice) {
case 1:
listBoxes(filename);
break;
case 2:
printf(\"Enter box ID: \");
scanf(\"%d\", &boxID);
displayBox(filename, boxID);
break;
case 3:
printf(\"Please enter the box\'s ID, length, width, height and weight: \");
scanf(\"%d %lf %lf %lf %lf\", &b.id, &b.size[0], &b.size[1], &b.size[2], &b.weight);
a = addBox(filename, &b);
printf(\"%d box added to storage!\ \ \", a);
break;
case 4:
displayRandomBox(filename);
break;
};
} while (choice > 0);
return 0;
}
Solution
/*
in for loop you can\'t declare int directly.
for(int i=0;i<n;i++) -> wrong
int i;
for(i=0;i<n;i++)
*/
#include <stdio.h>
#include <stdlib.h>
struct Box {
int id;
double size[3];
double weight;
};
void listBoxes(const char filename[]);
void printBox(struct Box b);
int searchBox(FILE *fp, int id2Find);
void displayBox(const char filename[], int id2Find);
int addBox(const char filename[], const struct Box * b2Add);
int numberBoxes(const char filename[]); // number of boxes in a file
int getRandomInt(int lower, int higher);
void displayBoxN(char filename[], int n); // displaying a box and it\'s given its record number
void listBoxes(const char filename[])
{
FILE *fp = NULL;
int a;
int i;
double b, c, d, e;
printf(\"List of boxes\ \");
printf(\"=============\ \ \");
printf(\"ID Length Width Height Weight\ \");
printf(\"-----------------------------\ \");
fp = fopen(\"storage.txt\", \"r\");
if (fp != NULL)
{
for (i = 1; i <= 5; i++)
{
fscanf(fp, \"%d %lf %lf %lf %lf\", &a, &b, &c, &d, &e);
printf(\"\ %2d %6.2lf %5.2lf %6.2lf %6.2lf\", a, b, c, d, e);
}
printf(\"\ \");
fclose(fp);
}
else
{
printf(\"Error\ \");
}
}
int searchBox(FILE *fp, int id2Find)
{
int a,i;
double b, c, d, e;
if (fp == NULL)
{
return -1;
}
else
{
rewind(fp);
for (i = 1; i < 5; i++)
{
fscanf(fp, \"%d %lf %lf %lf %lf\", &a, &b, &c, &d, &e);
if (id2Find == a)
{
return i;
}
}
return -1;
}
}
void displayBox(const char filename[], int id2Find)
{
FILE *fp = NULL;
struct Box b;
int a = 0,i;
fp = fopen(filename, \"r\");
if (fp != NULL)
{
if (a == -1)
{
printf(\"This box is not recorded.\ \");
}
else
{
a = searchBox(fp, id2Find);
printf(\"Found box %d as record #%d:\ \", id2Find, a);
rewind(fp);
for (i = 0; i < a; i++) {
fscanf(fp, \"%d %lf %lf %lf %lf\ \", &b.id, &b.size[0], &b.size[1], &b.size[2], &b.weight);
}
printBox(b);
}
}
else
{
printf(\"Fail\");
}
fclose(fp);
}
void displayRandomBox(const char filename[])
{
FILE *fp = NULL;
struct Box b;
int a = 0,i;
fp = fopen(filename, \"r\");
int id2Find=rand() % (5 + 1 - 1) + 1;
if (fp != NULL)
{
a = searchBox(fp, id2Find);
rewind(fp);
for (i = 0; i < a; i++)
{
fscanf(fp, \"%d %lf %lf %lf %lf\ \", &b.id, &b.size[0], &b.size[1], &b.size[2], &b.weight);
}
printBox(b);
}
else
{
printf(\"Fail\");
}
fclose(fp);
}
void printBox(struct Box b) {
printf(\"\ ID: %6d\ \"
\"Length: %6.2lf\ \"
\"Width: %6.2lf\ \"
\"Height: %6.2lf\ \"
\"Weight: %6.2lf\ \ \", b.id, b.size[0], b.size[1], b.size[2], b.weight);
}
int addBox(const char filename[], const struct Box * b2Add)
{
FILE *fp = NULL;
fp = fopen(filename, \"a\");
struct Box box;
box = *b2Add;
int c = 0;
if (fp == NULL)
{
printf(\"Error\ \");
return 1;
}
else
{
int a = box.id;
int b = searchBox(fp, a);
if (b != -1)
{
printf(\"A box with this ID is already recorded.\ \");
}
else
{
fprintf(fp, \"\ %d %lf %lf %lf %lf\", box.id, box.size[0], box.size[1], box.size[2], box.weight);
c = 1;
}
fclose(fp);
}
return c;
}
int menu(void) {
int select = -1;
printf(\"1- List all boxes\ \");
printf(\"2- Find a box\ \");
printf(\"3- Add a box\ \");
printf(\"4- Randomly pick a lucky box!\ \");
printf(\"0- Exit program\ \");
printf(\"Select an option: \");
do {
scanf(\"%d\", &select);
if (select < 0 || select > 4)
printf(\"Please enter a number between 0 and 4: \");
} while (select < 0 || select > 4);
return select;
}
int main(void) {
int choice, boxID, a;
char filename[] = \"storage.txt\";
struct Box b;
printf(\"Welcome to My Storage Room\ \");
printf(\"==========================\ \");
do {
choice = menu();
switch (choice) {
case 1:
listBoxes(filename);
break;
case 2:
printf(\"Enter box ID: \");
scanf(\"%d\", &boxID);
displayBox(filename, boxID);
break;
case 3:
printf(\"Please enter the box\'s ID, length, width, height and weight: \");
scanf(\"%d %lf %lf %lf %lf\", &b.id, &b.size[0], &b.size[1], &b.size[2], &b.weight);
a = addBox(filename, &b);
printf(\"%d box added to storage!\ \ \", a);
break;
case 4:
displayRandomBox(filename);
break;
};
} while (choice > 0);
return 0;
}







