There are several ways to implement in C ++ what the OP is trying to do. I would start avoiding Variable Length Arrays that are not standard and use std::string and std::vector instead.
One option is to read the whole line with the input std::getline , and then process the resulting line to save only the first half of the even characters:
#include <iostream> #include <string> #include <vector> int main() { using std::cin; using std::cout; using std::string; cout << "How many lines?\n"; int n; cin >> n; std::vector<string> half_words; string line; while ( n > 0 && std::getline(cin, line) ) { if ( line.empty() ) // skip empty lines and trailing newlines continue; string word; auto length = line.length() / 2; for ( string::size_type i = 1; i < length; i += 2 ) { word += line[i]; } half_words.push_back(word); --n; } cout << "\nShrinked words:\n\n"; for ( const auto &s : half_words ) { cout << s << '\n'; } return 0; }
Another, as Joachim Pieleborg did in his answer, disable the passing of leading spaces with formatted input functions using the std::noskipws manipulator and then read char at a time:
// ... // disables skipping of whitespace and then consume the trailing newline char odd, even; cin >> std::noskipws >> odd; std::vector<string> half_words; while ( n > 0 ) { string word; // read every character in a row till a newline, but store in a string // only the even ones while ( cin >> odd && odd != '\n' && cin >> even && even != '\n' ) { word += even; } // add the shrinked line to the vector of strings auto half = word.length() / 2; half_words.emplace_back(word.begin(), word.begin() + half); --n; } // ...
Bob__ source share