Need some insight on why my program isn't running like it should.

I am just taking my second C++ course and I am having trouble with getting this to work. I enter a number but when I do it doesn't loop or do the validation correctly. I just need it to find the number in the array and post the element in which it found it in. If it doesn't find the number I need it to loop and have the user input the number again so it finds the correct element.

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
#include <iostream>
using namespace std;

int main()
{
	int myarr[5] = { 54, 3, 5, 7, 8 };
	int ans;

	for (int x = 0; x < 5; x++)
	{
		cout << "Please enter the number: ";
		cin >> ans;
		cout << "\n\n";

		if (myarr[x] == ans)
		{
			cout << "The number has been found " << myarr[x] << endl;
			break;
		}
		else
		{
			cout << "The number has not been found!\n";
			x = 0;
			continue;
		}
	}

	system("pause");
	return 0;
}
That's because of your break statement. It breaks out of the for-loop. Simply remove it.

Edit: continue; Doesn't do anything here either.
Last edited on
I removed the break statement and the continue as well but it just takes the users input and displays nothing. It actually just remains on the screen with the cursor blinking.
That's weird. It works perfectly fine for me. See if this works -

full program:

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 myarr[5] = { 54, 3, 5, 7, 8 };
	int ans;

	for (int x = 0; x < 5; x++)
	{
		cout << "Please enter the number: ";
		cin >> ans;
		cout << "\n\n";

		if (myarr[x] == ans)
		{
			cout << "The number has been found " << myarr[x] << endl;
		}
		else
		{
			cout << "The number has not been found!\n";
			x = 0;
		}
	}
	system("pause");
	return 0;
}
The loop worked correctly, but now it is saying that when i put in any other number than 3, the number has not been found. It should be finding 54, 5, 7, & 8 if entered.
How silly of me to not have noticed this before. Your if statement is a bit weird. It will only work if you input the numbers in the same order that the numbers are in the array.

x starts with the value 0.
So the if statement will only search for myarr[0], becuase you're using "x". If you enter anything but 54 it will not work, once you enter 54, x gets the value 1. Then only 3 works, because myarr[1] is 3, and if you don't enter 3, x will reset, and you'll go back to only being able to enter 54.

Does that make sense, is this how you want the program to act or did you have something else in mind?
What I think its doing is checking the amount of numbers put it and when it hits element 3 or 5 it will say that any number entered is found within the array. Which is really weird because it shouldn't be doing that.
I had in mind that the number entered in by the user will go into the array to check to see if it matches the numbers in the array. If it doesnt it will say it wasnt found and will ask for user input again. If it does find the number it will display that it has found the number.
You should place the input part (line 11 to 13) outside of the for-loop.
And don't set x = 0; (line 22 in TarikNeaj's Code). If you do so, the loop will not iterate over the array.
I made a little program for you @Chrisjohn01. If there is anything you don't understand please ask =)

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
#include <iostream>
using namespace std;

//This is a function - http://www.cplusplus.com/doc/tutorial/functions/
int findNumber(int arr[], int size, int numberEntered)
{
	for (int i = 0; i < size; i++)
	{
		if (arr[i] == numberEntered)
		{
			return arr[i];
		}
	}
	return -1;
}

int main()
{
	int myarr[5] = { 54, 3, 5, 7, 8 };
	int ans = 0;

	while (ans != -1)
	{
		cout << "Please enter the number: ";
		cin >> ans;
		cout << "\n\n";

		int found = findNumber(myarr, 5, ans);

		if (found == -1)
		{
			cout << "The number has not been found!\n";
		}
		else
		{
			cout << "The number has been found " << found << endl;
		}
	}
	system("pause");
	return 0;
}
So would this function you made be a binary search or a linear search? Also if I wanted to take what the user enters and let them enter multiple numbers, then add the sum off all the numbers and display it, would that work the same for this array?
It's definitely a linear search -
http://stackoverflow.com/questions/700241/what-is-the-difference-between-linear-search-and-binary-search


Also if I wanted to take what the user enters and let them enter multiple numbers, then add the sum off all the numbers and display it, would that work the same for this array?

Do you simply mean add all the inputs the user enters in the program I provided and then sum it up?
Topic archived. No new replies allowed.