error:else without a previous if

Hello...I'm just starting with c++

Got an exercise as such: Modify the program so that a message "Not found is printed if the value is not in the array:
int main()
{
int N=9;
int A[N]={2,-3,5,7,-3,32,-5,0,7};//declaring the array
int sValue,position=-1;
cout<<"Input number to search: ";
cin>>sValue;
for(int index=0;index<N;index++)
if(A[index]==sValue)position=index;
cout<<"Value found at position "<<position;
}
I have modified it as such but receiving error:else without a previous if

#include <iostream>

using namespace std;

int main()
{
int N=9;
int A[N]={2,-3,5,7,-3,32,-5,0,7};//declaring the array
int sValue,position=-1;
cout<<"Input number to search: ";
cin>>sValue;
for(int index=0;index<N;index++)
if(A[index]==sValue)position=index;
cout<<"Value found at position "<<position;
else
cout<<"Not Found!";
}









Please use coding tags. Wrap all code inside [`code] code here [`/code] (` not to be included)

Here's your first code with formatting:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	int N = 9;
	int A[N] = { 2,-3,5,7,-3,32,-5,0,7 };//declaring the array
	int sValue, position = -1;

	cout << "Input number to search: ";
	cin >> sValue;

	for (int index = 0; index < N; index++) {
		if (A[index] == sValue) {
			position = index;
		}
	}

	cout << "Value found at position " << position;
}


Small note: declaring arrays with dynamic amount of indexes is supported by only few compilers, it's not part of the standard, and it's bad practice to do so.

ie, int A[N] = { 2,-3,5,7,-3,32,-5,0,7 }; should have been int A[] = { 2,-3,5,7,-3,32,-5,0,7 };

Here's your edited version with formatting
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{
	int N = 9;
	int A[N] = { 2,-3,5,7,-3,32,-5,0,7 };//declaring the array
	int sValue, position = -1;

	cout << "Input number to search: ";
	cin >> sValue;

	for (int index = 0; index < N; index++) {
		if (A[index] == sValue) {
			position = index;
		}
	}

	cout << "Value found at position " << position;
	else {
	cout << "Not Found!";
	}
}


Do you see what's wrong? The if statement is inside the for-loop. You can't write an else statement outside the for-loop, they won't be connected. An else must immediately follow an if.

Here's a hint: You need to change cout<<"Value found at position "<<position; in the original code by adding an if statement to verify that the value of position isn't -1 then print that same statement if true, otherwise the value was never found so use an else statement to print that it was never found.


Last edited on
Thanks a lot...But the error is still received
Let me be blunt.

> for(int index=0;index<N;index++)
This needs braces.

> if(A[index]==sValue)position=index;
This needs braces as well.

Figure out where the braces need to go in your program.



But braces as follows:
int main()
{
int N=9;
int A[N]={2,-3,5,7,-3,32,-5,0,7};//declaring the array
int sValue,position=-1;
cout<<"Input number to search: ";
cin>>sValue;
for(int index=0;index<N;index++)

{
if(A[index]==sValue)
{
position=index;}
}

cout<<"Value found at position "<<position;

else
{
cout<<"Not Found!";
}
}
No No No. I don't think you understand how the finding-in-array is happening.

1
2
3
for (int index = 0; index < N; index++)
	if (A[index] == sValue)
		position = index;


You should not change the for-loop.

The for loop iterates for N times. Each time it checks ever element of the array to see whether it matches the given element. If it's found then it change position's value to 'index' which holds the current iteration of the for-loop.

If the element is not found in the array, then position's value is never changed and it remains to be -1.

Note that the given element can occur more than once, in that case the last occurring element's position is stored. If you don't want then this you need to break out of the for-loop.

Everything is being done for you by the for-loop. After the for-loop there are two situations for the position.

Either position is -1, because the element was never found. Or it is the position in the array where the element was found.

So AFTER the for-loop, write a statement like this:
If the value of position is -1, print found at << position, otherwise print 'not found!'
Thanks...modify the program as follows and it worked!

#include <iostream>

using namespace std;

int main()
{
int N=9;
int A[N]={2,-3,5,7,-3,32,-5,0,7};//declaring the array
int sValue,position=-1;
cout<<"Input number to search: ";
cin>>sValue;
for(int index=0;index<N;index++)
{
if(A[index]==sValue)
{position=index;}
}
if (position!=-1)
cout<<"Value found at position "<<position;
else
{cout<<"Value Not Found!";
}
}
Topic archived. No new replies allowed.