infinite loop?

okay I'm trying to do the little test i found on this site to get better, but Ive hit a strange snag that i cant understand...
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>

int guess(int min, int max);
int main(){
    int min = 1;
    int max = 100;
    int count=0;
    int temp;
    int pAnswer;
    std::cout<<"Please think of a number between 1 and 100\n";
    std::cout<<"I will guess it in 7 or less moves, dont cheat now!\n";
    std::cout<<"Type 0 as an answer if i guess it,"<<
                " 1 if im too high, 2 if im to low!\n";
    std::cout<<"lets begin then!\n";
    do{
        count++;
        temp = guess(min,max);
        std::cout<<"Try #"<<count<<": Are you thinking of "<<temp<<"?:";
        std::cin>>pAnswer;
        switch(pAnswer){
            case 1:
            std::cout<<"Too high!\n";
            max = temp;
            break;
            case 2:
            std::cout<<"Too high!\n";
            min = temp;
            break;
            default:
            std::cout<<"Type 0 as an answer if i guess it,"<<
            " 1 if im too high, 2 if im to low!\n";
            count--;
            break;
        }

    }while(pAnswer);
}
int guess(int min,int max){
    int var = ((max - min)/2)+min;
    return var;
}


this is supposed to guess the number your thinking of, and when you type in something other than the 1,2, or 0, it should give an error and restart.. but when it restarts it skips the cin, and loops for ever, and i don't understand why?

Ive been asking a lot of questions here lately... thanks for any help!
Hi!

int guess(int min, int max);


The guess function makes the average of the min and max. For example: min = 10, max = 20 then this function give back the value 15.

The main function should return some value:

1
2
3
4
int main()
{
   return 0;
}


THis switch checks the 1 and 2 cases but if you type 0 or 3 then the default instructions is executed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
switch(pAnswer){
            case 1:
            std::cout<<"Too high!\n";
            max = temp;
            break;
            case 2:
            std::cout<<"Too high!\n";
            min = temp;
            break;
            default:
            std::cout<<"Type 0 as an answer if i guess it,"<<
            " 1 if im too high, 2 if im to low!\n";
            count--;
            break;
        }


If you type 0 then the while loop is terminated because the condition will become false (0);

So, it works for me.
Last edited on
It's the standard input problem.

1
2
int x;
std::cin >> x;


Although it is convenient to be able to write this (particularly, it is convenient to be able to
chain reads, especially from a file: inputFile >> var1 >> var2 >> var3;, it isn't
useful without a lot of modification.

The problem is that if the stream (std::cin) does not contain input that is integral (ie, "Hello"
is not integral, nor is "z4"), the input operation fails, x is left undefined, and the stream is
left unchanged. Which means if you attempt to read an integer a second time, the exact
same thing will happen. And again. And again. Etc.

You have to check the stream for an error after attempting to read an integer, and upon
error you have to flush the errant characters. Most people will flush until a newline,
since the user is required to press ENTER anyway.

1
2
3
4
5
6
7
8
9
int x;
while( 1 ) {
   std::cout << "Please enter an integer: " << std::flush;
   std::cin >> x;
   if( !std::cin ) {
       std::cout << "Invalid input entered." << std::endl;
       std::cin.ignore( std::numeric_limits<streamsize>::max(), '\n' );
   } else break;
}

@screw,
yes but the problem is if i type a letter instead of a number it falls into an infinite loop and prints the same guess for ever without ever pausing...


@jsmith
im sort of new to programming in c++, and i think i understand what you mean, but i dont think im getting, can you link me to an article that explains it or, tell me what cin.ignore is? and maybe what numeric_limits and all that means...
Ok, your problem was what happen if a letter was typed.

There are lot of solution.

@jsmith suggests the ignore function.
http://www.cplusplus.com/reference/iostream/ifstream/

Or you can query that the reading was successful or not.

int number;
cin >> number;
if (cin.good())
{
// the reading was successful
}
from what i can find, std::cin.ignore( std::numeric_limits<streamsize>::max(), '\n' ); will empty the input buffer, but what does std::numeric_limits<streamsize>::max() mean? i cant find anything on it except references to this same code, i guess it isn't necessary to know it exactly but i don't feel right using code i cant undertand...
and when i try to use it it seems it causes an error... i must not be getting something...
Last edited on
It is the maximum amount of characters that could be in a stream (basically ignoring everything until the next '\n').
when i use:
std::cin.ignore( std::numeric_limits<streamsize>::max(), '\n' );
i get the error:
error: `streamsize' was not declared in this scope|
...
It should be declared in iostream, maybe try std::streamsize?
See if this helps to explain the problem any better. The entire I/O section of this FAQ site has been really helpful for me in the past. It's the same thing that jsmith is saying but perhaps the examples there will help clarify the issue.
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.2

Oh, and you'll want to continue with FAQ15.3 as well. The subsequent FAQs also have relevant info on this subject matter.
Last edited on
Topic archived. No new replies allowed.