Why do i get error C2181: illegal else without matching if?

Posts 1–6 of 6 · Page 1 of 1
Why do i get error C2181: illegal else without matching if?
Here's the code:
Code:
#include <iostream>

int main()
{
	using namespace std;

	int carrots;

	cout << "How many carrots do you have?" << endl;
	cin >> carrots;		//User input
	cin.get();
	cout << "Here are two more. " << endl;
	carrots = carrots + 2;
	cout << "How many carrots do you have now?" << endl;
	int carrots2;
	cin >> carrots2;
	cin.get();

	if carrots2 == 8
		cout << "That's correct" << endl;
	else
		cout << "That's incorrect" << endl;
	cin.get();
	return 0;
}
i get these errors:
error C2061: syntax error : identifier 'carrots2'
error C2181: illegal else without matching if
You are missing the parenthesis.
if (carrots2 == 8)
Thanks, also fixed my code because it was incorrect, here it is (im proud because it was the first code i made on my own and not copied from the book im reading)
Code:
#include <iostream>

int main()
{
	using namespace std;

	int carrots;

	cout << "How many carrots do you have?" << endl;
	cin >> carrots;		//User input
	cin.get();
	cout << "Here are two more. " << endl;
	carrots = carrots + 2;
	cout << "How many carrots do you have now?" << endl;
	int carrots2;
	cin >> carrots2;
	cin.get();

	if (carrots2 == carrots)
		cout << "That's correct" << endl;
	else
		cout << "That's incorrect" << endl;
	cin.get();
	return 0;
}
you forgot something.
you forgot to put parenthesis in line 18
or change the if (carrots2 == 8) to ->> if (carrots2 == carrots)
Here's a little tip, try putting "using namespace std;" outside of the main function, because when you'll use more than one function, it'll be more efficient to declare it once than in every single function.
Posts 1–6 of 6 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?