if /else

This is a discussion on if /else within the C Programming forums, part of the General Programming Boards category; Is there a problem with this code because i cant compile it Code: #include <stdio.h> #include <conio.h> #include <stdlib.h> float ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    i cant compile it
    Very good bug description... Where is the erro message?

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

    scanf("\n%c",&t);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    And this
    Code:
    if(t=="A"||t=="a")
    should be
    Code:
    if(t=='A'||t=='a')
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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
    Posts
    22,887
    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, 05:53 PM
  2. Need help with if /else statements - maybe switch
    By dwinslett in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2003, 08:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21