0

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); } } 
6
  • 1
    You put in an exit condition that stops the loop. How is that code supposed to know when you're done giving input without a condition that tells it to stop looping? Commented May 2, 2020 at 0:29
  • 2
    On Windows you can terminate the console input with CTRL+Z. Commented May 2, 2020 at 0:31
  • 2
    #include<bits/stdc++.h> is non-standard and pulls in a lot of stuff you don't need. You should include the correct headers for the types you are using. Commented May 2, 2020 at 0:33
  • @BessieTheCow: You seriously think that's the proper way to exit a while loop in a C++ program? Commented May 2, 2020 at 0:36
  • 2
    @KenWhite - If a program is reading from standard input, and the user is running that program from a console or console window, and the operating system is windows, then one way for the user to signal end of input is to use CTRL-Z. Standard C++ does not provide a rich set of options for user interaction (e.g. no GUI support) so most of these things are outside control of the program. Commented May 2, 2020 at 0:49

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.