New to C++, have an assignment

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.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;

int main()
{

    const int 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;
}
line 44 if (found = 1) is incorrect.

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.
Last edited on
kigoral,

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.

Hope that helps,

Andy
Line 44 is incorrect as it is an assignment statement. You want an equality statement, which is two equal signs.
Last edited on
Kigoral,

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.

Sorry for the missdirect,

Andy
closed account (48T7M4Gy)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// http://www.cplusplus.com/forum/beginner/192742/

#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
    const int 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;
}
Last edited on
Thanks for all of the feedback everyone, definitely got it to work!
Topic archived. No new replies allowed.