Raihan Rifat
Raihan Rifat

Reputation: 1

How to stop taking an arbitrary number of inputs?

I don't have a specific amount of inputs. The amount can be anything. So I have to use this loop. But how do I stop taking input once I'm done?

#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
vector<int>v;

Using this loop to take the inputs because I don't know the amount of the inputs. But how do I stop the loop once I'm done giving input??

while(cin){
cin >> n;
v.push_back(n);

}

}

Upvotes: 0

Views: 194

Answers (2)

Azam Bham
Azam Bham

Reputation: 1399

Depends on what form you expect the input to take. If the expected input is a list of number, delimited by a space, on a single line:

>>>1 2 3 4 5 6

this is fairly easily solvable:

#include<vector>
#include<string>
#include<iostream>
#include<sstream>

int main(){
    std::vector<int> v; //default construct int vector

    //read in line of input into "buffer" string variable
    std::string buffer;
    std::getline(std::cin, buffer);

    //stream line of input into a stringstream
    std::stringstream ss;
    ss << buffer;

    //push space-delimited ints into vector
    int n;
    while(ss >> n){
        v.push_back(n);
    }     

    //do stuff with v here (presumably)

    return 0;
}

If however, the expected input is a list of numbers, delimited by a new line:

>>>1
2
3
4
5
6

You will have to decide on an exit condition, that will tell the program when to stop accepting inputs. This could take the form of a word, which would tell the program to stop. For example:

>>>1
2
3
4
5
6
STOP

A program that would work with such an input:

#include<vector>
#include<string>
#include<iostream>
#include<sstream>

int main(){
    std::vector<int> v; //default construct int vector

    const std::string exitPhrase = "STOP"; //initialise exit phrase   

    //read in input into "buffer" string variable. If most recent input
    //    matches the exit phrase, break out of loop
    std::string buffer;
    while(std::cin >> buffer){
        if(buffer == exitPhrase) break; //check if exit phrase matches

        //otherwise convert input into int and push into vector
        std::stringstream ss;
        ss << buffer;
        int n;
        ss >> n;
        v.push_back(n);
    }

    //do stuff with v here (again, presumably)

    return 0;

}

For a more robust solution, also consider checking the input to see if it can be made into ints.

Upvotes: 1

Nathaniel Blanquel
Nathaniel Blanquel

Reputation: 162

I believe that this wouldn't be so much an issue with the code itself, but more of an issue when it comes to formatting the input. You can put all your input into a text file and pass it as an argument to the executable in the command terminal as so: executable_name < file_name. Also, with a bit of refactoring you can reformat your while loop as so:

while(cin >> n){
    v.push_back(n);
}

With this while loop you can now place a escape character of your choosing at the end of your input file so that when a non-numeric character is detected the while loop breaks.

Upvotes: 0

Related Questions