Results 1 to 6 of 6
  1. #1
    Nissen1502 is back's Avatar
    Join Date
    Sep 2010
    Gender
    male
    Posts
    134
    Reputation
    10
    Thanks
    15

    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

  2. #2
    atom0s's Avatar
    Join Date
    May 2013
    Gender
    male
    Posts
    403
    Reputation
    139
    Thanks
    104
    You are missing the parenthesis.
    if (carrots2 == 8)
    - Gone; this is another shit forum with children as administrators. Not worth contributing to.

  3. The Following User Says Thank You to atom0s For This Useful Post:

    Nissen1502 is back (07-16-2013)

  4. #3
    Nissen1502 is back's Avatar
    Join Date
    Sep 2010
    Gender
    male
    Posts
    134
    Reputation
    10
    Thanks
    15
    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;
    }

  5. #4
    Mayion's Avatar
    Join Date
    Oct 2012
    Gender
    male
    Location
    Bed
    Posts
    13,504
    Reputation
    4018
    Thanks
    8,372
    My Mood
    Twisted
    Yea, Nice Programming
    I do not use any type of messenger outside of MPGH.
    Inactive but you can reach me through VM/PM.










     

    Donator - 30 August 2013
    Battlefield Minion - 26 October 2013

    Blackshot Minion - 14 January 2014/16 September 2014
    Minecraft Minion - 7 February 2014/16 September 2014
    WarRock Minion - 23 February 2014
    League of Legends Minion - 21 March 2014

    Minion+ - 15 May 2014
    Other Semi-Popular First Person Shooter Minion - 8 August 2014
    CrossFire Minion - 23 October 2014
    Programming Section Minion - 13 November 2014
    Marketplace Minion - 7 December 2014

    Official Middleman - 7 December 2014 - 27 June 2015
    Moderator - 29 December 2014
    Project Blackout Minion - 10 January 2015
    News Force Interviewer - January 2015
    Steam Games Minion - 21 March 2015
    Dragon Nest Minion - 31 March 2015
    Publicist - April 2015 - 21 September 2015
    Global Moderator - 25 August 2015
    Super User - 13 August 2016



  6. The Following User Says Thank You to Mayion For This Useful Post:

    Nissen1502 is back (07-16-2013)

  7. #5
    shampos's Avatar
    Join Date
    Apr 2013
    Gender
    male
    Posts
    6
    Reputation
    10
    Thanks
    2

    Cool you forgot something.

    you forgot to put parenthesis in line 18
    or change the if (carrots2 == 8) to ->> if (carrots2 == carrots)

  8. The Following User Says Thank You to shampos For This Useful Post:

    Nissen1502 is back (07-17-2013)

  9. #6
    Gab's Avatar
    Join Date
    Jul 2010
    Gender
    male
    Posts
    6,716
    Reputation
    1755
    Thanks
    1,543
    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.

  10. The Following User Says Thank You to Gab For This Useful Post:

    Nissen1502 is back (07-24-2013)

Similar Threads

  1. Replies: 1
    Last Post: 12-27-2012, 11:13 PM
  2. [Solved] CROSSFIRE ERROR: Send Error Report? Anyone else get this in th lobby?
    By WolfenXD in forum CrossFire Help
    Replies: 3
    Last Post: 12-05-2011, 09:04 AM
  3. Why am i getting these Link errors?
    By Auxilium in forum C++/C Programming
    Replies: 5
    Last Post: 08-07-2010, 08:59 AM
  4. Why i keep getting this error?
    By muumimamma in forum Combat Arms EU Help
    Replies: 10
    Last Post: 07-03-2010, 10:19 PM
  5. D3D base error, Why im i getting this?
    By HazXoD3D in forum C++/C Programming
    Replies: 5
    Last Post: 10-15-2009, 11:05 AM