Click here to Skip to main content
15,890,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I successfully got my validation to work, but when they type any character that isn't an integer, it prints the input prompt twice. How do I make this only print once? I'm sure it is a very simple fix, but I have only been using c++ for a few weeks now.

// Unit 5.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;


int main()
{

	int lowNum;
	cout << "Please enter your low number (only use numbers 1 - 256): ";
	cin >> lowNum;

	while (lowNum < 1 || lowNum > 256)
	{
		cout << "Please only enter numbers between 1 and 256: ";
		cin >> lowNum;
		if (cin.fail())
			cin.clear();
			cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

    return 0;
}


What I have tried:

I have tried everything I know how to, but I lack the knowledge to resolve the issue.
Posted

This works on my Linux box:
C++
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;


int main()
{
  int lowNum = 0;
  while ( lowNum < 1 || lowNum > 256)
  {
    cout << "Please enter your low number (only use numbers 1 - 256): ";
    cin >> lowNum;
    if ( cin.fail() )
    {
      cin.clear();
      cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
  }

  cout << "lowNum " << lowNum << endl;
}
 
Share this answer
 
You can use the scanf() function[^] which allows you to specify what you are expecting as input:
C++
#include <iostream>
#include <cstdio>
unsing namespace std;

int main() {
   int lowNum, returnValue;
   do {
      cout << "Please enter your low number (only use numbers 1 - 256): ";
      returnValue = scanf("%3d", &lowNum);
   } while (returnValue != 1 || lowNum < 1 || lowNum > 256);
   // ...
   return 0;
}

Or you can do that keeping cin if you need/want to:
C++
#include <iostream>
#include <limits>
using namespace std;

int main() {
   int lowNum;
   cout << "Please enter your low number (only use numbers 1 - 256): ";
   while (!(cin >> lowNum) || lowNum < 1 || lowNum > 256) {
      cin.clear();
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
      cout << "Please only enter numbers between 1 and 256: ";
   }
   // ...
   return 0;
}

Hope this helps. Kindly.
 
Share this answer
 
v2
Quote:
I have tried everything I know how to, but I lack the knowledge to resolve the issue.

When you don't understand why your program is doing something or don't, it is time to turn to debugger and see by yourself what is going on.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900