Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 2:11am
ctype.h1.7K
Edited 7-Feb-13 2:16am
v2
Comments
SoMad at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.150326.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100