Infinite loop problem

When I enter a double number, I get an infinite loop, and when I enter in a character, I get an infinite loop. How can this be fixed?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
        int spoolsOrdered;

	do
	{
		cout << "How many spools were ordered? ";
		cin >> spoolsOrdered;
	}while(spoolsOrdered < 1);



	system("pause");
	return 0;
}
You can check to see if the fail bit flag was set, and if so, clear the flag, like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <limits>

int main() {
    int spoolsOrdered = 0;

    std::cout << "How many spools were ordered? ";
    while (!(std::cin >> spoolsOrdered) && spoolsOrdered < 1) {
        // clear the fail flag
        std::cin.clear();
        // remove all other potentially 'dangerous' characters
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

        std::cout << "Invalid input." << std::endl;
        std::cout << "How many spools were ordered? ";
        // loop until a valid input is given
    }

    // Pause the console
    std::cin.sync();
    std::cin.ignore();

    return 0;
}
thanks, I tried that, and it worked perfectly, but that was only a part of the function. When I put it into my full function, letters caused the program to break and end, and double numbers allowed invalid input, although there were some errors

Invalid input.
How many spools were ordered? How many spools were in stock? 


this is the original void function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void getOrder(int& spoolsOrdered, int& spoolsStock, char& spec)
{
	do
	{
		cout << "How many spools were ordered? ";
		cin >> spoolsOrdered;
	}while(spoolsOrdered < 1);

	do
	{
		cout << "How many spools are in stock? ";
		cin >>  spoolsStock;
	}while( spoolsStock < 0);

	cout << "Are special shipping charges required? (y or n): ";
	cin >> spec;
	spec = toupper(spec);
	
}
I like to use a separate function for grabbing and validating inputs.

I suppose you could write a getInt function like this (note: untested):
1
2
3
4
5
6
7
8
9
10
11
12
int getInt()
{
    int ret;
    while (!(std::cin >> ret))
    {
        std::cout << "Invalid input, try again: ";
        std::cin.clear(); // Clear error flags
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Clear out the junk
        // (You'll need to #include <limits> for that line above)
    }
    return ret;
}

That way, you can just write
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void getOrder(int& spoolsOrdered, int& spoolsStock, char& spec)
{
    do
    {
        cout << "How many spools were ordered? ";
        spoolsOrdered = getInt();
    }while(spoolsOrdered < 1);

    do
    {
        cout << "How many spools are in stock? ";
        spoolsStock = getInt();
    }while( spoolsStock < 0);

    cout << "Are special shipping charges required? (y or n): ";
    cin >> spec;
    spec = toupper(spec);
    
}

With a few minor modifications, you can get it to work for any std::istream object, or even any type of variable that supports the >> extraction operator (not just int).
Topic archived. No new replies allowed.