Input Validation with Ints and Doubles

So i am trying to have the user only receive integers when they are choosing an array size but the numbers don't add up if I were to to type in a floating number. If they type in letters then the right error message pops up but I can't get right if they type in 4.4, 1.21, etc. the way I currently have it. Am I doing this the hard way? Would I be better off using stringstream? Is it possible to do it this way?

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
 
#include <iostream>
#include <limits>


int main()
{
    const int CAPACITY = 100;
    int choice;
    int size = 0;
    double array[CAPACITY];

        do
        {
        std::cout << "Hey there! Pick an option" << std::endl;
        std::cout << "Hit 1 to do some math " << std::endl;
        std::cout << "Hit 2 to quit" << std::endl;
        std::cin >> choice;

        if (choice == 1)
        {

            std::cout << "Choose the size of your array. The array can be set to capacity of 100" << std::endl;
            //std::cin >> size;

            if((std::cin >> size).fail()) {
                std::cin.clear();
                std::cin.ignore(1000, '\n');
                    std::cout << "Invalid input" << std::endl;
                    continue;
                }
            if(size <= 0){
                std::cout << "Sorry, that not a valid option" << std::endl;
                continue;
            }
            if (size > CAPACITY){
                std::cout << "Sorry, that is too large" << std::endl;
                continue;
            }
            if (size < CAPACITY){
                std::cout << "Add some numbers to your array" << std::endl;

                for (int i=0; i < size; i++)
                {
                    std::cout << "Enter a number: " << std::endl;
                    std::cin >> array[i];
                }
                std::cout << "Ok, hit 1 to sum up all the numbers or 2 to get the average" << std::endl;
                std::cin >> choice;

                if(choice == 1){
                    std::cout << getSum(array, size);
                    continue;

                }
                else if(choice == 2){
                    std::cout << getAverage(array, size);
                    continue;

                }
                if(!std::cin) {
                    std::cin.clear();
                    //and empty it
                    std::cin.ignore(1000, '\n');
                        std::cout << "Invalid input, sorry" << std::endl;
                        continue;
                }
            }
        }
        if(choice == 2){
            std::cout << "Buh bye!" <<std::endl;
            return 0;
        }
        else {
            std::cin.clear();
            std::cin.ignore(1000, '\n');
            std::cout << "Invalid input, sorry!" << std::endl;
        }

        }while(choice !=2);

    return 0;
}

You could try using a float auxiliary variable then cast it into int in order to don't mess up the program if you type rational numbers.


look at this for example, number1 is float, so if you introduce a rational number (4.5 for example), the program won't do crazy stuff. Then we cast it as an int into number2 and this will give use 4 instead, the integer value you need. Of course if you type an int value from the start (as number1) it won't make a difference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
#include <iostream>


int main()
{
    float number1;
    int number2;
    
    std::cout<<"Introduce an integer number\n";
    std::cin>>number1;
    number2=static_cast<int>(number1);
    
    std::cout<<number2;

    return 0;
}
Input as a string and validate the string

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
53
54
55
56
57
#include <iostream>
#include <string>
#include <regex>
#include <cctype>

bool isInt(const std::string input);
bool isFloat(const std::string input);


int main()
{
	std::cout << "Please enter a float ";
	std::string input;
	std::getline(std::cin, input);


	if (isFloat(input))
		std::cout << input << " is a float " << std::endl;
	else if (isInt(input))
		std::cout << input << " is an int " << std::endl;
	else
		std::cout << "Invalid input " << std::endl;



	std::cin.ignore();
	return 0;

}



bool isInt(const std::string input)
{
	if (input.empty())
		return false;


	for (const auto &element : input)
	{
		if (!isdigit(element))
			return false;
	}

	return true;
}


bool isFloat(const std::string input)
{
	if (input.empty())
		return false;

	std::regex test("[ ]*\\d*\\.\\d+[ ]*");

	return std::regex_match(input, test);
}


Note: codeblocks default compiler does not support regex
Last edited on
Topic archived. No new replies allowed.