I have a simple encryption program here. I have to alter it so I can format the the name lines so that "King Beast Kong" would come out "Kong, King B." and "Fay Wray" would come out "Wray, Fay." I'm supposed to use string streams to divide the names into fields and I can't figure out how. This is the hint I'm given:
Place the name line into a stringstream object and then use the stringstream object’s input operator (>>) to read names into three string variables. If there are only two names, a first name and a last name, then the >> operator will read strings into the first two variables only, and the third variable will be left uninitialized. You can check to see whether or not only two names were read by checking the stringstream object’s fail function. If it is true, then you only read two names (it failed trying to read the third name).
Here is the sample file it's reading:
King Beast Kong
3:16 PM
Where are you?
Fay Wray
10:06 AM
At the south tip of the island
Ginger Rogers
12:06 PM
I should be at the south tip of the island in 15 minutes
King Beast Kong
12:15 AM
I've been waiting for hours. Where are you?
Well I don't seem to be getting it right. All I can get is the King Beast Kong and none of the other names. I don't know if I should make name an array or make the first, middle, and last name variables arrays. How would you do it?
Look I like to figure these things out on my own but I've spent hours just on this one part and its probably something simple. If someone could just show me the code they would use on this one part I can assure you I'll have plenty more challenges to figure out on my own, all due by midnight.
Maybe I'm missing something, but the code you gave us doesn't even show your attempt at a solution to the problem, which is to organize the names in last name, first name (middle initial) format.
No you're right, I've tried a ton of combinations and nothing has worked. I was hoping someone that knew what they were doing could show me just how to properly feed the names into the string stream and into separate fields. I can figure out how to format it if I know that much.
Let's say you have the following line in a file called sample.txt:
one two three
And the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <fstream>
#include <sstream>
usingnamespace std;
int main()
{
ifstream blog;
stringstream ss;
string line,first,second,third;
blog.open("sample.txt");
getline(blog,line);
ss<<line; //Now the stringstream contains "one two three"
ss>>first>>second>>third; //first contains "one", second contains "two", and third contains "three".
}
You'll need more to do the assignment but I don't want to give it all away. You'll need to clear the stringstream of the end of file error after it reads each line.
Thanks, I appreciate the help! But say the file contained:
one two three
one1 two1 three1
and I have to put one and one1 in the same category. This is really where my problem is because I can only get "King Beast Kong" to feed in and I'm not sure how to get it to move on and put the next name in.
Umm, not sure exactly what you mean, but it sounds to me like you need to clear the stringstream. So, for instance, after reading in the first line and assigning "one" and "two" and "three" to strings 'first' and 'second' and 'third', clear the stringstream ss.clear(); since it's reached the end of file. Then use another getline for line 2, read line 2 into the stringstream, and then assign "one1" and "two1" and "three1" to strings 'first' and 'second' and 'third'. Like so:
Since you do not know whether a line has two or three words, you will need to use ss.eof() to check.
EDIT: Athar is right. A function would be better since you wouldn't need to clear the stringstream every time. Just be sure to declare the stringstream within the function, not within main.
Well, first of all, use a function line Athar suggested. Then you won't need the clear function. Just forget I mentioned it (Although you should look into it on your own time if you're interested). What you really need is the fail function, as the hint suggested. Also, why do you keep appending each line onto name?
#include <iostream>
#include <fstream>
#include <sstream>
usingnamespace std;
int main()
{
string fixedName;
getline(blog,name)
fixedName=fixName(name);
}
string fixName(string a)
{
stringstream ss;
string first,second,third,fixed;
ss<<a;
ss>>first>>second>>third;
if(ss.fail())
{
//This is the case where the line only had two words and so the stringstream failed in reading a third word.
}
else
{
//If the stringstream did not fail to read the third word.
}
return fixed;
}
Ok, that makes way more sense. Sometimes I get where I just can't think for myself anymore. I should be able to get it from here though, thanks for the help.