cin.getline error



well I have another problem

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#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();

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

    cout << "Email: ";
    data.open("Info.txt", fstream::in|fstream::app);
    cin >> person.email[0];
    cin.getline(person.email,256);
    data << "Email: " << person.email;
    data << "\n ---------------------------------------------------------------------------------------------------------------------------\n";
    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;
}

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

void sortPeople(Person people[], int nCount)
{
    int nSwaps = 1;
    while(nSwaps != 0)
    {
        nSwaps = 0;

        for(int n = 0; n < (nCount - 1); n++)
        {
            if(people[n].phone > people[n+1].phone)
            {
                Person temp = people[n+1];
                people[n+1] = people [n];
                people[n] = temp;

                nSwaps++;
            }
        }
    }
}

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);
    sortPeople(people,count);

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

    system("PAUSE");
    return 0;
}


These places, they don't work...
Last edited on
For your first code posting I would assume you would have to individually input the numbers one at a time with a loop. I might be wrong but I'm pretty sure this is the case.
I think a char array is the exception since the purpose is to attain a string.

Ex.
1
2
char array[256];
cin >> array;


EDIT: Specifically what I mean is that I don't think the istream >> operator overloads to allow for input of an array of ints. Also .getline and .get only allow for char types.
Last edited on
How come it doesn't work outside of int main() and in switches and in if's and loops?
This will not work because the members of the class should be declared inside the class i.e. The "void display()..." etc.

Also, "getline" can work outside the "main" function as long as it is used in the member(s) of a class and the member(s) properly called in the "main" function. As for the difference between "get" and "getline", use Google.
put looping for your array when display it .

1
2
3
4
5
for( int i = 0 ; i < 256 ; i ++ ){
        cout << array[i];
}

cout << endl;
Last edited on
Can someone post the code on how to fix it?... I don't get it...
I fixed it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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";
    data.close();
Topic archived. No new replies allowed.