Loop for checking correct integer input

Hello everyone,

I want to write a small "calculator" that can handle wrong input of int. In order to implement that, I used a function that checks the input using a while-loop. It works properly, if I add the loop directly into my int main function. But when I'm using my input_nr function, it does'nt return the new value of the integer (here, for instance a). My value of a is always the initilized one in the int main() function.
I think something in my loop is wrong or missing that properly returns my new input for a.

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <fstream>
#include "square.h"

using namespace std;

int input_nr(int x)
{
 int y = 0; //I added the new int y, because I thought the loop can't re-initilize "x"
 x = y;  //set my input to y = 0;
	while (cin) {
			cin >> y; //input new value of y
			if (!cin || y > 10 || y < 1){
                        cout << "No proper Input."; cin.clear(); cin.ignore(); 
                        cout << "\nRepeat input of integer: "; //this works ok
			}
			else
			{
				x=y; //set x to y
				break;
			}
		}
	return (x); // I think something went wrong here. It also does'nt work;
//when I just write "return (x+5)" or so, for example
};

int main()
{
	int a = 1; //i have to initialize my variable 
//before using it in a function
	int b;
	char o;
	cout << "Enter two integers (between 1 and 10) and an operator.\n";
	while (cin)
	{
		cout << "\nFirst integer (1-10): "; input_nr(a); 
// If i am using the while-statement of my input_nr function here directly, 
//everything goes well
		cout << "\na = " << a; 
// it always outputs the initialized value of a, so 1 and not the new
//value from the input_nr function
	}
	//end of program
	cout << endl;
	cin.get();
	return 0;
}


Thanks in advance, hrxs1!
Last edited on
I would do it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
#include<string>
using namespace std;

int getInt (const int min, const int max)
{
  int num;

  while (true)
  {
    cout << "\nPlease enter a number between " << min << " and " << max << ": ";
    if (!(cin >> num))
    {
      cout << "Invalid input - please try again";
      cin.clear ();
      cin.ignore (255, '\n');
    }
    else if (num >= min && num <= max)
    {
      return num;
    }
    else
    {
      cout << "Invalid input - please try again";
      cin.clear ();
      cin.ignore ();
    }
  }
}

int main ()
{
  int num = getInt (1, 10);
  cout << "Num: " << num << "\n";

  return 0;
}
Thanks Thomes 1965! This works great and it completely makes sense to me, except line 16.
What does 255 cin.ignore() mean?
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>

int input_nr()
{
    bool keep_going = true;
    int y = 0;
    
    do
    {
        std::cin >> y;
        
        if (!std::cin.good() || y > 10 || y < 1)
        {
            std::cout << "No proper Input.";
            std::cin.clear();
            std::cin.ignore();
            std::cout << "\nRepeat input of integer: ";
            keep_going = true;
        }
        else
            keep_going = false;
    }
    while (keep_going == true);
    
    return y;
}

int main()
{
    int a = 1;
    int counter = 0;
    
    std::cout << "Enter two integers (between 1 and 10) and an operator.\n";
    
    while(counter < 2)
    {
        std::cout << counter << " Integer (1-10): ";
        a = input_nr();
        std::cout << "a = " << a << '\n';
        counter++;
    }
    
    return 0;
}


An alternative way.

ignore clears the cin buffer where it still contains erroneous data. eg if a character is entered or other non-integer 'stuff'


Enter two integers (between 1 and 10) and an operator.
0 Integer (1-10): 2
a = 2
1 Integer (1-10): 0
No proper Input.
Repeat input of integer: 9
a = 9
Program ended with exit code: 0
Last edited on
Thanks. I will try to understand it.
Last edited on
Topic archived. No new replies allowed.