Return True or False, Not Print

This is a discussion on Return True or False, Not Print within the C Programming forums, part of the General Programming Boards category; If my two input values are equal, the program returns true...otherwise it returns false. How in the world will the ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    22

    Arrow Return True or False, Not Print

    If my two input values are equal, the program returns true...otherwise it returns false.

    How in the world will the user know it is true or false if neither will appear on the screen? My boolean function is facing that problem. Instead of using printf("True") or printf("False"); he wants something else.

    My code is as follows.
    Code:
    /*Project #30d
    Herbert Ortiz
    Due: September 23, 2004
    This program returns true if two values are equal, if not, it returns false*/
    #include <stdio.h>
    #include <conio.h>
    
    typedef enum Bool {false=0, true=1} bool; //enables boolean functions
    int AreSame(int integer1, int integer2);  //function prototype
    
    main()
    {
     int integer1, integer2, evaluation;     //variables declared
    
     printf("Enter a number: ");  //input prompt 1
     scanf("%d", &integer1);      //integer1 is stored in memory as a placeholder
    
     printf("Enter a number: ");  //input prompt 2
     scanf("%d", &integer2);     //integer 2 is stored in memory as a placeholder
    
     evaluation=AreSame(integer1, integer2);  //this calls the function
    
     getch();
    }
    bool AreSame(int integer1, int integer2)  //function header
    {
     int true=1, false=0;
    
     if(integer1==integer2)   //all of the below make up the function definition
     {
      return true;  //true is returned if the input values are the same
     }
     else        //if they are not the same, false is returned
     {
      return false;
     }
    }
    There's something wrong with the logic in my AreSame bool function, isn't there?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Forget the bool stuff...AreSame could just be:
    Code:
    int AreSame(int integer1, int integer2)
    {
      return integer1 == integer2;
    }
    It will return 1 if they're the same and 0 if they're not.

    And then you can print in the calling function with something like:
    Code:
    puts(AreSame(integer1, integer2) ? "True" : "False");
    Last edited by itsme86; 10-10-2004 at 02:15 PM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Instead of using printf("True") or printf("False"); he wants something else.
    Like what? Does he want you to have main return true or false? You'll need to ask for clarification or we won't be able to help you much in the way of solving your problem. We can help you quite a bit with the code you posted though:
    Code:
    /*
      Project #30d
      Julienne Guth
      Due: September 23, 2004 (Overdue?)
      
      Return true if two numbers are equal, false otherwise.
    */
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      int a, b;
    
      printf ( "Enter two numbers: " );
      fflush ( stdout );
      
      if ( scanf ( "%d%d", &a, &b ) != 2 ) {
        fprintf ( stderr, "Invalid input\n" );
        return EXIT_FAILURE;
      }
    
      if ( a == b )
        printf ( "True\n" );
      else
        printf ( "False\n" );
    
      return EXIT_SUCCESS;
    }
    There's little need for a boolean type or a separate function in this case.
    My best code is written with the delete key.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    *shakes head* What's wrong with people?
    tf.c:7: warning: return type defaults to `int'
    tf.c: In function `main':
    tf.c:19: warning: control reaches end of non-void function
    tf.c: At top level:
    tf.c:21: error: conflicting types for `AreSame'
    tf.c:4: error: previous declaration of `AreSame'
    Seriously, what's the point of even having warnings on a compiler? Noone uses them. Ever.

    Not to mention, do you see a problem with this:
    Code:
    typedef enum Bool {false=0, true=1} bool;
    ...snip...
    bool AreSame(int integer1, int integer2)  //function header
    {
     int true=1, false=0;
    
     if(integer1==integer2)   //all of the below make up the function definition
     {
      return true;  //true is returned if the input values are the same
     }
     else        //if they are not the same, false is returned
     {
      return false;
     }
    }
    Now which true and which false did you plan on returning?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    Somehow, this just seems neater to me
    Code:
    bool AreSame(int integer1, int integer2) {
        return integer1 == integer2;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    22

    Variety of Feedback

    I see that all of your assessments are pretty good. What I should have clarified was that either true or false would be returned to main().

    I will test all of your suggestions and see which one suits my fancy You are all passionate about this stuff, aren't you?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-02-2008, 12:56 AM
  2. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 08:14 AM
  3. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 11:40 AM
  4. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM

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