In the outer for loop, you are infinitely looping until getline()
evaluates to false. In the inner for loop, you are looping through each character in line
.
I don't know of any elegant syntax to parse a string into tokens like it seems you are trying to do. One way is to utilise std::stringstream
and put things into an array or vector. Below is a sample main I wrote demonstrating how versatile this is:
#include <iostream>
#include <sstream>
#include <vector>
int main() {
std::string line;
while ( true ) {
std::cout << "Enter parse string: ";
std::getline( std::cin, line );
if ( line == "Stop" ) break; // Enter stop to quit program
std::stringstream ss( line ); // constructor sets 'line' as contents of the stream
std::vector <std::string> tokens; // where our tokenised input is stored
std::string temp; // where current token is temporarily loaded before it's put into the vector
while( getline( ss, temp, ' ' ) ) // extract characters from ss until a space is found and stores them in temp
tokens.push_back( temp ); // put the token formed by getline in our vector, from back so order is kept
/*
Now you can use the vector as you would like. Below the contents are printed.
*/
for ( auto& x : tokens ) // for each token in 'tokens' vector,
std::cout << x << ' '; // print out token with a space at the end
std::cout << "
";
for ( int i = 0; i < tokens.size(); i++ ) // for each token in 'tokens' vector,
std::cout << i << ": " << tokens[i] << '
'; // print out index number and token
std::cout << std::endl; // newline & flush
}
return 0;
}
If you want to abstract the details away, you could always wrap the std::stringstream
stuff into a function that takes in a string and returns a std::vector<std::string>
. Below I demonstrate what that would look like:
#include <iostream>
#include <sstream>
#include <vector>
std::vector <std::string> toParsedVector( std::string input ) {
std::stringstream ss( input ); // constructor sets 'line' as contents of the stream
std::vector <std::string> tokens; // where our tokenised input is stored
std::string temp; // where current token is temporarily loaded before it's put into the vector
while( getline( ss, temp, ' ' ) ) // extract characters from ss until a space is found and stores them in temp
tokens.push_back( temp ); // put the token formed by getline in our vector, from back so order is kept
return tokens;
}
int main() {
std::string line;
while ( true ) {
std::cout << "Enter parse string: ";
std::getline( std::cin, line );
if ( line == "Stop" ) break; // Enter stop to quit program
std::vector <std::string> tokens = toParsedVector( line ); // create vector
// Now you can use the vector (tokens) as you would like. Below the contents are printed.
for ( auto& x : tokens ) // for each token in 'tokens' vector,
std::cout << x << ' '; // print out token with a space at the end
std::cout << "
";
for ( int i = 0; i < tokens.size(); i++ ) // for each token in 'tokens' vector,
std::cout << i << ": " << tokens[i] << '
'; // print out index number and token
std::cout << std::endl; // newline & flush
}
return 0;
}
I hope this helps; might not be the most elegant solution, but it is very versatile.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…