error on using if else

closed account (4jzvC542)
I was given following assignment:
Write a program that will ask user to enter a char.
Check if it is alphabatic or not, if yes than check is it
uppercase or lowercase....


simple it is i tried to do using inbuilt functions of <ctype.h>
here is my code:
-------------------------------------------------------
#include <iostream>
#include <ctype.h>
#include <stdio.h>
void main()
{
char A;
bool x, y;
bool True, False;
A = getchar();
x = isdigit(A);
if(x == False)
y = isalpha(A);
if (y == True)
std::cout << "Uppercase\n";
else
std::cout << "Lowercase\n";
else
std::cout << "Not char entered.....\n";
}

on compiling :
------------------------------------------------------
--------------------Configuration: 03 - Win32 Debug--------------------
Compiling...
03.cpp
F:\00 C++Study 13\03 Home-Work Exercise\chapter10 Functions\terminal\03 upper lower\03.cpp(10) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
F:\00 C++Study 13\03 Home-Work Exercise\chapter10 Functions\terminal\03 upper lower\03.cpp(12) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
F:\00 C++Study 13\03 Home-Work Exercise\chapter10 Functions\terminal\03 upper lower\03.cpp(17) : error C2181: illegal else without matching if
Error executing cl.exe.

03.obj - 1 error(s), 2 warning(s)
---------------------------------------------------

can anyone tell me where is error and what does:
illegal else without matching if
means......

regards,
Parjanya
closed account (Dy7SLyTq)
you can only have one else per if block. remove the last else
closed account (4jzvC542)
@DTSCode

er....than my code is destroyed...
can you suggert me how can i recode it....
i dont want to use goto.....
thanks
parjanya
Also it is INT main not void. Plus why the heck do you have two bools named "True" and "False." That is like naming a string "String" or a char "Char" , "Letter" , "Character" or an int "Int" , "Integer" , "Num" , "Number" which are very vague and do not say what they are doing.
illegal else without matching if means that the compiler sees an else statement and can't find an if statement above it. This would trigger the error too:
1
2
3
4
5
void main()
{
else
    cout<<"Hello World!";
}


Try adding whitespace (tabs or spaces) to your code-It will make the problem much more obvious (which is one reason programmers do it). If that isn't enough to make it click, you can find the solution by reading over this: http://www.cplusplus.com/doc/tutorial/control/
I'm guessing the second else belongs with the first if statement, just add some braces and it should be ok.

1
2
3
4
5
6
7
8
9
10
11
12
if(!x)
{
  if (y)
  { // the set in this if statement is optional since it's only 1 line
  }
  else // this belongs with the y statement
  {
  }
}
else // this belongs with the x statement
{
}
Last edited on
closed account (4jzvC542)
@munudude03
i am using ms visual studio 2006 std editon

if i try to add these braces then there is compilation error
dont know why...

if ( x == y)
{
cout << x;
}
else
{
if (y == x)
{
cout << y;
}
}
That code compiles fine on mine (I use Code::Blocks) when I set x and y as ints equal to 0. I just checked the site I learned C++ from and it mentions this:

1
2
3
Important note to Visual Studio users: Visual studio programs should ALWAYS begin with the following line:

#include "stdafx.h" 
Last edited on
Topic archived. No new replies allowed.