Stack around the variable corrupted?

I get this error:
Run-time check Failure #2 - Stack around the variable "city" was corrupted.

Please help! What is wrong? When I remove every instance of "city" (to troubleshoot), it then says:
Run-time check Failure #2 - Stack around the variable "address" was corrupted.

THANKS!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int write(void)
{
    ofstream outfile;
    char firstname[50],
         lastname[60],
         address[60],
         city[50],
         state[10],
         zipcode[10],
         phonenumber[13];
    int index;

    outfile.open("PersonalData.txt",ios::app);
    cout<<"Enter your First Name: ";
    cin>>firstname;
    cin.ignore();
    cout<<"Enter your Last Name: ";
    cin>>lastname;
    cin.ignore();
    cout<<"Enter your Address: ";
    cin.getline(address, sizeof(address));
    cin.ignore();
    cout<<"Enter your City: ";
    cin.getline(city, 50);
    cout<<"Enter your State: ";
    cin.getline(state, sizeof(state));
    cin.ignore();
    cout<<"Enter your ZipCode: ";
    cin.getline(zipcode, 10);
    cout<<"Enter your Phone Number: ";
    cin.getline(phonenumber, 13);
    cin.ignore();

    outfile<<firstname<<setw(2)<<lastname<<setw(2)<<address<<setw(2)<<city<<setw(2)<<state<<setw(2)<<zipcode<<setw(2)<<phonenumber<<endl;
    outfile.close();
    cout<<"Writing Completed."<<endl;
    return 0;
}

int read(void)
{
    ifstream input;
    input.open("PersonalData.txt",ios::in);

    char firstname[50],
         lastname[60],
         address[60],
         city[50],
         state[10],
         zipcode[10],
         phonenumber[13];

    int counter =1;
    while(!input.eof())
    {
        input>>firstname>>lastname>>address>>city>>state>>zipcode>>phonenumber;
        
        cout<<firstname<<setw(1)<<lastname<<endl;
        cout<<address<<endl;
        cout<<city<<setw(2)<<state<<setw(2)<<zipcode<<endl;
        cout<<phonenumber<<endl;
        counter++;
        cout<<endl;
    }
    cout<<"Reading Completed. "<<endl;
    input.close();
    return 0;
}

int main()
{
      int choice;
      char ch;

      do{
          cout<<endl;
          cout<<"Please choose from the following: \n";
          cout<<"\t1. Read Data. \n";
          cout<<"\t2. Write Data. \n";
          cout<<"\t3. Quit. \n";
          cout<<endl;
          cout<<"Your choice: ";
          cin>>choice;


           switch(choice)
          {
             case 1:
                     read();
                   break;
             case 2:
                     write();
                   break;
             case 3:

                   break;
             default:
                   cout<<"\tInvalid entry!"<<endl;
           }
           cout<<"\nWould you like to try again (y/n): ";
           cin>>ch;

     }while(ch == 'y' || ch == 'Y');

      system("PAUSE");
      return 0;
}
Last edited on
when you call sizeof() to get your array size, you are getting the number of bytes your array occupies, not the number of characters in your array. getline expects the number of characters, so it will continue to read from the stream into your buffer, irregardless of whether it overflows the buffer.

This post explains how to properly get the size of your array:
http://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c
Topic archived. No new replies allowed.