user input problem

Hi,

my code is a binary search program based on an input from the user. The input is either an integer or the character 'x'. What I am trying to do is have control exit the program if an 'x' is entered. But I can't seem to do that. The program just bypasses every command after 'x' is entered. I don't want to use get() and isdigit() since that hasn't been covered yet in the book I got this problem from.

Thanks for any help.

K

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main(){
    
    int array[] = {1,4,5,6,9,14,21,23,28,31,35,42,46,50,53,57,62,63,65,74,79,89,95};
    int elts = sizeof(array)/sizeof(int);
    PrintArray(array,elts);
    int key = 0;
    
    
    for(;;){
            cout << "Enter a search key (or 'x' to Exit): ";
            cin  >> key;
            int position = BinSearch(array,elts,key);
            if(position != -1){
                   cout << key << " is in position " << position << endl;
            }
            else cout << key << " is not in the list" << endl;
    }
    
    
    
    cout << "Press any key to continue" << endl;
    cin.ignore(numeric_limits<streamsize>::max(),'\n');
    return 0;
}

I would like to add the line
if(key == 'x') break; but it doesn't work. I can only get it to work by using system("PAUSE") instead of cin.ignore()
Last edited on
closed account (zb0S216C)
Krahl wrote:
if(key == 'x') break;

Here, you're comparing x as an integer, not a character. Since key is of type int, x's numerical equivalent is compared instead, which could be 126, for example.

Wazzak
Last edited on
Thanks for the reply framework

I have tried if(key == (int)'x') break; and it still didn't work. I think you mean 120 on the ASCII right? i've also tried key == 120.

To clarify I'm trying to write a short code where if an integer is entered then carry on looping but if x is entered quit it would be very helpful.
Last edited on
Try making a do while loop:
1
2
3
4
do
{
// binary search stuff goes here
} while(key != 'x');
Because key is an integer, when you enter 'x' the stream extraction fails leaving the character 'x' in the buffer and key unchanged. It will then do a loop iteration and then cin again. 'x' is still in the stream and the extraction will still fail so we enter an infinite loop.

Options include

1) Use getline to read a string, then istringstream to extract the key or the 'x' from it
2) Use a number like -1 instead of the character 'x' to terminate
3) Break if the last operation failed - if cin.fail() == true

kev82 accurately described the issue. 2) wasn't an option since the binary search might actually involve any integer, 1) I thought of using getline but it hadn't been covered in the literature yet, 3) is new to me.

Edit: I've tried 3) of kev's post but the x still lingers in the buffer and I can't let the user see the lovely "Press any key to continue" message.

Edit2: I've also tried ascii's suggestion to no avail. Also tried 1) in kev's post but I think I'll need to add in another function to convert the string to an integer.

Seems the only simple way I can get it to work is if I use system("PAUSE");
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
#include <iostream>
using namespace std;
int main(){

    int array[] = {1,4,5,6,9,14,21,23,28,31,35,42,46,50,53,57,62,63,65,74,79,89,95};
    int elts = sizeof(array)/sizeof(int);
    //PrintArray(array,elts);
    char key; // char


    for(;;){
            cout << "Enter a search key (or 'x' to Exit): ";
            cin  >> key;
            if(key == 'x') break;
            //int position = BinSearch(array,elts,key);
            //if(position != -1){
            //       cout << key << " is in position " << position << endl;
            //}
            //else cout << key << " is not in the list" << endl;
    }



    cout << "Lol that was easy" << endl;
    //cin.ignore(numeric_limits<streamsize>::max(),'\n');
    return 0;
}


ohhh... I didn't read the entire post sorry...

Anyway I think you can't do it that way, int are made to store just numbers, there is no way you could use int to show characters, well that is what I think. You need to use the other tools.
Last edited on
I would have thought my 3rd suggestion would work. Not tested though.

1
2
cin >> key;
if(cin.fail()) break;
Last edited on
Topic archived. No new replies allowed.