C++: Reading elements from a vector, loops being skipped over -
currently trying write program ask user input string, read off these values via iterator. final step program apply simple caesar shift message before displaying user. however, since appear have made error causes program skip on "for" loop (or whichever loop use, have tried "for" "while" "if, else" loops). i'm relatively new c++, i'm hoping set loop incorrectly, despite best efforts.
#include <iostream> #include <vector> #include <string> int main () { std::vector <std::string> vector_1; int c; int d = 0; int = 0; std::string message; std::vector<std::string>::iterator it_1; std::cout << "please input message caesar shifted \n"; while (std::cin >> message, d != 0) { vector_1.push_back (message); ++d; } std::cout << "encrypted message is"; (std::vector<std::string>::iterator it_1 = vector_1.begin (); it_1 != vector_1.end (); ++it_1) { std::cout << *it_1; } return 0; }
i spent fair amount of time researching similar situations, far can tell, i've missed pitfalls others have fallen in similar situations. of course, being new, wrong, , appreciated.
it_1 < vector_1.end ()
should be
it_1 != vector_1.end ()
and
while (std::cin >> message, d != 0) { vector_1.push_back (message); ++d; }
should be
getline(std::cin, message); std::stringstream ss(message); std::string temp; while (ss >> temp) vector_1.push_back(temp);
you'll want #include <sstream>
@ top work.
Comments
Post a Comment