3

I am trying a coding Q and am looking for a line of code that detects if there was no input given (user just presses enter). The concerned datatype is int.

I've read few other Qs about this very problem, but didn't fit in well with my needs. I have tried eof & other such suggestions to no avail...

Here's the code -

#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
int main() {
int ogv,cgv=0,i,j=0,k;
int arr [3];
vector<int> ans;
while(true) {
    cgv=0;
    cin>>ogv;
    //if("ogv is not a number, just an enter") 
          break;
    arr[0]=floor(ogv/4);
    arr[1]=floor(ogv/3);
    arr[2]=floor(ogv/2);
    for(i=0;i<=2;i++) {
        if (arr[i]<0)
            arr[i]=0;
        cgv+=arr[i];
    }

    if(ogv>cgv) {
        ans.push_back(ogv);
    }
    else {
        ans.push_back(cgv);
    }
   j++;
}
for(k=0;k<j;k++) {
    cout<<ans.at(k)<<endl;
}
}

Your help is greatly appreciated...! :D

Thanks

1
  • my be get char using getch() function is good for you Commented Jun 9, 2012 at 9:06

2 Answers 2

2

You can use the noskipws manipulator

Example:

int x = -1;
cin>>noskipws>>x;
if(x==-1)
{
    // no number was entered, the user just pressed enter
}
else
{
    // the user entered a number
}


EDIT: In order to use this in a loop, you need to discard the characters that are currently in the buffer before each read attempt.

For example, if the user inputs the number 4 and presses enter in the first iteration of the loop, cin will read the 4, but it will leave the end-of-line character in the buffer. When the read occurs in the second iteration, cin will see the end-of-line character in the buffer and it will treat it as if the user pressed enter, exiting the loop.

We can use the sync() method in order to discard any unread characters in the buffer. We need to do this before any attempt to read from cin:

cin.sync();
cin >> noskipws >> x;
Sign up to request clarification or add additional context in comments.

9 Comments

It works, but partially. The program quits only after the first iteration. Any fixes? Maybe its because it doesn't go to the next line for the input. Any idea how to take it to the next line? Thanks...
When you read a number, there is an end-of-line character that is left in the stream, and that is treated as if the user pressed enter the next time the read is attempted. You need to ignore the characters that are currently in the buffer before each attempt to read. In order to do that, you can use the sync() method before each read, like this: cin.sync(). I tested it in your program, and it works.
Awesome dude! I however, had to use cin.ignore() instead of cin.sync(). Thanks a lot...
I think you should use sync(). If you use cin.ignore() (with no arguments), it will ignore at most 1 character, until it reaches an end-of-line character. If the user enters more that 1 character, it will only ignore the first character. So, for example, if the user enters "7 ⏎" (seven, a space and an enter-EOL), then cin will read seven, ignore() will ignore the space, and the EOL will remain in the buffer, and the loop will exit the next iteration. If you use sync(), you won't have this problem, because sync() ignores everything that is in the buffer.
I understand the risk, and would preferably use sync() rather than ignore(). But as it turns out, cin.sync() or sync() makes the program exit after the very first iteration...
|
0

If you use streams to input an integer, it will skip white space before reading it in. That includes any amount of newline characters you may enter.

In other words:

int i;
std::cin >> i;

won't return until it finds some non-whitespace.

If you want to detect an empty line, I usually just use getline to get a complete line into a string, then string streams to convert that to an integer, something like:

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

int main (void) {
    std::string s;
    int i = -1;

    std::cout << "Enter string: ";
    std::getline (std::cin, s);
    std::cout << "String is '" << s << "'\n";

    std::stringstream ss (s);
    ss >> i;
    std::cout << "Integer is " << i << "\n";

    return 0;
}

Transcript follows:

pax> ./qq
Enter string: 
String is ''
Integer is -1

pax> ./qq
Enter string: hello
String is 'hello'
Integer is -1

pax> ./qq
Enter string: 0
String is '0'
Integer is 0

pax> ./qq
Enter string: 1
String is '1'
Integer is 1

pax> ./qq
Enter string: 314159
String is '314159'
Integer is 314159

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.