使用二维字符数组读取一个句子直到按下 ENTER 键
Reading a sentence until ENTER key pressed using 2-D char array
我需要逐字逐句读一个句子,直到按下 "ENTER" 键。我使用 do..while 循环来读取单词,直到按下 ENTER 键。请建议我一些检查 ENTER 键按下的条件(或)其他读取类似输入的方法。
#include<iostream>
#include<string.h>
using namespace std;
int main(){
char a[100][20]={'[=10=]'};
int i=0;
do{
cin>>a[i++];
} while( \ Enter key is not pressed );
for(int j= 0 ; j < i ; j++)
cout<< a[j] << " ";
return 0;
}
如何将其用作您的 while 语句
while( getchar() != '\n' );
为此您需要 <stdio.h>
头文件。
声明
cin>>a[i++];
已经在提示符处阻塞,直到按下 ENTER 键。因此解决方案是一次读取一行(使用 std::getline()
),并在单独的步骤中解析其中的单词。
由于您的问题被标记为 c++,您可以执行以下操作:
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
int main() {
std::string sentence;
std::cout << "Enter a sentence please: "; std::cout.flush();
std::getline(std::cin,sentence);
std::istringstream iss(sentence);
std::vector<std::string> words;
std::string word;
while(iss >> word) {
words.push_back(word);
}
for(std::vector<std::string>::const_iterator it = words.begin();
it != words.end();
++it) {
std::cout << *it << ' ';
}
std::cout << std::endl; return 0;
}
请查看完整的工作demo。
作为改进,当前标准为 for()
循环提供了更简单的语法:
for(auto word : words) {
std::cout << word << ' ';
}
首先,我必须说,πìντα ῥεῖ 的解决方案更好。
但如果你是初学者,这对你来说可能就太多了。
此外,我认为你想要二维数组,而不是向量。
#include<iostream>
#include<string.h>
using namespace std;
int main(){
/*
All of this is simplified.
For example there are no check if user entered word larger than 100 chars.
And that's not all, but because of simplicity, and easy of understanding....
*/
char a[100][20]={'[=10=]'};
char c;
char buffer[2000];
int i=0, j, k = 0;
/*
Read in advance all to way to ENTER, because on ENTER key all data is flushed to our application.
Untill user hits ENTER other chars before it are buffered.
*/
cin.getline(buffer, 2000, '\n');
do {
c = buffer[k++]; //read one char from our local buffer
j = 0;
//while we don't hit word delimiter (space), we fill current word at possition i
// and of cource while we don't reach end of sentence.
while(c != ' ' && c != '[=10=]'){
a[i][j++] = c; //place current char to matrix
c = buffer[k++]; //and get next one
}
i++; //end of word, go to next one
//if previous 'while' stopped because we reached end of sentence, then stop.
if(c == '[=10=]'){
break;
}
}while(i < 20);
for(j= 0 ; j < i ; j++)
cout<< a[j] << " ";
cout << endl;
return 0;
}
while (cin.peek() != '\n') {
cin >> a[i++];
}
你可以试试
cin.getline(缓冲区,1000);
我需要逐字逐句读一个句子,直到按下 "ENTER" 键。我使用 do..while 循环来读取单词,直到按下 ENTER 键。请建议我一些检查 ENTER 键按下的条件(或)其他读取类似输入的方法。
#include<iostream>
#include<string.h>
using namespace std;
int main(){
char a[100][20]={'[=10=]'};
int i=0;
do{
cin>>a[i++];
} while( \ Enter key is not pressed );
for(int j= 0 ; j < i ; j++)
cout<< a[j] << " ";
return 0;
}
如何将其用作您的 while 语句
while( getchar() != '\n' );
为此您需要 <stdio.h>
头文件。
声明
cin>>a[i++];
已经在提示符处阻塞,直到按下 ENTER 键。因此解决方案是一次读取一行(使用 std::getline()
),并在单独的步骤中解析其中的单词。
由于您的问题被标记为 c++,您可以执行以下操作:
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
int main() {
std::string sentence;
std::cout << "Enter a sentence please: "; std::cout.flush();
std::getline(std::cin,sentence);
std::istringstream iss(sentence);
std::vector<std::string> words;
std::string word;
while(iss >> word) {
words.push_back(word);
}
for(std::vector<std::string>::const_iterator it = words.begin();
it != words.end();
++it) {
std::cout << *it << ' ';
}
std::cout << std::endl; return 0;
}
请查看完整的工作demo。
作为改进,当前标准为 for()
循环提供了更简单的语法:
for(auto word : words) {
std::cout << word << ' ';
}
首先,我必须说,πìντα ῥεῖ 的解决方案更好。 但如果你是初学者,这对你来说可能就太多了。 此外,我认为你想要二维数组,而不是向量。
#include<iostream>
#include<string.h>
using namespace std;
int main(){
/*
All of this is simplified.
For example there are no check if user entered word larger than 100 chars.
And that's not all, but because of simplicity, and easy of understanding....
*/
char a[100][20]={'[=10=]'};
char c;
char buffer[2000];
int i=0, j, k = 0;
/*
Read in advance all to way to ENTER, because on ENTER key all data is flushed to our application.
Untill user hits ENTER other chars before it are buffered.
*/
cin.getline(buffer, 2000, '\n');
do {
c = buffer[k++]; //read one char from our local buffer
j = 0;
//while we don't hit word delimiter (space), we fill current word at possition i
// and of cource while we don't reach end of sentence.
while(c != ' ' && c != '[=10=]'){
a[i][j++] = c; //place current char to matrix
c = buffer[k++]; //and get next one
}
i++; //end of word, go to next one
//if previous 'while' stopped because we reached end of sentence, then stop.
if(c == '[=10=]'){
break;
}
}while(i < 20);
for(j= 0 ; j < i ; j++)
cout<< a[j] << " ";
cout << endl;
return 0;
}
while (cin.peek() != '\n') {
cin >> a[i++];
}
你可以试试 cin.getline(缓冲区,1000);