Hello,
I'm trying to write a function that will take in a single string. In that string it has multiple pieces of information only broken up by a '\n'. So far the only thing I can do is get the string into the function but I need to use and compare different parts of the string to other parts. My first thought obviously, was to separate each substring and store in a variable or an array to compare. That approach didn't work. I then tried to use a for loop to separate the substrings based of the start and end. I thought that might work since there is exactly 15 characters not including the '\n' (maybe not calculating that is why it messed up). But if anyone could give me advice or which resource to look into would be great. I have been on the string, sstream reference pages already.
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::string input {"line one\nline two\nline three\nline four"}; // okay for last line to have '\n' too
std::istringstream iss { input };
std::string line;
while (getline(iss, line)) {
std::cout << line << '\n';
}
}
Thank you for the help, I have seen ways to extract the place value in the sub string but not a way to get the part of the string and compare to another. Ex:
String info = "Sam is 60\nGary is 20\nFred is 90";
So to take that string compare the names and ages and find the difference in the ages. If it was in a string and all input one at a time that I can set the variable and compare. But with strings I'm just not wrapping my head around it.
Thank you guys for the help I will try to also keep searching I know someone out there has asked this question before
I will try to also keep searching I know someone out there has asked this question before
Are you saying my solution won't work for you? It gives you the lines one at a time, like you said you wanted.
How about something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::string input {"Sam is 60\nGary is 20\nFred is 90"};
std::istringstream iss { input };
std::string name, is;
int age;
while (iss >> name >> is >> age) {
std::cout << name << ": " << age << '\n';
}
}
JayGln when you're asked to manipulate with strings like this you should look for patterns. In this case it seems substrings are separated by \n and under each substring, the first word is the name and the third is a number.
If you are sure that this is the format the strings will be ALWAYS, then there are many ways to implement it.
You could just split the string again but this time using " " white space delimiter and then work on the names and ages which you get (first word and third word).
Feel free to ask for help. How must the output look like?
As usual, nwb doesn't know what he's talking about.
Of course, if you are doing this as an exercise and feel like searching for spaces, then do so.
If you want a C++ solution, the something like I've shown is the way to go.
The output should say which guy is older and the age difference. Tpb I will try again, i have been trying code and erasing it as soon as it doesn't work and trying a different approach. Yes the format will never change, i have to compare the first part which is the name and the last part the age. I was going to hard code it to work for the example but I know that's not good practice. Sorry guys, thank you
edit: or you can do what tpb did in his second post but instead of printing push to a vector.
-> First make a vector for the substrings. Then using tpb's approach replace with vector.push_back().
-> Now that you have the substrings, make two more vector for names and ages.
Use a for loop that iterates 0 to less than vector.size()
-> Inside the for loop find the first word and push it to the vector having the name. And likewise find the third word and push it to the age vector.
After you've done that it's just about comparing the elements of the ages vector and calling corresponding index from names' vector to identify who's age it is.
Try doing the second and third part yourself if you don't get it you can ask us. ;)