0

It was easy to do a non-numerical value check, and it displays the error. The thing I'm having trouble with is when the Enter key is hit it does not display the error just waits for another input. Here is the code:

int x;

std::cout << "\nEnter level:\n";

if(!(std::cin >> x))
{
  std::cout << "Error";
  return ();                                
}
else 
  std::cout << "You have entered: " << x;

return(0);
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • Its different in the way that I can determent when a character is entered as shown here: std::cin.getline(pa[x].fullname,SLEN); if(pa[x].fullname[0]=='\0') { std::cout << "Error"; return (x); } but I cant differentiate if a integer number was entered or just enter as shown here: if((std::cin >> pa[x].ooplevel)==NULL) { std::cout << "Error"; return (x); } – Michal Stormi Kucerak Oct 13 '16 at 14:56
  • You are running in a loop, so the last `std::cin >> pa[x].ooplevel` leaves a newline in the input buffer. Also that condition doesn't make sense. – πάντα ῥεῖ Oct 13 '16 at 15:01
  • I have tried a couple of stuff as well as `std::cin >> pa[x].ooplevel`. the problem is that when I hit enter it just waits for an input but i want it to display a error. – Michal Stormi Kucerak Oct 13 '16 at 15:12
  • Change that condition to `if(!(std::cin >> pa[x].ooplevel))` and don't forget to call `cin.clear()` and to consume the wrong input. – πάντα ῥεῖ Oct 13 '16 at 15:18
  • Ok I tried that but still same result when I only hit enter(without entering anything else) it just keeps waiting for input instead of displaying the error. I don't know what I'm doing wrong. I have been stuck at this for a while before I tried to write here and on reddit . – Michal Stormi Kucerak Oct 13 '16 at 15:26
  • Post a [MCVE] that reproduces your problem, and I'll consider to lift the lock from your question. – πάντα ῥεῖ Oct 13 '16 at 15:29
  • Ok i hope now its fine. – Michal Stormi Kucerak Oct 13 '16 at 15:43
  • In any case you should flush after printing the `"Error"`: `std::cout << "Error" << std::endl;` – πάντα ῥεῖ Oct 13 '16 at 15:45
  • Sry I'm trying to learn C++ so I'm still new to this. – Michal Stormi Kucerak Oct 13 '16 at 15:50
  • You probably should better learn c++ from a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Stack Overflow isn't meant as a tutoring site. – πάντα ῥεῖ Oct 13 '16 at 15:54

0 Answers0