Thread: noob help if /else

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    62

    noob help if /else

    iam creating a program that the user will enter (upper/lower case) from A-H or (a-h)
    how can i do that with an if statement this is what i did. but is not working

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    
    int main ()
    {
    
          system("cls");
          float a;
          scanf("\n&#37;f",&a);
          if((a==A)||(a==H)&&(a==a)||(a==h));
          {    
               printf("hi");
          }
          
          else
    
          { 
               printf("hello");
          }
      
          getch();
    }
    Last edited by joker_tony; 03-21-2008 at 04:39 PM.

  2. #2
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Firstly, declare it as

    Code:
     char c
    not float a

    You are just inviting trouble there.

    Code:
    if (((c >= 'A') && (c <= 'H')) || ((c >= 'a') && (c <= 'h'))) {
    What that does is get an ASCII comparison
    =========================================
    Everytime you segfault, you murder some part of the world

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Also...

    You are scanning it in wrong.

    If it's a character, it should be &#37;c not %f.

    Also, use getchar() instead of scanf
    =========================================
    Everytime you segfault, you murder some part of the world

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob Q: How to read a value of a byte in binary?
    By Hitsuin in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 02:46 PM
  2. I'm a noob and I need help.
    By nifear4 in forum C Programming
    Replies: 17
    Last Post: 10-14-2008, 01:20 PM
  3. noob needs help!!!!:(
    By tykenfitz in forum C Programming
    Replies: 1
    Last Post: 07-10-2005, 08:49 AM
  4. Just a little request from a noob...
    By Lee134 in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 03-18-2005, 05:45 AM
  5. noob: compiling error... plz help!
    By chosii in forum C Programming
    Replies: 2
    Last Post: 05-10-2002, 05:53 AM