Hello I am a cs140 student so I am not the most experienced. I have an assignment and basically I have to find the longest line in a string vector. First I have to take paragraph as input. How do I take the input from a file and split it line by line into a string vector? Please go easy on me
Asked
Active
Viewed 186 times
0
-
What have you tried? Where did you get stuck? Please show a [mre] and any error messages – Alan Birtles Feb 04 '21 at 21:46
-
Hint: look at `std::ifstream`, `std::string`, `std::vector`, `std::getline()`, `while`, and `std::vector::push_back()`. – Remy Lebeau Feb 04 '21 at 21:50
-
What is the name of the cs140 class? Which university or college? Not all universities and colleges have the numbering or class subjects. – Thomas Matthews Feb 04 '21 at 22:23
1 Answers
5
Well you don't usually read the text and then split it line by line. Instead, you read the text line by line in the first place, using the getline function
std::string line;
std::getline(std::cin, line);
You can choose to store all the lines in a vector of strings, however, in order to find the longest string, you don't have to store all of them. You can just compare the current line's length with a saved "longest line" or the length thereof.

Armen Tsirunyan
- 130,161
- 59
- 324
- 434
-
1Pay special attention to that last bit of advice. don't store anything you don't have to. Not only does it waste resources, it almost always results in a more complicated program. If you can get away with only holding the current value and a minimum, maximum, sum, or some other statistic, do it. – user4581301 Feb 04 '21 at 21:58