Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Well I'm writing a code that will get a key from user and find that key, weather it is present in the array or not. . .
I'm using VS - 2012, I've never encourtered an error like this ..
when i build the project, it prompts an error

"error C2181: illegal else without matching if"<br />
whats' this error,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,?


C++
#include "stdafx.h"
#include <iostream>

using namespace std;

void fill(int a[], int length)
{
	for (int i = 0; i < length; i++)
	{
		cout << "Element #" << i+1 << endl;
		cin >> a[i];
	}
	 
}

void print(int a[], int length)
{
	cout << "A R R A Y Of 10 Elements .." << endl;
	for (int j = 0; j < length; j++)
	{
		cout << a[j] << " | ";
	}
}
void find_key(int a[], int length)
	{
		int key;
		cout << "Find The Number: ";
		cin >> key;
		bool found = false;
		for (int i = 0; i < length; i++)
		{
			if (key == a[i])
			{
				found = true;
			}
			if (found)
			{
				cout << "K E Y Present" << endl;
			else
			{
				cout << "K E Y Absent" << endl;
			
			}
		}
	}
}

int main()
{
	const int length = 10;
	int label[length];
	fill(label, length);
	print(label, length);
	find_key(label, length);
	system("pause");
	return 0;
}
Posted
Updated 7-Feb-13 1:16am
v2
Comments
SoMad 7-Feb-13 7:19am    
Matthew has the correct answer in Solution 1. I just wanted to add that it will probably not be the last time you encounter an error like this ;-)

Soren Madsen

1 solution

You missed a bracket here:
if (found)
			{
				cout << "K E Y Present" << endl;
>>>>
			else

"You broke the law! Go directly to jail, do no collect £200" as it says on the Monopoly board.
Or you could just put in the }
 
Share this answer
 
Comments
Usman Hunjra 7-Feb-13 7:16am    
hahaha .. LOL
Thank You BrO .. !!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900