C: Error with stack around the variable 's' was corrupted

AirWick

I'm having an error with my code when executed about the variable 's' being corrupted. I believe it's something with saving the bin or the text. I've tried editing more with the text, but can't figure it out thinking it might be the bin or something I must have mistyped or added on accident.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 4

struct Person
{
    char name[16], dep[16];
    float cyi, ra, rp, npa, tyi, tra, tpa;
};

void load(struct Person s[], int n)
{
    for (int i = 0; i < n; i++)
    {
        printf("Enter your name   ");
        gets(s[i].name);
        printf("Enter your department   ");
        gets(s[i].dep);
        printf("Enter your current yearly income   $");
        scanf("%f", &s[i].cyi);
        printf("Enter your raise percentage   ");
        scanf("%f", &s[i].rp);
        s[i].ra = (s[i].cyi * s[i].rp) / (float)100;
        s[i].npa = (s[i].cyi) + (s[i].ra);
        printf("\n");

        fflush(stdin);
    }
}

void sort(struct Person s[], int n)
{
    int i, j;
    Person t;
    for (i = 0; i < n - 1; i++)
        for (j = 0; j < n - 1; j++)
            if (strcmp(s[j].name, s[j + 1].name) > 0)
            {
        t = s[j];
        s[j] = s[j + 1];
        s[j + 1] = t;
            }
}

void print(struct Person s[], int n)
{
    printf("\n\n");
    for (int i = 0; i < n; i++)
    {
        printf("%s in department %s\n", s[i].name, s[i].dep);
        printf("The current yearly income is $%0.2f the raise percentage is %0.2f%%\n", s[i].cyi, s[i].rp);
        printf("The raise amount is $%0.2f, the new pay amount is $%0.2f\n\n", s[i].ra, s[i].npa);
    }
}

void calc(struct Person s[], int n)
{
    float tyi = 0, tra = 0, tpa = 0;
    for (int i = 0; i < n; i++)
    {
        tyi += s[i].cyi;
        tra += s[i].ra;
        tpa += s[i].npa;
    }
    printf("The total current yearly income is $%0.2f\n", tyi);
    printf("The total raise amount is $%0.2f\n", tra);
    printf("The total new pay amount is $%0.2f\n", tpa);
}

void savetext(struct Person s[], int n)
{
    int i;
    FILE *f;
    f = fopen("G:\\College\\CS 36\\Projects\\Final Program\\FinalHomework\\info.txt", "w");
    for (i = 0; i < n; i++)
    {
        fprintf(f, "%s\n", s[i].name);
        fprintf(f, "%s\n", s[i].dep);
        fprintf(f, "%f %f %f\n", s[i].cyi, s[i].rp, s[i].npa);
    }
    fclose(f);
}

void retrievetext(struct Person s[], int n)
{
    int i;
    FILE *f;
    f = fopen("G:\\College\\CS 36\\Projects\\Final Program\\FinalHomework\\info.txt", "r");
    for (i = 0; i < n; i++);
    {
        fgets(s[i].name, sizeof(s[i].name), f);
        fgets(s[i].dep, sizeof(s[i].dep), f);
        fscanf(f, "%f%f%f\n", &s[i].cyi, &s[i].rp, &s[i].npa);
    }
    fclose(f);
}

void savebin(struct Person s[], int n)
{
    FILE *f;
    f = fopen("G:\\College\\CS 36\\Projects\\Final Program\\FinalHomework\\info.bin", "wb");
    fwrite(&s, sizeof(s[0]), n, f);
    fclose(f);
}

void retrievebin(struct Person s[], int n)
{
    FILE *f;
    f = fopen("G:\\College\\CS 36\\Projects\\Final Program\\FinalHomework\\info.bin", "rb");
    fread(&s, sizeof(s[0]), n, f);
    fclose(f);
}

void main()
{
    Person s[SIZE];
    load(s, SIZE);
    sort(s, SIZE);
    print(s, SIZE);
    calc(s, SIZE);
    savetext(s, SIZE);
    retrievetext(s, SIZE);
    printf("\nAfter the text file is retrieved\n");
    print(s, SIZE);
    savebin(s, SIZE);
    retrievebin(s, SIZE);
    printf("\nAfter the binary file is retrieved\n");
    print(s, SIZE);
    system("PAUSE");
}

This is for a homework assignment of mine for class.

Error Received is screenshotted here

Saurabh Meshram

Few things to correct in your Code.

  1. Structure variable Declaration.

    struct Person
    {
        char name[16], dep[16];
        float cyi, ra, rp, npa, tyi, tra, tpa; 
    }; 
    

    You cannot initialize a structure variable as follow:

    void sort(struct Person s[], int n)
    {   
        ...
        Person t;
        ...
    }
    

    Change the above to

    `struct Person t;` or use typedefs.
    
  2. Avoid using gets,
    see here : warning:gets function is dangerous

    Instead use fgets:
    char* fgets(char *string, int length, FILE * stream);

  3. I'm using __fpurge(stdin) to flush the stdin.
    Define in #include <stdio_ext.h>

  4. Change return type of main

    int main
    {
        ...
        return 0;
    }
    

Changes in your code:

void load(struct Person s[], int n)                                             
{ 
    int i;                                                                      
    for (i = 0; i < n; i++)                                                     
    {                                                                           
    printf("Enter your name   ");                                           
    fgets(s[i].name, 16, stdin);                                            
    printf("Enter your department   ");                                     
    fgets(s[i].dep, 16, stdin);
    ...
    ...                                                 
    __fpurge(stdin);    
    }                   
}       

Your Code should pretty much look like this:
http://pastebin.com/STtDLdT4

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

"Stack around the variable was corrupted" error

From Dev

"Stack around the variable was corrupted" error

From Dev

Stack around variable was corrupted - C

From Dev

Getting error: Stack around the variable was corrupted

From Dev

Stack around the variable was corrupted

From Dev

Stack around variable was corrupted

From Dev

Stack around variable was corrupted

From Dev

C - Stack around the variable 'name' was corrupted

From Dev

Stack around the variable is corrupted c++

From Dev

stack around the variable...was corrupted

From Dev

Stack around variable 'x' was corrupted

From Dev

Stack around the variable 'sortArray' was corrupted

From Dev

stack around the variable "variable name" was corrupted C++

From Dev

Basic C++ error. Run-Time Check Failure #2 - Stack around the variable 'matrix' was corrupted

From Dev

Error when accessing array - stack around the variable 'scores' was corrupted

From Dev

C - stack around the variable 'ch1' was corrupted

From Dev

Doubly Linked List C - stack around variable 'list' was corrupted

From Dev

Stack around the variable was corrupted with pointer arithmetics

From Dev

Array issues: Stack around the variable 'arr' was corrupted

From Dev

Why does my function gives me "Stack around the variable 'url' was corrupted." error?

From Dev

C++ - Run-Time Check Failure #2 - Stack around variable 'sourceCount' was corrupted

From Dev

C++ - Run-Time Check Failure #2 - Stack around variable 'sourceCount' was corrupted

From Dev

C++ Stack around the variable '' was corrupted. (D3D9)

From Dev

RunTime Check Failure #2 - Stack around the variable "tab" was corrupted

From Dev

Run-Time Check Failure #2 - Stack around the variable '' was corrupted

From Dev

"Stack around the variable 'buffer' was corrupted" - What is wrong here

From Dev

Stack around the variable was corrupted, after inputing unsigned char by scanf

From Dev

glReadPixels: Stack around variable was corrupted after read pixels

From Dev

Run-Time Check Failure #2 - Stack around the variable '...' was corrupted

Related Related

  1. 1

    "Stack around the variable was corrupted" error

  2. 2

    "Stack around the variable was corrupted" error

  3. 3

    Stack around variable was corrupted - C

  4. 4

    Getting error: Stack around the variable was corrupted

  5. 5

    Stack around the variable was corrupted

  6. 6

    Stack around variable was corrupted

  7. 7

    Stack around variable was corrupted

  8. 8

    C - Stack around the variable 'name' was corrupted

  9. 9

    Stack around the variable is corrupted c++

  10. 10

    stack around the variable...was corrupted

  11. 11

    Stack around variable 'x' was corrupted

  12. 12

    Stack around the variable 'sortArray' was corrupted

  13. 13

    stack around the variable "variable name" was corrupted C++

  14. 14

    Basic C++ error. Run-Time Check Failure #2 - Stack around the variable 'matrix' was corrupted

  15. 15

    Error when accessing array - stack around the variable 'scores' was corrupted

  16. 16

    C - stack around the variable 'ch1' was corrupted

  17. 17

    Doubly Linked List C - stack around variable 'list' was corrupted

  18. 18

    Stack around the variable was corrupted with pointer arithmetics

  19. 19

    Array issues: Stack around the variable 'arr' was corrupted

  20. 20

    Why does my function gives me "Stack around the variable 'url' was corrupted." error?

  21. 21

    C++ - Run-Time Check Failure #2 - Stack around variable 'sourceCount' was corrupted

  22. 22

    C++ - Run-Time Check Failure #2 - Stack around variable 'sourceCount' was corrupted

  23. 23

    C++ Stack around the variable '' was corrupted. (D3D9)

  24. 24

    RunTime Check Failure #2 - Stack around the variable "tab" was corrupted

  25. 25

    Run-Time Check Failure #2 - Stack around the variable '' was corrupted

  26. 26

    "Stack around the variable 'buffer' was corrupted" - What is wrong here

  27. 27

    Stack around the variable was corrupted, after inputing unsigned char by scanf

  28. 28

    glReadPixels: Stack around variable was corrupted after read pixels

  29. 29

    Run-Time Check Failure #2 - Stack around the variable '...' was corrupted

HotTag

Archive