Thread: if /else

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

    if /else

    Is there a problem with this code because i cant compile it

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    float get_value(void);
    int main(int argc, char* argv[])
    
    {
          system("cls");
          
    
    }
    float get_value(void)
    {   
          float x,c;
          char t;
          scanf("\n&#37;c",t);
          if(t=="A"||t=="a")
          {      printf("hi");
                  return(t);
          }
          
          else
    
          { printf("\a\nWrong input");
          }
        
          getch();
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    i cant compile it
    Very good bug description... Where is the erro message?

    scanf("\n&#37;c",t);
    Should be

    scanf("\n%c",&t);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    And this
    Code:
    if(t=="A"||t=="a")
    should be
    Code:
    if(t=='A'||t=='a')
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by joker_tony View Post
    Is there a problem with this code
    I would have to say "Yes".

    What's the matter? Did I not provide enough information in my response?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I wouldn't consider that a good practice:
    Code:
          if(t=="A"||t=="a")
          {      printf("hi");
                  return(t);
          }
          
          else
    
          { printf("\a\nWrong input");
          }
    Far better:
    Code:
    if(t=="A"||t=="a")
    {      
    	printf("hi");
    	return(t);
    }
    else
    {
    	printf("\a\nWrong input");
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You return (t), which is a char, in a function that is declared to be returning a float. But if you post the compiler output, that might help.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. noob help if /else
    By joker_tony in forum C Programming
    Replies: 2
    Last Post: 03-21-2008, 04:53 PM
  2. Need help with if /else statements - maybe switch
    By dwinslett in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2003, 07:23 AM