C++, Trying to create a program to store animals in a cage, stuck on line 48

Viewed 43

I am trying to code a program to store animals in a specific cage and then print the animals' name using commands 1 through 4, but when I try to test the code it shows me one error in line 48 that "i" is not declared. Not sure why it shows that and I do not know how to fix it, and I am scared to change a bunch of codes as that will probably mess with the whole code and shows much more errors, any advice is appreciated!.


// 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;
}

2 Answers

in 46'th line:

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

remove ; (I assume You placed it by mistake)

In your code on lines 47 through 52:

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

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

}

You have an erroneous semicolon in your for loop, making the body of the for loop not in the scope of the for loop.

Related