Program skipping cin

I am having a problem with my program skipping the second part of the cin. It works up until line 17 and then after you enter your first name it just couts Enter last name but doesn't let you enter anything and then displays the results. Any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    char first[100];
    char last[100];

    fstream data_store;

    data_store.open("first.txt", ios::in);
    data_store.open("last.txt", ios::in);

    cout << "== Star Wars Name Generator ==" << endl;
    cout << "Enter first name" << endl;
    cin.getline(first, 4);
    cout << "Enter last name" << endl;
    cin.getline(last, 3);

    cout << "Your Star Wars name is: " << first << "-" << last << endl;

    data_store.close();
}
Seems to work fine, so long as I stick to the size limits on input you imposed (4 and 3).
Yeah, I just realized that while I was trying to figure out the problem. I am trying to get the user to input there first and last name and then have it output the first 3 letters of their first name and the first 2 letters of their last name. I thought cin.getline(first, 4) would only take the those characters. I changed the code and now it doesn't measure the lenght of the last name or output the character lenghts.

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
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

int main()
{
    char first[100];
    char last[100];
    int first_length;
    int last_length;

    fstream data_store;

    data_store.open("first.txt", ios::in);
    data_store.open("last.txt", ios::in);

    cout << "== Star Wars Name Generator ==" << endl;
    cout << "Enter first name" << endl;
    cin.getline(first, 100);

    first_length = strlen (first);
    last_length = strlen (last);

    if(first_length < 3)
    {
        cout << "First name must be at least three letter" << endl;
    }
    else
    {
        cout << "Enter last name" << endl;
        cin.getline(last, 100);

        if(last_length < 2)
        {
            cout << "Last name must be at least two letters" << endl;
        }
        else
        {
            cout << "Your Star Wars name is: " << first << "-" << last << endl;
        }
    }

    data_store.close();
}
Last edited on
It does, but it also sets the fail bit on cin (making it not work anymore). To make it work again, call cin.clear() after line 17 (and 19). At that point, you'll see last pick up the next characters that were in the buffer (the part of the first name you didn't read). To remove these from the buffer, call cin.ignore(80, '\n');
You're making this much harder than it has to be.

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    string first;
    string last;
  

    cout << "== Star Wars Name Generator ==" << endl;
    cout << "Enter first name" << endl;
    cin >> first;
 
    if(first.length() < 3)
    {
        cout << "First name must be at least three letter" << endl;
    }
    else
    {
        cout << "Enter last name" << endl;
        cin >> last;

        if(last.length() < 2)
        {
            cout << "Last name must be at least two letters" << endl;
        }
        else
        {
            cout << "Your Star Wars name is: " << first << "-" << last << endl;
        }
    }
}
I tried the cin.clear and it did the same thing. The code from Moschops post works except for it doesn't limit the output of the first name to the first 3 characters and the last name to the first 2 characters.
So that's what you're trying to do? Take the first three letters of the first name and the first two letters of the last name? You need to read up on the string class.

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    string first;
    string last;
  

    cout << "== Star Wars Name Generator ==" << endl;
    cout << "Enter first name" << endl;
    cin >> first;
 
    if(first.length() < 3)
    {
        cout << "First name must be at least three letter" << endl;
    }
    else
    {
        cout << "Enter last name" << endl;
        cin >> last;

        if(last.length() < 2)
        {
            cout << "Last name must be at least two letters" << endl;
        }
        else
        {
             first.resize(3);
	  last.resize(2);

            cout << "Your Star Wars name is: " << first << "-" << last << endl;
        }
    }
}
Thanks. I just learned about File I/O so I was trying to incorporate what I just learned. The last name should be the last 2 characters not the first two characters. That was my mistake. That's why I was using the fstream.
I'm not sure there's any need for file input/output in this.
Topic archived. No new replies allowed.