How to find number in array

My program is displaying number does not exist after each input. I need help to fix my if(TheNum!=cnt) because I know it's incorrect, but I don't know what to change it to.

Find index of a number: Displays the index of a number if it exists otherwise a message saying that the number is not found.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  else if ( input == 3){
        int TheNum =0;
        int i =0;
        cout << "Number to search for: ";
        cin >> TheNum;
        
        for(i = 0; i < cnt; i++)
        {
            if(array[i] == TheNum){
                cout <<  TheNum << " is at index " << i << endl;
                
                for( int i = 0; i < cnt ; ++i )
                    cout << array[i] << ' ' ;
                cout << '\n' ;
            }
            
        } if(TheNum!=cnt){
            cout << TheNum << " does not exist" << endl;} }
Hello cash,

What you have will mostly work.

The for loop and first if statement is fine except that when the if statement is true you need to break out of the for loop and not continue until the end.

The second if statement is comparing the wrong variables. So if "TheNum" just happens to be the same value as cnt chances are the this if statement will always be true. If "i" is equal "cnt" that means that you have transversed the entire usable array and found nothing other wise it should be less than "cnt" making the condition false.

Hope that helps,

Andy
Should it be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
else if ( input == 3){
        int TheNum =0;
        int i =0;
        cout << "Number to search for: ";
        cin >> TheNum;
        
        for(i = 0; i < cnt; i++)
        {
            if(array[i] == TheNum){
                cout <<  TheNum << " is at index " << i << endl;
                
                for( int i = 0; i < cnt ; ++i )
                    cout << array[i] << ' ' ;
                cout << '\n' ;
            }
            
        }
  

        if(cnt>i){
            cout << TheNum << " does not exist" << endl;} }
Hello cash,

Almost.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
else if ( input == 3){
        int TheNum =0;
        int i =0;
        cout << "Number to search for: ";
        cin >> TheNum;
        
        for(i = 0; i < cnt; i++)
        {
            if(array[i] == TheNum)
            {
                cout <<  TheNum << " is at index " << i << endl;
                
                for( int i = 0; i < cnt ; ++i )
                    cout << array[i] << ' ' ;
                cout << '\n' ;
                break;  // <--- Means that you found your number and are finished with the loop.
            }
        }
  
        if (cnt > i )  // <--- Right variables Wrong symbol ">". 
            cout << TheNum << " does not exist" << endl;


What would the value of "i" be if you check each usable element of the array and find nothing?

FYI the code is much easier to read when the opening and closing {} are in the same column. Which is not to say that there is anything wrong with your way.

Hope that helps,

Andy
Last edited on
Thank you, for all the help. I finally completed my menu!!
You are welcome.

Andy
Topic archived. No new replies allowed.