I have an assignment that requires user input of numbers into an array. Then I need to be able to search that array for a specific number, and output if the number is found or not. I tried searching the forum, but I am having a hard time understanding how to adapt what I am reading to my code. Any help is appreciated, I am probably doing this all wrong.
#include <iostream>
#include <conio.h>
#include <stdio.h>
usingnamespace std;
int main()
{
constint maxList = 50; //max number of items
int n; // max number of data elements in list
int listInput[maxList]; //array for input
int i; //holds value to keep number entries looping
int target; //number to search for
//get input for maximum and list
cout << "What is the number of numbers in the list?";
cout << " (the maximum is 50)" << endl;
cin >> n;
if (n < maxList)
{
i = 0;
cout << "Enter the first number in the list" << endl;
cin >> listInput[i];
while (i < n - 1)
{
i = i + 1;
cout << "Enter next number: ";
cin >> listInput[i];
}
int found = 0;
cout << "What is the number you want to find in the list?" << endl;
cin >> target;
for (int j = 0; j < i; j = j + 1)
{
if (target == listInput[j])
{
found = 1;
break;
}
if (found = 1)
{
cout << "Number " << target << " found" << endl;
break;
}
else
{
cout << "Number " << target << " not found" << endl;
}
}
}
else
{
cout << "Max list was 50" << endl;
}
cout << "Press any key to terminate program..." << endl;
_getch();
return 0;
}
maybe remove conio.h and replace line 63 with cin.ignore()
1 2 3 4 5
if (target == listInput[j])
{
found = 1;
break;
}
when the above statement is executed, you are removed from the loop. You are waiting for found to equal 1, but you never go back through the loop again for this to happen.
I will disagree with privateRyan in that line 44 if (found = 1) is correct it is just never reached. It is the preceding if block that is the problem. when the statement is true you set found = 1 then you break out of the for loop and never stet for if (found = 1) so you will never reach output line for found.
Try commenting out the break on line 42 and see what happens. By the time you try that I will have tried it for myself and will have a better idea.
I stand corrected line 44 is a problem, but not the only problem. When I commented out the first break statement and changed line 44 the program appears to work properly.
Sometimes it is the simplest things that are the hardest to find.
// http://www.cplusplus.com/forum/beginner/192742/
#include <iostream>
#include <stdio.h>
usingnamespace std;
int main()
{
constint maxList = 50; //max number of items
int n; // max number of data elements in list
int listInput[maxList]; //array for input
int i; //holds value to keep number entries looping
int target; //number to search for
//get input for maximum and list
cout << "What is the number of numbers in the list?";
cout << " (the maximum is 50)" << endl;
cin >> n;
if (n < maxList)
{
i = 0;
cout << "Enter the first number in the list" << endl;
cin >> listInput[i];
while (i < n - 1)
{
i = i + 1;
cout << "Enter next number: ";
cin >> listInput[i];
}
int found = 0;
cout << "What is the number you want to find in the list?" << endl;
cin >> target;
for (int j = 0; j < i; j = j + 1)
{
if (target == listInput[j])
{
found = 1;
break;
}
}
if (found == 1)
cout << "Number " << target << " found" << endl;
else
cout << "Number " << target << " not found" << endl;
}
else
cout << "Max list was 50" << endl;
return 0;
}