windows - C++ Issue with cin and CTRL + Z -
i'm reading c++ primer 5th , have little problem exercise:
read sequence of words cin , store values vector. after you’ve read words, process vector , change each word uppercase. print transformed elements, 8 words line.
my code this:
#include <iostream> #include <vector> #include <string> #include <cctype> using std::vector; using std::string; using std::cin; using std::cout; using std::endl; int main(){ vector<string> words; string wordbuffer; vector<string> output(1); while (cin >> wordbuffer){ words.push_back(wordbuffer); } (string &word : words){ (char &letter : word){ letter = toupper(letter); } } unsigned currentline = 0; (decltype(words.size())index = 0; index < words.size(); ++index){ output[currentline] += words[index] + " "; if ((index+1) % 8 == 0){ ++currentline; output.push_back(""); } } (string s : output){ s[s.size() - 1] = 0; //removing whitespace cout << s << endl; } system("pause"); return 0; }
now, works well, have issue input of words console.
if write
i writing random words ^z
and press enter nothing happens. have rewrite ^z after have pressed enter, here:
i writing random words
^z
can expain me why? thanks!
ps: i'm saying because in previous programs writing ^z in same line worked fine. in code:
#include <iostream>; int main(){ int currval = 0,val = 0; int count = 1; while (std::cin >> val){ if (currval == val){ ++count; } else { std::cout << "the number " << currval << " appears " << count << " times" << std::endl; currval = val; count = 1; } } std::cout << "the number " << currval << " appears " << count << " times" << std::endl; system("pause"); return 0; }
i can't figure out why :(
the ^z has first in order windows treat ctrl+z, otherwise treated meaningless characters.
if work wrote i'd suggest:
string wordbuffer("") while (strcmp(wordbuffer[strlen(wordbuffer)-3], "^z") != 0){ words.push_back(wordbuffer); cin >> wordbuffer }
edit: in second example works because when read integers c++ knows divide given string of numbers in space (or enter if numbers entered separately in every line) read every number separately if you'll enter:
123 2323 4545 43 ^z
it read 123, 2323, ... , ^z , though got in separate line when read string, cant because string contain every symbol , separate input in enter pressed , why second 1 works
Comments
Post a Comment