Validating whether input is an integer

i'm working on this lab..
*********
- Write a C++ program consisting of main plus two other functions which will do the following:

- Take an integer input from the keyboard.

- Send the integer to a function which will output the integer to the screen.

- Send the integer to a second function which will tell the user that the integer is an odd value.

- Do not tell the user anything if the integer is an even value.

- Repeat this process until the user enters something which is not an integer; use input validation to check for validity.

- Any not valid input should terminate the program.
*************************
i got almost everything down. Im ready to set up the loop that would repeat this process until the user enters something that isnt an integer, but i'm having a hard time finding a way to validate whether the user input is an integer or not...
any ideas?
Thanks in advance!


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

#include <iostream>

using namespace std;

void first(int);
void second(int);
int remainder;

int integer;
bool gotValidInput = false;

int main()
{

do
{
    cout << "Please enter an integer" << endl;
    cin >> integer;
    cout << endl;
    first(integer);
    second(integer);
 } while

}

//definition of function first
//this function will output integer into the screen

void first(int integer)
{
    cout << "you entered the integer: " << integer << endl ;
}

//definition of function second
void second(int integer)
{
    remainder = integer % 2;
 if    (remainder != 0)
    cout << endl << "the integer you entered is an odd value" << endl;
    else
        cout << endl;

}

closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    cout << "Please enter an integer" << endl;
    while (cin >> integer)
    {
        first(integer);
        second(integer);
        cout << "Please enter an integer" << endl;
    }
 
    cout << "Wake up! That's not an integer" << endl;
}


Here's a small start.
Last edited on
thanks that helped a little but doesn't address my main concern. i think you gave me a code that validate whether the input (cin) is greater than integer or not.... i need help on finding a way to validate whether the input is an integer or not. any ideas?
i think you gave me a code that validate whether the input (cin) is greater than integer or not


Incorrect. What is happening on line 4 is that cin is trying to pull an integer from the input stream and put it in the variable integer. If the user enters an integer, then this operation will be successful, cin will remain in a good state, and the loop will continue to execute.

If the input is not an integer, then the >> operation to put it in integer will fail, causing cin to go into a fail state, causing the loop to exit.

EDIT: For clarity, "cin" does not represent your input. It represents the standard input stream. You use the ">>" operator to extract data from the input stream into a variable. Typically, when you see cin >> myInteger myInteger is considered your "input" because you've pulled data out of "cin" and put it in "myInteger". This will fail if the ">>" operator doesn't know how to take stuff out of "cin" and put it the variable.
Last edited on
closed account (48T7M4Gy)
Thanks booradley60 that's exactly as I meant it. The idea of just substituting my suggested snippet into the OP program got lost in translation - 1000 pardons from me for that.
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>

using std::cout;
using std::cin;
using std::endl;

void first(int);
void second(int);

bool gotValidInput = false;

int main()
{
    int integer = 0;
    cout << "Please enter an integer" << endl;
    while (cin >> integer)
    {
        first(integer);
        second(integer);
        cout << "Please enter an integer" << endl;
    }
 
    cout << "Wake up! That's not an integer" << endl;
}

//definition of function first
//this function will output integer into the screen

void first(int integer)
{
    cout << "you entered the integer: " << integer << endl ;
}

//definition of function second
void second(int integer)
{
    int remainder = 0;
   
   remainder = integer % 2;
 if    (remainder != 0)
    cout << endl << "the integer you entered is an odd value" << endl;
    else
        cout << endl;
}
Please enter an integer
7
you entered the integer: 7

the integer you entered is an odd value
8
you entered the integer: 8

we
Wake up! That's not an integer
 
Exit code: 0 (normal program termination)


There we go :)
Last edited on
closed account (48T7M4Gy)
Separately I also just noticed that integer and remainder are declared as global variables which is not necessary and verges on bad practice.
closed account (48T7M4Gy)
BTW The reason I said "small start" is that my code doesn't recover from bad input (i.e. when not an integer) which in a 'real' program where it would not be unusual to incorporate error/exception handling code. That aspect can be chased up by following through with the comments booradley60makes on cin and streams.
Thanks guys! Appreciate it!
ok so now it is validating the input as i wanted it to. but now even when i dont enter an integer it (say I enter 3.5), it converts to an integer and it reads out I entered "3" and reads out the statement about it being an odd number, before it reads out the statement that states its not an integer...

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
52

//Abdillahi Mohamed

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

void first(int);
void second(int);



int main()
{
    int integer = 0;
    cout << "Please enter an integer" << endl;
    while (cin >> integer)
    {
        first(integer);
        second(integer);
    }

    cout << "That's not an integer" << endl;
}

//definition of function first
//this function will output integer into the screen

void first(int integer)
{

        cout << "you entered: " << integer << endl ;

}


//definition of function second
void second(int integer)
{
    int remainder = 0;

   remainder = integer % 2;
 if    (remainder != 0)
    cout << endl << "the integer you entered is an odd value" << endl;
    else
        cout << endl;
}



closed account (48T7M4Gy)
This is where you need to read up on streams etc in more detail to trap the input inconsistencies with type int I mentioned. What is happening is the 3 at the start of 3.5 is read from the stream OK but the following . causes the dropout because it is not an integer.

Cheers :)
Topic archived. No new replies allowed.