我读的C++底漆第五,我有一个小问题,一个练习:
C++使用问题CIN和CTRL + Z
阅读从CIN的词序列和存储的值的矢量。在 之后,您已阅读所有单词,处理矢量并将每个单词更改为 大写。打印变形的元素,八个字到一行。
我的代码是这样的:
#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);
}
for (string &word : words){
for (char &letter : word){
letter = toupper(letter);
}
}
unsigned currentLine = 0;
for (decltype(words.size())index = 0; index < words.size(); ++index){
output[currentLine] += words[index] + " ";
if ((index+1) % 8 == 0){
++currentLine;
output.push_back("");
}
}
for (string s : output){
s[s.size() - 1] = 0; //removing the whitespace
cout << s << endl;
}
system("pause");
return 0;
}
现在,一切正常,但我有一个问题,用的话通过控制台输入。
如果我写
我写一个随机单词^ Z
,然后按Enter键没有任何反应。我不得不重新改写^ Z我已经按下后输入,喜欢这里:
我写一个随机单词
^ Z
你能expain我为什么?谢谢!
PS:我在说,因为在我以前的程序中,在同一行中编写^ Z可以正常工作。像这样的代码:
#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;
}
我想不通为什么:(
你是写^ Z还是按Ctrl + Z? (在另一个程序中) – 2014-09-10 13:33:47
这不是C++问题,而是Windows中的标准行为。为了确认你可以运行这个简单的程序并获得相同的行为(CTRL + Z必须在它自己的行上按下才能终止):'char c; while((c = getchar())!= EOF)putchar(c);' – Brandin 2014-09-10 13:35:43