Need help with strings and file input?

Hi! I have made an access code program where you store access codes into a file via the program. I successfully made the program, but when the data is stored, the first letter is always deleted. how can I fix this?

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <sstream>
#include <fstream>

using namespace std;

char szstring[100];
char szstring2[200];

char szstring3[500];

void notes(){

    ofstream data;
    cout<<"Do you want to leave any notes with this? Type in 1 for yes and any other number for no: ";
    int choice;
    cin>> choice;
    switch (choice)
    {
        case 1:
        cout<<"\n\n\n\n\n";
        cout<<"NOTES: ";
        data.open("ac.txt", fstream::in|fstream::app);
        cin>> szstring3[500];
        cin.getline(szstring3, 500);
        data<<"NOTES: ";
        data<<szstring3;
        data<<"\n\n\n";
        data<<"==============================================================================================================\n\n";
        cout<<"\n\n\n"
            <<"Thank you for using this application.\n\n\n"
            <<"RECOVERY CODE: 102983746\n\n";
        break;

        default:
        cout<<"\n\n\n"
            <<"Thank you for using this application.\n\n\n";
        data.open("ac.txt", fstream::in|fstream::app);
        data<<"===============================================================================================================";
        break;
    }
}

int main(){

    ofstream data;
    cout<<"This program stores access codes in the file :'ac.txt'"
        <<"\nPlease note that at the start of each feild, enter  '#' and type."
        <<endl
        <<endl
        <<endl;

    cout<<"Enter your code details;\n\n"
        <<"This is the code for: ";
    data.open("ac.txt", fstream::in|fstream::app);
    cin>> szstring[100];
    cin.getline(szstring, 100);
    data<<"NAME: "<<szstring;
    data<<"\n\n";
    data.close();


    cout<<"\n\n"
        <<"Now enter the code: ";
    data.open("ac.txt", fstream::in|fstream::app);
    cin>>szstring2[100];
    cin.getline(szstring2, 100);
    data<<"CODE: "<< szstring2;
    data<<"\n";
    data.close();
    cout<<"\n\n";

    notes();

    system("PAUSE");
    return 0;
}
1
2
        cin>> szstring3[500];
        cin.getline(szstring3, 500);

it is wrong on so many levels...
What happens:
First line: You take first character from user input and store it at szstring3 in 500th cell - outside memory area allocated to szstring3! Can lead to unexpected behavior sometime.
Second line: you take the rest of using input, store it into szstring3 and append a terminating zero.
You are effectively discards first character from user input.
Right... Big thanks for pointing that out to me...
so, how do you fix this/
I tried something... didnt work... any ideas?
Example for free:

The bolded and underlined part is where you need to pay attention

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
114
115
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <sstream>
using namespace std;

class Person
{
    public:
    char FirstName[256];
    char LastName[256];
    char phone[256];
    char address[256];
    char email[256];
};

Person getPerson()
{
    Person person;
    ofstream data;

    cout << "\nEnter another Person\n"
    << "First Name: ";
    data.open("Info.txt", fstream::in|fstream::app);
    cin >> person.FirstName;
    data << "First Name: " << person.FirstName;
    data << "\n";
    data.close();

    cout << "Last Name: ";
    data.open("Info.txt", fstream::in|fstream::app);
    cin >> person.LastName;
    data << "Last Name: " << person.LastName;
    data << "\n";
    data.close();

    cout << "Phone number: ";
    data.open("Info.txt", fstream::in|fstream::app);
    cin >> person.phone;
    data << "Phone: " << person.phone;
    data << "\n";
    data.close();

    [b]cin.getline(person.address,256);
    cout << "Address: ";
    data.open("Info.txt", fstream::in|fstream::app);
    cin.getline(person.address,256);
    data << "Address: " << person.address;
    data << "\n";
    data.close();

    cout << "Email: ";
    data.open("Info.txt", fstream::in|fstream::app);
    cin.getline(person.email,256);
    data << "Email: " << person.email;
    data << "\n ---------------------------------------------------------------------------------------------------------------------------\n" << endl;[/b]
    data.close();

    return person;
}

int getPeople(Person people[], int nMaxSize)
{
    int index;
    for(index = 0; index < nMaxSize; index++)
    {
        char cAnswer;
        cout << "Enter another person? (Y or N): ";
        cin >> cAnswer;

        if(cAnswer != 'Y' && cAnswer != 'y')
        {
            break;
        }

        people[index] = getPerson();
    }
    return index;
}

void displayPerson(Person person)
{
    cout << "Name: " << person.FirstName << " " << person.LastName << endl;
    cout << "Phone number: " << person.phone << endl;
    cout << "Address: " << person.address<< endl;
    cout << "Email: " << person.email << endl;
    cout << "-----------------------------------------------------------\n" << endl;
}

void displayPeople(Person people[], int nCount)
{
    for(int index = 0;index < nCount; index++)
    {
        displayPerson(people[index]);
    }
}

int main(int nNumberofArgs,char* pszArgs[])
{
    Person people[256];

    cout << "Hello!, this is a program that stores: Name, Phone, Address, and email." << endl;
    cout << "It will store it temporarily in this console while" << endl;
    cout << "storing it in 'info.txt'." << endl;

    int count = getPeople(people, 256);

    cout << endl << "Here is what your entered: " << endl << endl;
    displayPeople(people,count);

    system("PAUSE");
    return 0;
}


As you can see, I put cin.getline before the first one.
Ahhhh... I see it... Thanks, greenleaf!
No problem
Topic archived. No new replies allowed.