Infinite while loop

Hello, need some help with while loop, then enter number in this program everything works fine, but if any other character is entered for example a, i get infinite while loop, it not the first time i get this and just wanted to ask how could i fix problem like that.
I thought if anything else is entered than number if statement should get called again.
line 15: "if (std::cin >> rad)" but I just get infinite loop.
How i should fix it?

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
 #include <iostream>
void circle(float rad, float& area, float& circumference, float pi);

int main()
{
	const float Pi = 3.14f;
	float rad;
	float area = 0;
	float circumference = 0;
	bool rightValue = false;

	std::cout << "Please enter radius of the circle: ";
	while (rightValue != true)
	{
		if (std::cin >> rad)
		{
			circle(rad, area, circumference, Pi);
			std::cout << "Area of circle is: " << area << std::endl;
			std::cout << "Circumference of circle is: " << circumference << std::endl;
			rightValue = true;
		}
		else
		{
			std::cout << "Wrong input" << std::endl;
			//std::cin.clear();  does not help
			//std::cin.ignore(rad); does not help either
		}

	}
	system("pause");
	return 0;
}

void circle(float rad, float& area, float& circumference, float pi)
{
	area = pi * rad * rad;
	circumference = 2 * pi * rad;

}
Last edited on
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
#include <iostream>

void circle(float rad, float& area, float& circumference, float pi);

int main()
{
	const float Pi = 3.14f;
	float rad;
	float area = 0;
	float circumference = 0;
	bool again = true;    //CHANGE HERE

	std::cout << "Please enter radius of the circle: ";
	while (again == true)    //CHANGE HERE
	{
		if (std::cin >> rad)
		{
			circle(rad, area, circumference, Pi);
			std::cout << "Area of circle is: " << area << std::endl;
			std::cout << "Circumference of circle is: " << circumference << std::endl;
			again = false;    //CHANGE HERE
		}
		else
		{
			std::cout << "Wrong input" << std::endl;
			//std::cin.clear();  does not help
			//std::cin.ignore(rad); does not help either
			again = false;    //CHANGE HERE
		}

	}
	system("pause");
	return 0;
}

void circle(float rad, float& area, float& circumference, float pi)
{
	area = pi * rad * rad;
	circumference = 2 * pi * rad;

}


Your condition for the while loop to execute never changes when the input it wrong, hence the infinite loop
Consider writing get_radius() as a separate (additional) function:

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
#include <iostream>

double get_radius()
{
    std::cout << "Please enter radius of the circle: ";
    double radius ;

    if( std::cin >> radius ) // if the user entered a number
    {
        if( radius > 0 ) return radius ; // return it if it is positive
        else std::cout << "enter a positive value\n" ;
    }

    else // the user did not enter a number
    {
        std::cout << "enter a number\n" ;
        std::cin.clear(); // clear he failed state
        std::cin.ignore( 1000, '\n' ); // extract and throw the offending input away
    }

    return get_radius() ; // input failed (did not return), try again
}

int main()
{
    const double radius = get_radius() ;
    std::cout << "radius: " << radius << '\n' ;
}
If a read operation fails std::cin will be put in an error state which will cause all read operations that comes after to also fail. To clear the error state you can use the clear() member function. If you don't want to try and read the same input again (the one that caused the error) you need to get rid of it before reading again. You can do that using the ignore() member function.

1
2
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');

The second argument to ignore is the character where it will stop discarding input. The newline character '\n' is used because we want to discard the whole line.

The first argument to ignore is the maximum number of character that will be discarded if the character that was passed as second argument is never found. Above I used std::numeric_limits<std::streamsize>::max() because it's the maximum but any large value should do.
Topic archived. No new replies allowed.