Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
 }
like image 943
Gokul Avatar asked Mar 29 '15 14:03

Gokul


5 Answers

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 << ' ';
    }
like image 75
πάντα ῥεῖ Avatar answered Nov 15 '22 04:11

πάντα ῥεῖ


while (cin.peek() != '\n') {
  cin >> a[i++];
}
like image 24
Lingxi Avatar answered Nov 15 '22 03:11

Lingxi


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;
 }
like image 38
Marko Avatar answered Nov 15 '22 04:11

Marko


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

like image 36
Arun A S Avatar answered Nov 15 '22 03:11

Arun A S


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

like image 29
Varun Rao Avatar answered Nov 15 '22 04:11

Varun Rao