Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am trying to finish this program which is about using dynamic memory to store an animal in a specific cage and then be called again using commands 1 through 4. but when I try to run the program it tells me that there's an error in line 48 that "i" is not declared.. I am stuck there and I could not figure out why that error is showing

 <pre>// Include the header files

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Structures for cages and sections

typedef struct Cage Cage;
typedef struct Section Section;

// A structure to store animals in a cage

struct Cage {
        char animal [1001];
} cage;

// A structure to store cages in a section
// This struct was copied from the Assignment 1 PDF file

struct Section {
        struct Cage *cages;
        int numCages;
} *sections;

int numSections, type, nSection, nCage;
char nAnimal [1001];



// Add main function
// Add commands "type"
// Command "1" is to add sections and cages.
// Command "2" is to add an animal to a section/cage.
// Command "3" is to print the animal.
// Command "4" is to end the program.

int main(){

    scanf ("%d", &numSections);

// Dynamic memory for numSections

sections = malloc (sizeof(struct Section) * numSections);

for (int i = 0; i < numSections; i++); {

    sections [i].cages = NULL;
    sections [i].numCages = 0;

}

    while
        (type !=4){
        scanf("%d %d", &nSection, &nCage);

        if (type == 1){

            if (nSection > numSections) continue;
            sections [nSection - 1].cages = realloc (sections [nSection - 1].cages, sizeof(Cage) * nCage);
            for (int i = sections [nSection - 1].numCages; i < nCage; i++)
              strcpy (sections[nSection - 1].cages [i].animal, "No animal found");
              sections [nSection - 1].numCages = nCage;
              break;
        }

        if (type == 2){

            scanf("%d %d", &nSection, &nCage);
            if (nSection > numSections) continue;
            if (nCage > sections [nSection - 1].numCages) continue;

            scanf ("%s\n", sections [nSection - 1].cages [nCage -1].animal);
            break;
        }

        if (type == 3){

            scanf("%d %d", &nSection, &nCage);
            if ((nSection > numSections) || (nCage > sections [nSection - 1].numCages)){
                printf("No animal found");
                continue;
            }
            printf ("%s\n", sections [nSection -1].cages [nCage - 1].animal);
            break;
        }

    }while (type !=4);

    for (int i = 0; i < numSections; i++) free (sections [i].cages);
    free (sections);

    return 0;
}


What I have tried:

I could not understand why it shows "i" as undeclared, especially that this is the only error that shows so I did not want to change a whole bunch of the code so I won't get more errors.
Posted
Updated 12-Sep-22 14:12pm

1 solution

C
for (int i = 0; i < numSections; i++); {
The problem is the last semicolon on that line. It ends the for statement and the scope of the variable i is limited to that for statement so it falls out of scope after the semicolon and is no longer defined.

This will work better :
C
for (int i = 0; i < numSections; i++) {
 
Share this answer
 
v2
Comments
CPallini 13-Sep-22 2:09am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900