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

14,453

Solution 1

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

Solution 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 , 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 << ' ';
    }

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

Solution 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

Solution 5

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

Share:
14,453
Gokul
Author by

Gokul

Updated on June 12, 2022

Comments

  • Gokul
    Gokul almost 2 years

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