Sulligy
Sulligy

Reputation: 21

Trying to output all index values if they're found

I have a homework assignment that I'm pretty much done with except for one part. This part of my homework asks me to generate an array with 10 random values that are in between 15 and 20, then ask the user to input an integer and search the array for that integer. If the integer (and any duplicates of the integer) is found, the index value(s) of the integer should be outputted. If the integer is not found, the text "Integer was not found" should be outputted instead.

OK, so I'm done with literally everything except the part where I'm supposed to output whether or not the integer was found.

My block of code looks like this:

..............................................................................

search=0;
cout<<endl<<"Enter an integer to search for in the array: "<<endl;
cin>>search;
cout<<endl;

for(int i=0;i<10;i++)
{
     if(search == array[i])
     {
     cout<<"Integer "<<search<<" was found at location "<<i+1<<"."<<endl;
     }

     else
     {
     cout<<"Integer "<<search<<" was not found."<<endl;
     }
}        
break;

..............................................................................

The way I have this setup doesn't meet the requirements for the assignment.

Say that my randomly generated array is this string of numbers -

[16] [18] [16] [20] [19] [15] [15] [20] [19] [16]

If I input "16" as the integer I want to search for, the output will be-

"Integer 16 was found at location 1"

"Integer 16 was not found"

"Integer 16 was found at location 3"

"Integer 16 was not found"

"Integer 16 was not found"

"Integer 16 was not found"

"Integer 16 was not found"

"Integer 16 was not found"

"Integer 16 was not found"

"Integer 16 was not found"

"Integer 16 was found at location 10"

If I input an integer that isn't in the array, the output will repeat 10 times.

I want to change this so that if I input "16," I just get-

"Integer 16 was found at location 1"

"Integer 16 was found at location 3"

"Integer 16 was found at location 10"

And if I input an integer that isn't on the array, I just want-

"Integer (x) was not found"

Sorry if this is really simple, this is my first StackOverflow question and my first programming class, and the professor "teaches" entirely with cookie cutter youtube videos. I haven't even heard his voice yet lmao.

Thanks in advance for any help. If you need me to provide more info, let me know.

Upvotes: 2

Views: 91

Answers (1)

Aditya Ishan
Aditya Ishan

Reputation: 507

No need for that "break" statement. What you want to do is check if the number is there in the randomly generated array. If it is available then you need to turn on the "found_flag". When the "for loop" ends and still the "found_flag" is off that means the number you are searching for is not there.

#include <iostream>
using namespace std;

int main(){

  int search;
  bool found_flag = false;
  cout<<endl<<"Enter an integer to search for in the array: "<<endl;
  cin>>search;
  cout<<endl;

  for(int i=0;i<10;i++)
  { 
    if(search == array[i])
    {
     cout<<"Integer "<<search<<" was found at location "<<i+1<<"."<<endl;
     found_flag = true;
    }
 }

  if(!found_flag)  cout<<"Integer "<<search<<" was not found."<<endl;

 return 0;
}   

Upvotes: 1

Related Questions