C++ multiple lines input
开发者_JAVA技巧So I need to create a char buffer containing 4 lines of text (EN_us) like
first line
line with some number like 5
line 3
empty line
What is correct way of getting such char buffer from user and how to get its length?
Instead of getting a buffer like that, it might be easier to read four lines into separate string
s from the standard input using getline
(use a loop if you prefer):
Then the total data length is the sum of the individual string
lengths. Alternatively use this method to retrieve the data from the user and then concatenate them into a four-line stringstream
.
Combined code example:
#include <string>
#include <sstream>
#include <iostream>
std::string s[4];
size_t length(0);
std::ostringstream output;
for (size_t index = 0; index < 4; ++index)
{
getline(std::cin, s[index]);
length += s[index].length();
output << s[index] << std::endl;
}
output.flush();
streamoff streamLength = output.tellp();
精彩评论