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