Click here to Skip to main content
Sign Up to vote bad
good
See more: C++Homework
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"
whats' this error,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,?

 
#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 7 Feb '13 - 1:11
Edited 7 Feb '13 - 1:16

Comments
SoMad - 7 Feb '13 - 7:19
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 }
  Permalink  
Comments
Usman Hunjra - 7 Feb '13 - 7:16
hahaha .. LOL Thank You BrO .. !!

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 8,376
1 OriginalGriff 6,571
2 CPallini 3,533
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid