taking input into string array

Jul 4, 2016 at 7:40pm
i'm wondering why this isn't working, it keeps giving me a build error. Here is the code snippet with the error. i have tried cin and getline.

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



using namespace std;

class General{
    
    string name[25];
    string location[20];
    string destination[20];
    string seatno[3];
    string depdate[10];
    string redate[10];
    
public:
    
    void acceptInfo(){
        
        cout << "Please what is your full name? " << endl;
        getline (cin, name);
        cout << "Please where are you flying from? " << endl;
        getline (cin, location);
        cout << "Please where are you flying to? " << endl;
        getline (cin, destination);
        cout << "Please what is your preferred seat?" << endl;
        getline (cin, seatno);
        cout << "Please what date do you depart?" << endl;
        getline (cin, depdate);
        cout << "Please what day do you return?" << endl;
        getline (cin, redate);
            }
    
    
    void checkSeats() {
        
        ofstream myfile;
        myfile.open ("Aisosa.txt");
        myfile << name[25] << "  " << location[20] << "  " << seatno[3] << "  " << destination[20] << "  " << redate[10];
        
        myfile.close();
    }


Jul 4, 2016 at 8:23pm
Why are you trying to create those arrays of string in your class?

In your acceptInfo() you are trying to access arrays like they are single strings.

In your checkSeats() function you are trying to access those arrays out of bounds. Remember in C++ arrays start at zero and end at size - 1.

You seem to be confusing C++ strings with C-strings. You probably only want single instances of your string in the class definition, not arrays.

Jul 4, 2016 at 8:51pm
thanks i guess i changed my mind from char type and i retained the arrays...at what point does one use string arrays??
Jul 4, 2016 at 8:57pm
also how do i continuously save it into a file such that it saves it into it, line after line with multiple entries
Jul 4, 2016 at 9:32pm
at what point does one use string arrays??

When you want arrays of strings.

Really this program shouldn't be using arrays of strings, if anything you should have a vector/array of the class.

also how do i continuously save it into a file such that it saves it into it, line after line with multiple entries

Probably a loop of some kind.
Topic archived. No new replies allowed.