Enter multiple strings into char array

I am having issues getting this working. This is a simple program that is designed to ask a user if he would like to enter a string, if yes, the user is prompted to enter it and its stored in a char array. User is then asked if he wants to enter another string... Once user responds no, the program outputs the strings and the program ends... Note: I'm using in.getline(myarray[i], MAX, '\n'), to avoid white space problems if user enters a space. Lastly I would like the option of letting user enter any number of strings, but how would you do this when declaring the 2 dimentional char array?

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

#include <iostream>
#include <string>
using namespace std;

const int MAX = 81;   //max char is sting is 80

int main(){
    
    string y_n;
    bool go = true;
    char myarray [][MAX];     //How can i make this allow an open ended amount of string entries?
    int inputcount = 0;
    
    
    cout << "Do you want to input a Phrase? ('Yes' or 'No')" ;
    cin >> y_n;

    
    do{
        if((y_n == "no") || (y_n == "No"))
            go = false;
        
        else if (y_n == "yes" || y_n == "Yes")
            for(int i=0; i <= 4; i++)
            {
                cout << "Please Enter the Phrase:";
                cin.getline(myarray[i], MAX, '\n');
                 ++inputcount;

           cout << "Do you want to input a value (Y or N)" ;
           cin >> y_n;
        }
        
            else{
            cout << "Please enter a valid response (Either 'Yes' or 'No'" ;
            cin >> y_n;
        }

    }while (go);
    
    for(int z = 0; z < inputcount; z++)
        cout << "Entry # " << z << " equals :  " << myarray[z] << endl;
    
    return 0;
    
}

Last edited on
You can do this using new opearator or vectors.
Use a vector of strings. A string is a easier to use character array because it dynamically allocates for the space you need. Replace

 
char myarray[][MAX];


with

 
std::vector<std::string> myarray;

Topic archived. No new replies allowed.