using get line to enter a strings into a dynamic array

Hi, I'm in my second semester of c++ and am having trouble entering a string into a dynamic array. Here is my function:

[code in main]
int size;
//string input;
cout <<"How many lines of a poem would you like to write today?";
cin >> size;
DynamicStringArray poem2(size);
poem2.InputEntry();

cout<< "The lines of your poem: \n";
poem2.viewArray();

[code in .cpp file]

void DynamicStringArray::InputEntry()
{

for(int i=0; i < size; i++){
cout << "Write a line of your poem:" << endl;
getline(std::cin, dynamicArray[i]);
}
}
void DynamicStringArray::viewArray()
{
for(int i=0; i < size+1; i++)
{
cout << dynamicArray[i] << endl;
}
}
[/code]

For some reason, the program doesn't let me write to the first index of the array. Can anyone tell me why? I am using Xcode boy.

How many lines of a poem would you like to write today?3
Write a line of your poem:
Write a line of your poem:
love fails
Write a line of your poem:
straw berry
The lines of your poem:

love fails
straw berry

Program ended with exit code: 0
Your problem is one of using cin (which leaves the newline character in the input buffer), followed by getline (which doesn't). A getline following a cin >> would just pick up the blank end of line.

There are a lot of ways around that, including
- using getline for everything (you can soon convert a string to an int)
- using cin.ignore(1000,'\n') after your cin >> size.
- calling getline(cin,dummy) with a sacrificial string dummy after your cin >> size.

This is so common an occurrence that I'm surprised nobody standardised something like
cin >> size >> EOF()
(no, that doesn't exist) to deal with it.

You shouldn't need size+1 in your viewArray routine.
Last edited on
And here's one way of putting that into practice:
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
#include <iostream>

using namespace std;

// SUBSTITUTE FOR THE REAL CLASS
string* dynamicArray;
int sz{0};

void InputEntry()
{
    std::cin.clear();
    std::cin.ignore(1000, '\n');
    
    for(int i = 0; i < sz; i++)
    {
        cout << i << ' ' << "Write a line of your poem: ";
        getline(std::cin, dynamicArray[i]);
    }
}

void viewArray()
{
    for(int i = 0; i < sz; i++)
    {
        cout << i << ' ' << dynamicArray[i] << endl;
    }
}

int main()
{
    cout <<"How many lines of a poem would you like to write today? ";
    cin >> sz;
    
    dynamicArray = new string[sz];
    InputEntry();
    
    cout << "The lines of your poem: \n";
    viewArray();
    
    return 0;
}


How many lines of a poem would you like to write today? 3
0 Write a line of your poem: first line here
1 Write a line of your poem: second line here it is
2 Write a line of your poem: third line as a test
The lines of your poem: 
0 first line here
1 second line here it is
2 third line as a test
Program ended with exit code: 0
Woo, thank you very much for your contribution
againtry
, I was looking for some of this for academic activities
You might also want to validate that a number is entered, so you could do something like:

1
2
3
4
5
6
7
   cout << "How many lines lines of a poem would you like to write today?";
   int lines;
   while(!(cin >> lines)){
      cin.clear();
      while(cin.get() != '\n') continue;
      cout << "How many lines lines of a poem would you like to write today?";
   }


Here if you do not enter an integer, a flag on cin is set that stops further input via cin. Therefore cin.clear() resets the flag. The second while loop clears the input buffer. Then you re prompt. The first loop continues until and integer is entered.

Last edited on
Good idea to check and an alternative while loop arrangement:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

int main()
{
    
    std::string prompt
    { "How many lines of a poem would you like to write today? "};
    
    int lines{0};
    
    while(std::cout << prompt && !(std::cin >> lines) )
    {
        std::cout << "MUST BE AN INTEGER\n";
        std::cin.clear();
        std::cin.ignore(1000, '\n');
    }
    
    std::cout << "No. lines " << lines << " to proceed with ...\n";
    
    return 0;
}

Topic archived. No new replies allowed.