Ok, so I made a program that takes as input (standard) several lines of integers separated by spaces with cin. is it possible to end the input when the user doesn't type anything in a line and just hits enter? If so, how? Thanks
C++ end of input help?
getch() isn't standard ANSI C++ and is used mostly on windows/dos machines. It is good to get into the habit of writing standard code whenever possible.
What you really want to do is read all your values into a string and then break them up. istringstream is ideal for this. Your input stops when the user hits return w/o entering any values. Note that no checking for valid input is done so...garbage in garbage out.
I did not know what you wanted to do with entered values so I just put them into an array and displayed them. You will have to change according to your needs.
#include %26lt;iostream%26gt;
#include %26lt;sstream%26gt;
#include %26lt;string%26gt;
#include %26lt;vector%26gt;
using namespace std;
int main(int argc, char *argv[])
{
vector%26lt;int%26gt; valArray;
int i = 0;
string str;
getline(cin, str);
while (str.length() != 0)
{
istringstream iss (str);
while (iss.good())
{
int temp;
iss %26gt;%26gt; temp;
valArray.push_back(temp);
i++;
}
getline(cin, str);
}
for (int i = 0; i %26lt; valArray.size(); i++)
{
cout %26lt;%26lt; valArray[i] %26lt;%26lt; endl;
}
return(0);
}
Reply:Just use this
do{
// your integer input here
int key = getch();
}
while(key!=13)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment