[RESOLVED] Input Validation
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: [RESOLVED] Input Validation

  1. #1
    Join Date
    May 2015
    Posts
    8

    [RESOLVED] Input Validation

    I have routines that will check an input of a full number for integer or floating point. What I would like to do is to input a character, validate it as being a number (0-9, -, .) and then keep adding to it until a complete number has been entered and validated along the way. I have a routine in BASIC that does this, but being a novice in C++ can't translate it. I've tried on-line translators but they either come up with errors or produce complicated code that I don't understand or VC++, which i don't use. I use Code::Blocks with g++. Can anyone help?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: Input Validation

    [new thread started]

    What is the c++ code produced by the on-line translator?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  3. #3
    Join Date
    May 2015
    Posts
    8

    Re: Input Validation

    I couldn't get the VB to C++ converters to work and the BASIC is QB64, so the closest I got was VB to C# converter. The code is below and I think some of it is probably rubbish. It's really the function void EnterInteger() that I need to get my head around, but it just won't go in. If you can help, I'd be most grateful.

    Code:
    string num;
    num = EnterInteger("");
    // my change
    PRINT;
    "STRING RESULT: ";
    num;
    
    result = double.Parse(num);
    
    print;
    "NUMERICAL OUTPUT: ";
    result;
    
    END
        
        void EnterInteger(void prompt) {
            col = POS(0);
            row = CSRLIN;
            b = "";
            while ((a != 13)) {
                k = INKEY;
                if ((k.Length == 1)) {
                    a = ASC(k);
                    // IF k$ = "-" AND LEN(b$) = 0 THEN b$ = "-"
                    // IF k$ = "." AND INSTR(b$, ".") = 0 THEN b$ = b$ + "."
                    if (((47 < a) 
                                && (a < 58))) {
                        b = (b + k);
                    }
                    
                    if (((a == 8) 
                                && (b.Length > 0))) {
                        b = b.Substring(0, (b.Length - 1));
                    }
                    
                    LOCATE;
                    row;
                col:
                    PRINT;
                    (prompt + SPACE((b.Length + 1)));
                    LOCATE;
                    row;
                col:
                    PRINT;
                    (prompt + b);
                    _LIMIT;
                    60;
                    EnterInteger = b;
                }         
            }
        }
    I've also included the BASIC code below, so you can see where it is coming from:

    Code:
    '***************************************************
    ' Input numbers one at a time, whilsy checking that
    ' the entry is withing the range 0-9 and - and .
    '***************************************************
    
    DIM num AS STRING
    
    num = EnterInteger$("")  
    PRINT "STRING RESULT: ";num     
    result = val(num)				
    print "NUMERICAL OUTPUT: "; result  
    
    END
    
    FUNCTION EnterInteger$(prompt$)
        col = POS(0): row = CSRLIN
        b$ = ""
        WHILE a <> 13
            k$ = INKEY$
            IF LEN(k$) = 1 THEN
                a = ASC(k$)
                'IF k$ = "-" AND LEN(b$) = 0 THEN b$ = "-"
                'IF k$ = "." AND INSTR(b$, ".") = 0 THEN b$ = b$ + "."
                IF 47 < a AND a < 58 THEN b$ = b$ + k$
                IF a = 8 AND LEN(b$) > 0 THEN b$ = LEFT$(b$, LEN(b$) - 1)
            END IF
            LOCATE row, col: PRINT prompt$ + SPACE$(LEN(b$) + 1)
            LOCATE row, col: PRINT prompt$ + b$
            _LIMIT 60      'stops cursor flicker
        WEND
        EnterInteger$ = b$
    END FUNCTION

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: Input Validation

    Try this c++

    Code:
    #include <conio.h>
    #include <cctype>
    #include <string>
    #include <iostream>
    using namespace std;
    
    double enterint(const string& prmp)
    {
    	string num;
    
    	cout << prmp;
    
    	for (char ch; ((ch = getch()) != '\n') && (ch != '\r');)
    		if (isdigit(ch) || ((ch == '.') && (num.find('.') == string::npos)) || ((ch == '-') && num.empty())) {
    			num += ch;
    			cout << ch;
    		} else
    			if ((ch == '\b') && !num.empty()) {
    				cout << ch << ' ' << ch;
    				num.erase(prev(num.end()));
    			} else
    				if (ch != '\b')
    					cout << '\a';
    
    	return num.empty() ? 0.0 : stod(num);
    }
    
    int main()
    {
    	auto num =enterint("Enter a number: ");
    
    	cout << "\nNumerical input: " << num;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  5. #5
    Join Date
    May 2015
    Posts
    8

    Re: Input Validation

    @2kaud
    Thank you very much for the code. I can understand quite a bit of it and I'm reading up to check the rest. Unfortunately, I get an error "Line 26: stod was not declared in this scope". I'm not sure why this is happening and I'm not sure I understand Line 26.
    I am using Code::Blocks and GCC g++.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: Input Validation

    stod() is a c++11 feature. You need to make sure that you tell g++ that you are compiling for c++11. I think the option is -std=c++11

    stod() is doing what Basic val() is doing. It returns a double numeric value of the string. What that line does is check that a number has been entered before trying to convert it. If no number has been entered when <CR> pressed then 0 will be returned (or can be any other number required). You could change the code so that not entering a number before pressing <CR> is not allowed.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  7. #7
    Join Date
    May 2015
    Posts
    8

    Re: Input Validation

    @2kaud
    I have modified the code you provided so that it will input numbers 0-9 only. I suspect there are probably easier ways to do this, but I am keeping the same basic code so it is clear in my aging mind. I have further modified the code so it will accept a negative integer.

    For anyone who is interested, I have shown all three solutions below:

    Enter floating point number:
    Code:
    #include <conio.h>
    #include <cctype>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    double enterint(const string& prmp)
    {
    	string num;
    
    	cout << prmp;
    
    	for (char ch; ((ch = getch()) != '\n') && (ch != '\r');)
    		if (isdigit(ch) || ((ch == '.') && (num.find('.') == string::npos)) || ((ch == '-') && num.empty())) {
    			num += ch;
    			cout << ch;
    		} else
    			if ((ch == '\b') && !num.empty()) {
    				cout << ch << ' ' << ch;
    				num.erase(prev(num.end()));
    			} else
    				if (ch != '\b')
    					cout << '\a';
    
    	return num.empty() ? 0.0 : stod(num);
    }
    
    int main()
    {
    	auto num =enterint("Enter a number: ");
    
    	cout << "\nNumerical input: " << num;
    }

    Enter integer number (0-9 no minus sign)
    Code:
    #include <conio.h>
    #include <cctype>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    double enterint(const string& prmp)
    {
    	string num;
    
    	cout << prmp;
    
    	for (char ch; ((ch = getch()) != '\n') && (ch != '\r');)
    		//if (isdigit(ch) || ((ch == '.') && (num.find('.') == string::npos)) || ((ch == '-') && num.empty())) {
    		if (isdigit(ch) ) {
    			num += ch;
    			cout << ch;
    		} else
    			if ((ch == '\b') && !num.empty()) {
    				cout << ch << ' ' << ch;
    				num.erase(prev(num.end()));
    			} else
    				if (ch != '\b')
    					cout << '\a';
    
    	return num.empty() ? 0.0 : stod(num);
    }
    
    int main()
    {
    	auto num =enterint("Enter a number: ");
    
    	cout << "\nNumerical input: " << num;
    }

    Enter integer number (0-9 including minus)
    Code:
    #include <conio.h>
    #include <cctype>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    double enterint(const string& prmp)
    {
    	string num;
    
    	cout << prmp;
    
    	for (char ch; ((ch = getch()) != '\n') && (ch != '\r');)
    		//if (isdigit(ch) || ((ch == '.') && (num.find('.') == string::npos)) || ((ch == '-') && num.empty())) {
    		//if (isdigit(ch) ) {
    		if (isdigit(ch) || ((ch == '-') && num.empty())) {
    			num += ch;
    			cout << ch;
    		} else
    			if ((ch == '\b') && !num.empty()) {
    				cout << ch << ' ' << ch;
    				num.erase(prev(num.end()));
    			} else
    				if (ch != '\b')
    					cout << '\a';
    
    	return num.empty() ? 0.0 : stod(num);
    }
    
    int main()
    {
    	auto num =enterint("Enter a number: ");
    
    	cout << "\nNumerical input: " << num;
    }
    @2kaud
    Thank you again for resolving my problem so quickly. I've still got a lot to learn with C++, but I think the above routines are a pretty basic requirement and save a lot of error checking and looping.

  8. #8
    Join Date
    May 2015
    Posts
    8

    Re: [RESOLVED] Input Validation

    @2kaud
    Your code worked very well for me, but when I incorporated it into a program I received criticism for using conio.h in a C++ program. I looked around on the internet and found some code on the Cplusplus forum by Duthomhas that got around this.

    The code is reproduced in the following progams:

    Enter integer:

    Code:
    //*************************************
    // Original code by 2kaud codeguru forum
    // 13 November 2017 with modification to
    // accept integers 0-9 only by RNBW
    // 16 November 2017.
    // Filename: EnterInteger_02.cpp
    //*************************************
    
    #include <cstdio>
    #include <cctype>
    #include <string>
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    // This code from Duthomhas in CPlusPlus Forum
    // 18 August 2008
    TCHAR getkeypress()
      {
      DWORD mode, count;
      HANDLE h = GetStdHandle( STD_INPUT_HANDLE );
      if (h == NULL) return 0;  // not a console
      GetConsoleMode( h, &mode );
      SetConsoleMode( h, mode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT) );
      TCHAR c = 0;
      ReadConsole( h, &c, 1, &count, NULL );
      SetConsoleMode( h, mode );
      return c;
      }
    
    double enterInteger(const string& prmp)
    {
    	string num;
    
    	cout << prmp;
    
    	for (char ch; ((ch = getkeypress()) != '\n') && (ch != '\r');)
    	//for (char ch; ((ch = cin.get()) != '\n') && (ch != '\r');)
    		//if (isdigit(ch) || ((ch == '.') && (num.find('.') == string::npos)) || ((ch == '-') && num.empty())) {
    		if (isdigit(ch) ) {
    			num += ch;
    			cout << ch;
    		} else
    			if ((ch == '\b') && !num.empty()) {
    				cout << ch << ' ' << ch;
    				num.erase(prev(num.end()));
    			} else
    				if (ch != '\b')
    					cout << '\a';
    	return num.empty() ? 0.0 : stod(num);
    }
    
    int main()
    {
    	auto num =enterInteger("Enter a number: ");
    
    	cout << "\nNumerical input: " << num;
    }
    and, Enter Floating Point:

    Code:
    //*************************************
    // Original code by 2kaud codeguru forum
    // 13 November 2017 with minor modification
    // by RNBW 16 November 2017.
    // Filename: EnterNumeric_01.cpp
    //*************************************
    
    #include <cctype>
    #include <string>
    #include <iostream>
    #include <iomanip>
    #include <windows.h>
    
    using namespace std;
    
    
    // This code from Duthomhas in CPlusPlus Forum
    // 18 August 2008
    TCHAR getkeypress()
      {
      DWORD mode, count;
      HANDLE h = GetStdHandle( STD_INPUT_HANDLE );
      if (h == NULL) return 0;  // not a console
      GetConsoleMode( h, &mode );
      SetConsoleMode( h, mode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT) );
      TCHAR c = 0;
      ReadConsole( h, &c, 1, &count, NULL );
      SetConsoleMode( h, mode );
      return c;
      }
    
    double enterNumber(const string& prmp)
    {
    	string num;
    
    	cout << prmp;
    
    	for (char ch; ((ch = getkeypress()) != '\n') && (ch != '\r');)
    		if (isdigit(ch) || ((ch == '.') && (num.find('.') == string::npos)) || ((ch == '-') && num.empty())) {
    			num += ch;
    			cout << ch;
    		} else
    			if ((ch == '\b') && !num.empty()) {
    				cout << ch << ' ' << ch;
    				num.erase(prev(num.end()));
    			} else
    				if (ch != '\b')
    					cout << '\a';
    
    	return num.empty() ? 0.0 : stod(num);
    }
    
    int main()
    {
    	/* Note: here you could insert a seperate prompt,
    		say: cout << "ENTER A NUMBER: "; and then change the prompt
    		in the function to  enterNumber("");.   This would make the function
    		useable in most situations.
    	*/
    	//auto number =enterNumber("Enter a number: ");
    	double number =enterNumber("Enter a number: ");
    
    	cout << "\nNumerical input: " << fixed << setprecision(2) << number;
    }
    I hope the code is of use.

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: [RESOLVED] Input Validation

    received criticism for using conio.h in a C++ program
    Code:
    CHAR getkeypress()
      {
      DWORD mode, count;
      HANDLE h = GetStdHandle( STD_INPUT_HANDLE );
      if (h == NULL) return 0;  // not a console
      GetConsoleMode( h, &mode );
      SetConsoleMode( h, mode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT) );
      TCHAR c = 0;
      ReadConsole( h, &c, 1, &count, NULL );
      SetConsoleMode( h, mode );
      return c;
      }
    So you use this function to do what getch() does. So every time your code gets a char, it executes all this. Why re-invent the wheel? Apart from ReadConsole(), the rest could be done once at the start of the program and pass h as a param to a function say readch(HANDLE h). Also note that this code is not testing for errors returned by the called functions (SetConsoleMode, ReadConsole() etc etc) which it should if used in production code.

    IMO you use what's available to do what's required before writing code to provide what's already provided.
    Last edited by 2kaud; November 18th, 2017 at 04:26 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  10. #10
    Join Date
    May 2015
    Posts
    8

    Re: [RESOLVED] Input Validation

    @2kaud
    Thank you for your response.
    Yes, you are correct. The code replaces getch(). The code was all I could find that actually did the same job, not echoing input to the screen. I'm a beginner at C++, so I can't say that I understand the code. No doubt this will come in due course. Unfortunately, I don't understand what you are saying either. Perhaps you could put it into one of the code examples I have provided, so that I can go through it and see if I can get an understanding.

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: [RESOLVED] Input Validation

    Apart from a test check (for .), enter floating point and enter integer are basically the same. Rather than duplicating the code (and having multiple places to change if a code revision is required), why not have one function that either allows int input or floating input? That way there is only 1 function to alter if a change is required.

    Rather than set the console mode for each key input, it would be better to just set the console mode once at the start of the numeric input and reset after input has been entered. That way you aren't executing all the console init code for every key but just once.

    It would also be more in keeping with c++ code to wrap all of this into a class rather than having separate functions. So consider

    Code:
    #define WIN32_LEAN_AND_MEAN
    #include <Windows.h>
    #include <cctype>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    class number {
    public:
    	number()
    	{
    		if ((h = GetStdHandle(STD_INPUT_HANDLE)))
    			if (!(GetConsoleMode(h, &mode) && SetConsoleMode(h, mode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT))))
    				h = 0;
    
    	}
    
    	~number()
    	{
    		if (h)
    			SetConsoleMode(h, mode);
    	}
    
    	char getkey()
    	{
    		char c;
    		DWORD count;
    
    		return h ? (ReadConsoleA(h, &c, 1, &count, NULL) ? c : 0) : 0;
    	}
    
    	int enterint(const string& prm = "")
    	{
    		auto num = enternum(prm);
    
    		return num.empty() ? 0 : stoi(num);
    	}
    
    	double enterfloat(const string& prm = "")
    	{
    		auto num = enternum(prm, numtyp::tfloat);
    
    		return num.empty() ? 0.0 : stod(num);
    	}
    
    private:
    	enum class numtyp { tint, tfloat };
    
    	HANDLE h;
    	DWORD mode;
    
    	string enternum(const string& prmp, numtyp nt = numtyp::tint)
    	{
    		string num;
    
    		cout << prmp;
    
    		for (char ch; ((ch = getkey()) != '\n') && (ch != '\r') && ch;)
    			if (isdigit(ch) || ((nt == numtyp::tfloat) && (ch == '.') && (num.find('.') == string::npos)) || ((ch == '-') && num.empty())) {
    				num += ch;
    				cout << ch;
    			} else
    				if ((ch == '\b') && !num.empty()) {
    					cout << ch << ' ' << ch;
    					num.erase(prev(num.end()));
    				} else
    					if (ch != '\b')
    						cout << '\a';
    
    		return num;
    	}
    };
    
    int main()
    {
    	auto numi = number().enterint("Enter an int number: ");
    	cout << endl;
    
    	auto numd = number().enterfloat("Enter a float number: ");
    	cout << endl;
    
    	cout << "Integer input: " << numi << endl;
    	cout << "Float input: " << numd << endl;
    }
    Last edited by 2kaud; November 18th, 2017 at 10:54 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  12. #12
    Join Date
    May 2015
    Posts
    8

    Re: [RESOLVED] Input Validation

    @2kaud
    Thank you for your code.
    Very interesting and I think I get the gist of it whilst not understanding it fully. It's a bit more advanced than me at the moment.
    What it doesn't do, is restrict input to a positive integer and there are times when this is required, at least in the program that I am developing. For instance, entering an integer as a type of load (in civil engineering). If a minus is entered later code may not pick this up and for presentation purposes it would mess up the output. One of the routines I posted yesterday does this and I have another based on the same code that will allow a negative integer.
    However, I'll have a look at incorporating your code into my program and see how it sits.

    As an aside, just to explain the program I am developing to develop my understanding of C++, I am developing a program based on a proven BASIC program, but using C++. Initially, it will follow a BASIC route, but will be continuously changed as I develop in C++. I expect the finished article to bear little resemblance to the original, especially as I intend to develop it as a GUI. I know there are arguments to forget BASIC completely when developing in C++, but it makes it easier for my old decrepit brain that sometimes finds it hard to take in new things.

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: [RESOLVED] Input Validation

    What it doesn't do, is restrict input to a positive integer
    That is easily changed. Consider

    Code:
    #define WIN32_LEAN_AND_MEAN
    #include <Windows.h>
    
    #include <cctype>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    class number {
    public:
    	number()
    	{
    		if ((h = GetStdHandle(STD_INPUT_HANDLE)))
    			if (!(GetConsoleMode(h, &mode) && SetConsoleMode(h, mode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT))))
    				h = 0;
    
    	}
    
    	~number()
    	{
    		if (h)
    			SetConsoleMode(h, mode);
    	}
    
    	char getkey()
    	{
    		char c;
    		DWORD count;
    
    		return h ? (ReadConsoleA(h, &c, 1, &count, NULL) ? c : 0) : 0;
    	}
    
    	int enterint(const string& prm = "")
    	{
    		auto num = enternum(prm);
    
    		return num.empty() ? 0 : stoi(num);
    	}
    
    	int enterposint(const string& prm = "")
    	{
    		auto num = enternum(prm, numtyp::tpos);
    
    		return num.empty() ? 0 : stoi(num);
    	}
    
    	double enterfloat(const string& prm = "")
    	{
    		auto num = enternum(prm, numtyp::tfloat);
    
    		return num.empty() ? 0.0 : stod(num);
    	}
    
    private:
    	enum class numtyp { tint, tfloat, tpos };
    
    	HANDLE h;
    	DWORD mode;
    
    	string enternum(const string& prmp, numtyp nt = numtyp::tint)
    	{
    		string num;
    
    		cout << prmp;
    
    		for (char ch; ((ch = getkey()) != '\n') && (ch != '\r') && ch;)
    			if (isdigit(ch) || ((nt == numtyp::tfloat) && (ch == '.') && (num.find('.') == string::npos)) || (((nt == numtyp::tfloat) || (nt == numtyp::tint)) && (ch == '-') && num.empty())) {
    				num += ch;
    				cout << ch;
    			} else
    				if ((ch == '\b') && !num.empty()) {
    					cout << ch << ' ' << ch;
    					num.erase(prev(num.end()));
    				} else
    					if (ch != '\b')
    						cout << '\a';
    
    		return num;
    	}
    };
    
    
    int main()
    {
    	auto numi = number().enterint("Enter an int number: ");
    	cout << endl;
    
    	auto numd = number().enterfloat("Enter a float number: ");
    	cout << endl;
    
    	auto numpi = number().enterposint("Enter a positive integer: ");
    	cout << endl;
    
    	cout << "Integer input: " << numi << endl;
    	cout << "Float input: " << numd << endl;
    	cout << "Positive integer input: " << numpi << endl;
    }
    Last edited by 2kaud; November 18th, 2017 at 10:53 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: [RESOLVED] Input Validation

    I get the gist of it whilst not understanding it fully. It's a bit more advanced than me at the moment.
    How are you learning c++? For books see http://forums.codeguru.com/showthrea...560001-C-Books

    Also, consider these web resources

    http://www.learncpp.com/
    http://www.cplusplus.com/
    http://en.cppreference.com/w/cpp
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  15. #15
    Join Date
    May 2015
    Posts
    8

    Re: [RESOLVED] Input Validation

    @2kaud
    Thank you for the code and for the advice on books and tutorials, some of which I've got. I've not got the tutorial at http://en.cppreference.com/w/cpp, but I have bookmarked it and will have a good look. It looks very comprehensive.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)