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.
#include <iostream>
#include <fstream>
#include <cstring>
usingnamespace 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();
}
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.
#include <iostream>
#include <fstream>
#include <cstring>
usingnamespace 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();
}