I am currently taking C++, and I just want to know why it says,"Illegal Else without if," and I would like to know how to fix it so I will not encounter this problem again. I would like to know where to put the brackets, "{ "

Here is the Code:

<snipped>
//CS 1361, Spring 2008
//Lab 21

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;

const string ID = "<snipped> - CS 1361, Spring 2008 - Lab 21";


int main()
{
  int x,
	  y,
	  z,
	  u,
	  v,
	  w,
	  t,
	  d,
	  c,
	  year;

  ifstream fin;
  ofstream fout;
  

  cout << "running Lab 01 . . . " << endl << endl;
  fin.open("lab21.dat");
  fout.open("<snipped> - CS 1361, Spring 2008 - Lab 21.out");

  	if (!fin)                       //p. 175 Testing the State of an I/O stream
  {                                             
	cout<<"Can't open the input file." << endl << endl;
	return 1;                                 
  }
  fin >> year;

  // Calculate
  x = ((year) / (100 + 1));
  y = ((3 * x) / (4 - 12));
  z = ((year % 19) + 1);
  u = (((8 * x) + 5) / ((25 -5) - y));
  v = ((5 * year) / ((4 - y) - 10));
  w = ((11 * z) + (20 + u));
  t = (w % 30);
  fout << ID << endl;
 
  if (t == 25)
  {
	  if (z > 11)

	  else 
		t = (t + 1);
  }
  


  if (t == 24)
	  else 
	  t = (t + 1)
  
  d = (44 - t)
  


  if (d < 21)
  else 
	  d = (d + 30)
  
  

  c = (d + v);
  d = ((d + 7) - (c % 7));
  



  if (d > 31)
	  d = (d - 31));
	  fout << d << endl;
  else 
	  fout << d << endl;
      fout << year << endl;

  return 0;
}

Welcome to Daniweb!

Please use [ code ][ /code ] tags (with the spaces next to the square brackets) when posting code to this board to maintain the indentation you (hopefully) use when writing your code.

The error message probably means that you should have a statement of some sort between each if and each else.

So for example

if (d < 21)
{
   //do something
}
else 
  d = (d + 30)

instead of 

if (d < 21)
else 
  d = (d + 30)

In Lerner's example, you can see that the IF code is wrapped in brackets but he didn't do the same for the ELSE code. That's because when there is only one line, brackets are implied and therefore not needed. (Although I would recommend them, especially when starting out.)

That's because when there is only one line, brackets are implied and therefore not needed. (Although I would recommend them, especially when starting out.)

Completely agree! Always use brackets within an if statement

Welcome to Daniweb!

Yeah, what he said! :)

The error message probably means that you should have a statement of some sort between each if and each else.

No it doesn't. It is legal to have nothing in the TRUE position of an if .

Follow cscgal's advice and you will automatically solve your problem. And you will probably figure out what it was, too.

> The error message probably means that you should have a statement of some sort between each if and each else.

>> No it doesn't. It is legal to have nothing in the TRUE position of an if

yes, it does. having nothing is not legal C++. however, the statement may be
a null statement ( just a ; ) or an empty compound statement ( {} ).

>> No it doesn't. It is legal to have nothing in the TRUE position of an if
yes, it does. having nothing is not legal C++. however, the statement may be
a null statement ( just a ; ) or an empty compound statement ( {} ).

I consider just a ; to be a nothing, but you're right. I did gloss over that fact you need it. Sorry 'bout that. Thanks, Vijayan

I solved the problem, but both of you helped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.