If statement not working

I'm working on a program that will take the first 3 characters of the users name and the last 2 characters name and combine them to make their star wars name. I am have problems with line 34 it doesn't seem to work. I also can't figure out how just use the last 2 characters of the users last name. I think I would use strcpy but I'm not sure. Any help would 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
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();
}
Move line 23 down to line 33.

You are checking the length before you write to the string.
Thank you for the help. That worked. Now, I can't figure out how to just use the last two characters of the last name. I tried to use last.remove(last_length - 2) but that didn't work. It would not compile. Would the cin.ignore() work? Any pointers or help would be appreciated. Thank you.

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
#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);

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

        last_length = strlen (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;
        }
    }

    data_store.close();
}
Topic archived. No new replies allowed.