Getline function error

Maybe it's just me but for some reason when input the size of the string array I can really only input the size I inputted - 1. For instance, If i make my string array be able to hold 4 elements I can only input 3 of those 4 elements with it closing out? I believe the problem lies with the getline function?

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

using namespace std;

int main()  {
    string* convert;
    char response;
    int letters;
    int enter;
    int amount = 0;
    int number;
    const int ascii = 64;
    string stream;
    cout << "How many lines of text would you like to convert: ";
    cin >> enter;
    cout << enter << " strings\n";
    convert = new string[enter];
    cout << "How many words now: ";
    cin >> letters;
    char check = ' ';
    string message = "";
    cout << "Please enter your messages!\n";
    for (int i = 0; i < enter; i++) {
        getline(cin, convert[i]); //If i change this to cin>>convert[i] it works
        for (int f = 0; f < convert[i].size(); f++) {
            if(convert[i][f] != ' ') {
                if (convert[i][f] == '8' && convert[i][f + 1] == ' ') {
                    amount = 96;
                    check = convert[i][f + 1]; 
                    number = (int)check - 48;
                    amount += number;
                    message += (char)amount;
                }
                else if (convert[i][f] == '9' && convert[i][f + 1] != ' ') {
                    amount = 105;
                    check = convert[i][f + 1];
                    number = (int)check - 48;
                    amount += number;
                    message += (char)number;
                }
                else if (convert[i][f] == 'A') {
                    amount = 113;
                    check = convert[i][f + 1];
                    number = (int)check - 48;
                    amount += number;
                    message += (char)number;
                }
                else if (convert[i][f] == '4' && convert[i][f + 1] == '0') {
                    message += ' ';
                }
                else if (convert[i][f] == '4' && convert[i][f + 1] == 'B') {
                    message += '.';
                }
                else if (convert[i][f] == 'C') {
                    amount = 0 + ascii;
                    check = convert[i][f + 1];
                    number = (int)check - 48;
                    amount += number;
                    message += (char)amount;
                    
                }
                else if (convert[i][f] == 'D') {
                    amount = 9 + ascii;
                    check = convert[i][f + 1];
                    number = (int)check - 48;
                    amount += number;
                    message += (char)amount;
                }
                else if (convert[i][f] == 'E') {
                    amount = 17 + ascii;
                    check = convert[i][f + 1];
                    number = (int)check - 48;
                    amount += number;
                    message += (char)amount;
                }
            }
            else {
                check = ' ';
            }
        }
    }
    cout << message;
    cout << "\nThank you for using my EBDIC to Ascii converter!\n";
    cin >> response;
    return 0;
}
At the end of line 21, you have a new line character remaining in the input stream. The extraction operator (>>) reads characters until what is read is no longer an int and then stops reading, leaving the next character in the stream.

When you do the getline, it reads until it finds a new line (and consumes it). In this case, the first character is a new line, so the first read is empty. The extraction operator, on the other hand, skips white space (including the new line) and reads characters to for a string.

In order to get newline to work properly, try the ignore function:
cin.ignore(256, '\n');

This will clear out cin and get ready for reading the strings.
Holy crap I never even knew that. Dang you got some good knowledge and vision lol. That's something new to add to my knowledge thanks! I've only ever used cin.ignore with cin.clear for when extraction operator got the wrong input.
Topic archived. No new replies allowed.