Cajuu'
Cajuu'

Reputation: 1166

C - Stack around the variable 'name' was corrupted

I've tried to handle this error but no success. I have the next function which is supposed to calculate the average age:

float CalculateAverageAge(int ages[],int size, const float &average)
{
    float sumOfAges,averageOfAges;
    sumOfAges=0;

    for(int counter=1;counter<=size;counter++)
    {
        sumOfAges=sumOfAges+ages[counter];
    }
    averageOfAges=sumOfAges/size;

    return averageOfAges;
}

And this one, which is supposed to print some information only from the persons that live in Texas:

void DisplayMembersInTexas(char names[],char states[],int size)
{
    for(int counter=1;counter<=size;counter++)
        if(states[counter]=="Texas")//  here, another error: operand types are incompatible ("char" and "const char *")
            printf("%d members lives in Texas");
}

And the main function:

void main()
{
    char names[50];
    int ages[50];
    char states[50];

    int count;
    float average;

    char name,state;
    int age;

    printf("How many familly members do you have? ");
    scanf("%d",&count);

    for(int counter=1;counter<=count;counter++)
    {
        printf("Please enter member name:");
        scanf("%s",&name);
        printf("Please enter member age:");
        scanf("%d",&age);
        printf("Please enter familly member state:");
        scanf("%s",&state);

        names[counter]=name;
        ages[counter]=age;
        states[counter]=state;

        printf("\n");
    }

    CalculateAverageAge(ages,count,average);
    printf("The average age of all familly members is:",average);
    DisplayMembersInTexas(names,states,count);
}

I receive the following errors:

Any hints on how can I handle these errors ?

Upvotes: 1

Views: 2662

Answers (3)

Christophe
Christophe

Reputation: 73386

THere is a misunderstanding about parameters:

char name,state;   // ATTENTION these are single chars !! Not strings

When you do the input:

 scanf("%s",&name);  // You request for a string but have only place for a single char

all the entered chars will be copied to the memory location starting at name, but not for 1 char, but all the chars entered plus the trailing '\0'. So you overflow your buffer of 1 char, which corrupt the stack (i.e. local memory of the function). For state it's exactly the same.

Solution 1: build on c-style:

char names[50][31];
char states[50][31];
char name[31],state[31];
...
scanf("%30s",name);
...
scanf("%30s",state);
strcpy (names[counter],name);
strcpy (states[counter],state);

As the names and states array are now arrays of array of chars you need to adapt the called function:

void DisplayMembersInTexas(char names[][31], char states[][31], int size) 
{
    int members=0; 
    for (int counter = 1; counter <= size; counter++)
        if (strcmp(states[counter],"Texas")==0)  // they are equal when strcmp()==0 
            members++;
    printf("%d members lives in Texas", members);
}

The main advantage of this approach is that it uses the c input/putput that you know. However, the fact that the C-strings are in fact a null terminated array of chars is cumbersome to manage.

Solution 2: adopt plain C++:

string names[50];
string states[50];
string name,state;
...
cin >> name;
...
cin >> state;
names[counter]=name;
states[counter]=state;

You'd then change the called function accordingly:

void DisplayMembersInTexas(string names[], string states[], int size)
{
     int members=0;
    for (int counter = 1; counter <= size; counter++)
        if (states[counter]=="Texas")
            members++;
    cout << members << " members lives in Texas";
}

This approach requires you to digg a litle bit more into C++ especially in iostreams which is different from what you know. But the c++ string are very powerfull. You can manipulate them almost like build in type, and they have no limits regarding their size.

Upvotes: 2

user4617155
user4617155

Reputation:

When input state (and also for name) like this

scanf("%s",&state);

you broke memory, because state has only one byte. It must be

scanf("%c",&state);

I think your datatypes is inappropriate... state (as well as name) have to be array (char[...]) not just one char, and states should be char[][].

And also use

if( strcmp(states, "Texas") == 0)  // string comparison in C

instead of

if(states[counter]=="Texas")   // address comparison

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385194

Arrays start at index 0, not 1.

Furthermore, you're treating objects of type char as strings, which they are not.

You tagged your question C++, so I suggest using std::string. If you're stuck on C, you're going to need multidimensional arrays and more complicated logic than you've got now, because arrays are not assignable.

Upvotes: 0

Related Questions