Reading a sentence until ENTER key pressed using 2-D char array

与世无争的帅哥 提交于 2021-02-07 03:28:57

问题


I need to read a sentence word by word until "ENTER" key is pressed. I used a do..while loop to read words until ENTER key is pressed. Please suggest me some conditions for checking ENTER key press (or) others ways for reading similar input.

#include<iostream>
#include<string.h>
using namespace std;

int main(){

     char a[100][20]={'\0'};
     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;
 }

回答1:


while (cin.peek() != '\n') {
  cin >> a[i++];
}



回答2:


The statement

cin>>a[i++];

blocks at the prompt already, until the ENTER key is pressed. Thus the solution is to read a single line at once (using std::getline()), and parse the words from it in a separate step.

As your question is tagged c++, you can do the following:

#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;
}

See the fully working demo please.


As an improvement the current standard provides an even simpler syntax for the for() loop:

    for(auto word : words) {
        std::cout << word << ' ';
    }



回答3:


First, I must say that πάντα ῥεῖ's solution is better. But it might be to much for you if, you are beginner. Besides, I think you want in 2-D array, not vectors.

#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]={'\0'};
     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 != '\0'){
             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 == '\0'){
             break;
        }


    }while(i < 20);


     for(j= 0 ; j < i ; j++)
        cout<< a[j] << " "; 

        cout << endl;

    return 0;
 }



回答4:


How about using this as your while statement

while( getchar() != '\n' );

You'll need the <stdio.h> header file for this.

See the ideone link




回答5:


You can try cin.getline(buffer, 1000);



来源:https://stackoverflow.com/questions/29330446/reading-a-sentence-until-enter-key-pressed-using-2-d-char-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!