6 Replies - 346 Views - Last Post: 03 June 2012 - 01:11 PM Rate Topic: -----

#1 Red Prince  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 03-June 12

if statement is missing )

Posted 03 June 2012 - 08:41 AM

hello, I am just experiencing a problem while compiling this program. Which is this "if statement is missing )" while compiling the following program the Borlad Turboo C++ v5.02. Can anybody tell me how to get rid from this:


#include <iostream.h>
#include <conio.h>

void main()
{
char ch;
clrscr();

cout<<"Enter a character to see it's ASCII Value: ";
cin>>ch;
if(ch==cin.fail())
{
cout<<"You are not entered a character";
}
else
	{
    if(ch=='a' || ch=='b' || ch=='c' || ch=='d' || ch=='e' || ch=='f' || ch=='g'
       ch=='h' || ch=='i' || ch=='j' || ch=='k' || ch=='l' || ch=='m' || ch=='n'
       ch=='o' || ch=='p' || ch=='q' || ch=='r' || ch=='s' || ch=='t' || ch=='u'
       ch=='v' || ch=='w' || ch=='x' || ch=='y' || ch=='z')
       cout<<"You Enter a character in lower case";
       }
    	cout<<"You enter a caracter in Upper case";

getch();
}

This post has been edited by Salem_c: 03 June 2012 - 08:47 AM
Reason for edit:: Added [code][/code] tags - learn to use them yourself


Is This A Good Question/Topic? 0
  • +

Replies To: if statement is missing )

#2 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 269
  • View blog
  • Posts: 3,825
  • Joined: 08-January 12

Re: if statement is missing )

Posted 03 June 2012 - 08:54 AM

In your if statement, after
ch=='g'

You have not written || before the next condition. That is causing the error. Besides that, use proper indentation. Also, Turbo C++ is non-standard so stop using it. It has not been developed for like 10 years. Besides, you can simplify your condition to
if(ch>='a' && a<='z')
   cout << "You enter lower case number." ;

Note that this approach makes use of ASCII values of characters. And one last thing, since Salem_c is reading this, let me tell you this before he tells you - NEVER use void main(). Always use int main(), since void main() is not standard.

This post has been edited by aresh: 03 June 2012 - 08:55 AM

Was This Post Helpful? 1
  • +
  • -

#3 bobmax  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 20
  • Joined: 29-July 11

Re: if statement is missing )

Posted 03 June 2012 - 09:53 AM

there were few problems as aresh said,so i cleaned up the code a little (and its working):

#include <iostream>
#include <conio.h>

int main();
{
char ch;

cout<<"Enter a character to see it's ASCII Value: ";
cin>>ch;
if(ch==cin.fail())
{
cout<<"You are not entered a character";
}
else 
// Part where the value is checked
	{
    if(|| ch=='a' || ch=='b' || ch=='c' || ch=='d' || ch=='e' || ch=='f' || ch=='g'
      || ch=='h' || ch=='i' || ch=='j' || ch=='k' || ch=='l' || ch=='m' || ch=='n'
      || ch=='o' || ch=='p' || ch=='q' || ch=='r' || ch=='s' || ch=='t' || ch=='u'
      || ch=='v' || ch=='w' || ch=='x' || ch=='y' || ch=='z')
      
      if(ch>='a' && a<='z')
   cout << "You enter lower case number." ;
       }
    	cout<<"You enter a caracter in Upper case";

getch();
}




Quote

Also, Turbo C++ is non-standard so stop using it. It has not been developed for like 10 years.


yes,he is right,its very very very old....

you can download one of the latest from here:

DEV C++

microsoft visual c++ 2010 express (Favorite/ Recommended)
Was This Post Helpful? 0
  • +
  • -

#4 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 269
  • View blog
  • Posts: 3,825
  • Joined: 08-January 12

Re: if statement is missing )

Posted 03 June 2012 - 10:01 AM

Well, your code is also not perfect.
#include <iostream>
#include <conio.h>

int main();
{
   char ch;
   cout<<"Enter a character to see it's ASCII Value: ";
   cin>>ch;
   if(ch==cin.fail())
   {
      cout<<"You are not entered a character";
   }
   else
   {
      if(ch>='a' && a<='z')
         cout << "You enter lower case number." ;
      else
    	 cout<<"You enter a caracter in Upper case"; // Notice I have added an else
   }
   getch();
}


As you can see, I added an else. Otherwise, it would always print the uppercase message. And, although I myself use Dev-Cpp sometimes if I am too lazy to open VC++, Dev-Cpp is also not exactly updated. It's last release was in 2005, and that was also a beta.
Was This Post Helpful? 1
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon

Reputation: 3048
  • View blog
  • Posts: 9,283
  • Joined: 25-December 09

Re: if statement is missing )

Posted 03 June 2012 - 10:33 AM

Also in the following code:
   cin>>ch;
   if(ch==cin.fail())

The variable ch will not be equal to cin.fail(). When the stream fails nothing is retrieved so ch will still be an uninitialized variable. Also you are trying to retrieve a character, anything you can enter via the keyboard is a character including the numbers '0','1','2', etc. If you want to test if a number has been entered you will need to test the character to see if it is a number with something like isalpha() or isdigit().


Jim

This post has been edited by jimblumberg: 03 June 2012 - 10:34 AM

Was This Post Helpful? 1
  • +
  • -

#6 Red Prince  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 03-June 12

Re: if statement is missing )

Posted 03 June 2012 - 11:40 AM

you are all right. Actually my teacher asked me to do this using if statement that's why I use if statement instead of isalpha or isdigit . Also, I am a new user so that's why I am making mistakes but I hope I'll learn from my this errors. Thank you gys for helping me.
Was This Post Helpful? 0
  • +
  • -

#7 bobmax  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 20
  • Joined: 29-July 11

Re: if statement is missing )

Posted 03 June 2012 - 01:11 PM

Quote

And, although I myself use Dev-Cpp sometimes if I am too lazy to open VC++, Dev-Cpp is also not exactly updated. It's last release was in 2005, and that was also a beta.


yes you're right,but at least its more updated/better than Turbo C++.....

Quote

Well, your code is also not perfect.

oh yes, thank you for improving my code.

Quote

And, although I myself use Dev-Cpp sometimes if I am too lazy to open VC++, Dev-Cpp is also not exactly updated. It's last release was in 2005, and that was also a beta.


yes you're right,but at least its more updated/better than Turbo C++.....

Quote

Well, your code is also not perfect.

oh yes, thank you for improving my code.
and actually i am not as good in java as in other languages and i barely use it..

anyway thanks..

sorry i double quoted everything by mistake :wacko: :wacko: :wacko:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1