2

I want to take input from the user until he presses enter key. I have used the following approach, but in vain, as the loop won't terminate. Please help.

#include<bits/stdc++.h>
using namespace std;

int main() {
    int n;
    vector<int> array;
    cout<<"Please enter the elements of the array"<<endl;
    while(cin>>n) {
        array.push_back(n);
    }
    for(auto i: array){
       cout<<i<<" ";
    }
    cout<<endl;
    return 0;
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Aman Mulani
  • 295
  • 3
  • 6
  • please include your input in the question. How do you know that the loop does not terminate? – 463035818_is_not_an_ai Jan 25 '21 at 12:40
  • not the problem, but nevertheless... [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) and [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – 463035818_is_not_an_ai Jan 25 '21 at 12:42
  • 1
    Your loop would end after EOF (^D on unix, ^Z on windows), not a line end. If you want to end it after newline, you will have to write the logic for that. Possibly first read a line into string and take it apart afterwards. –  Jan 25 '21 at 12:42
  • 1
    Possible duplicates: [Keep reading numbers until an empty input](https://stackoverflow.com/questions/25522890/keep-reading-numbers-until-an-empty-input) and [How to read groups of integers from a file, line by line in C++](https://stackoverflow.com/questions/2287121/how-to-read-groups-of-integers-from-a-file-line-by-line-in-c) – rustyx Jan 25 '21 at 12:46

3 Answers3

2

The while loop will go on until cin >> n fails. It doesn't fail when you press enter. Enter is treated like any other whitespace character and will be skipped by default when doing formatted reading using operator>>.

You could read complete lines and extract data from those instead. If the user enters a blank line, you break out of the loop.

#include <iostream> // include the correct headers, not bits/stdc++.h
#include <sstream>
#include <string>
#include <vector>

// don't do: using namespace std;

int main() {
    std::vector<int> array;

    for(std::string line; std::getline(std::cin, line);) {

        if(line.empty()) break; // the user didn't enter anything, break out

        // put the line in an istringsstream for extraction
        std::istringstream is(line);
        int n;
        while(is >> n) { // extract from the istringstream
            array.push_back(n);
        }

        // if you want to break out of the loop if the user entered
        // something that couldn't be extracted:
        if(not is.eof()) { // if all was extracted, eof() will be true
            is.clear(); // clear the error state from the failed extraction
            std::getline(is, line); // read out the rest of the line
            std::cout << "could not extract an int from \"" << line << "\".\n";
            break;
        }

    }
    
    // print the result
    for(auto i: array) {
        std::cout << i << ' ';
    }
    std::cout << '\n';
}

If you only want exactly one line of data, replace the for loop with

if(std::string line; std::getline(std::cin, line)) {

and adjust the breaks accordingly (since there's no loop to break out of).

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

You can use std::peek function to check if the next character is a newLine.

while(cin.peek() != '\n'  && cin>>n) {
    array.push_back(n);
}

Alternatively, you can enter an int at the beginning of your input determining the size of your input array.

int arr_size;
cin >> arr_size;
// Take input for that array according to its entered size

Or even take the whole input as string using getline and parse that string to get your array numbers.

0

As, already mentioned in the comments, there is no end-of-line, during command line inputs, you have to manually press ctrl + z.
But, you can use this alternate approach by using string::empty.

#include <iostream> // Only include things you use
#include <string>
#include <vector>

// Using namespace std

int main() {
    std::vector<int> array;
    std::string s;

    while(getline(std::cin, s)) {

        if(s.empty())   // user give empty string as input.
            break;

        array.push_back(stoi(s));   // using stoi (one of many string methods)

        s.clear();  // Clear the string for the next input.
    }
    
    // print the result
    for(auto i: array) {
        std::cout << i << " ";
    }

    std::cout << std::endl;
}
dukeforever
  • 298
  • 1
  • 7