Thread: Sams Teach Yourself ?

  1. #1
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179

    Sams Teach Yourself ?

    This is question #8 from chapter 5 in Sams teach yourself c programming in 21 days:

    8. Write a function that receives two numbers as arguments. The function
    should divide the first number by the second. Don't divide by the second
    number if it's zero. (Hint: Use an if statement.)

    Code:
    #include <stdio.h>
    
    float a,b,c=0;
    
    float divide(float c,float d);
    
    
    
    int main(void)
    {
    	printf("Please enter two numerical values(deciamals are accepted): ");
    	scanf("%f,%f",&a,&b);
    
    	float divide(float c,float d);
    
    	c=float divide(float c, float d);
    
    	printf("Answer: %d", c);
    
    	return 0;
    }
    
    float divide(float c, float d)
    {
    	float e=0;
    
    	if(d>0)
    		e=c/d;
    	else
    		void;
    
    		return e;
    }
    Anyways when I execute it, I enter one number and it goes straight to "Hit any key to..." how do I stop this, it has never happened before and I know this should be an easy question but still I would appreciate a little help thanks in advance

    P.S. The only error it has is: (16) : error C2062: type 'float' unexpected, how exactly do I go about fixing that, because it is already declared as a float...
    Last edited by CAP; 09-03-2002 at 06:30 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Anyways when I execute it
    You mean it actually compiles for you? What compiler do you use?
    Code:
    #include <stdio.h>
    
    /*
    ** Globals should be avoided when possible.
    */
    float a,b,c=0;
    
    float divide(float c,float d);
    
    int main(void)
    {
      printf("Please enter two numerical values(deciamals are accepted): ");
      /*
      ** Be aware that scanf will expect the user to enter <num>,<num>.
      */
      scanf("%f,%f",&a,&b);
      /*
      ** You already did this, there's no need for another prototype.
      */
      float divide(float c,float d);
      /*
      ** This is an invalid function call.
      */
      c=float divide(float c, float d);
      /*
      ** We are working with floats, right? Why print an integer value then?
      */
      printf("Answer: %d", c);
      
      return 0;
    }
    
    float divide(float c, float d)
    {
      /*
      ** Not needed.
      */
      float e=0;
      
      if(d>0)
        e=c/d;
      else
        /*
        ** This is illegal.
        */
        void;
      
      return e;
    }
    Here is another way to do it, with the errors (mostly)removed. But that is for a later lesson.
    Code:
    #include <stdio.h>
    
    static float divide(float c,float d);
    
    int main(void)
    {
      float a, b, c;
      printf("Enter two numbers(decimals are accepted): ");
      if ( scanf("%f %f",&a,&b) == 2 ) {
        c=divide(a, b);
        printf("Answer: %.2f", c);
      }
      return 0;
    }
    
    static float divide(float c, float d)
    {
      if(d > 0.0)
        return c / d;
      return 0.0f;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Well I will try to fix it, then if it is still weird(most likely ) I will probably try your "later lesson" way, thanks Prelude

    P.S.
    >Anyways when I execute it
    You mean it actually compiles for you? What compiler do you use?
    Sorry about that, I have yet another program that...er somewhat works , but that is for a "later time" right
    Last edited by CAP; 09-03-2002 at 08:10 PM.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I will probably try your "later lesson" way
    By later lesson I meant the remaining problem areas. I'm still curious as to what compiler ran your code, even MSVC++ 6 gave several errors on it and we all know how lenient that compiler is.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    That would be what I am using, and it did give me errors, I just got the 3 different programs mixed up , sorry again, like I said I will just try what you posted and go from there, I don't like these "factorials" and "recursive functions" very much, but then again I am only learning and haven't made anything good so I suppose when/if I do I will find out then.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Book Recommendations
    By sean in forum C Programming
    Replies: 160
    Last Post: 07-22-2011, 01:55 PM
  2. sams C++ 21 days code error
    By marbles in forum C++ Programming
    Replies: 9
    Last Post: 03-12-2009, 07:11 PM
  3. Sams Books
    By algi in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 01-04-2005, 10:47 AM
  4. Sams Teach Yourself Game Programming in 24 Hours
    By Android in forum Game Programming
    Replies: 17
    Last Post: 06-08-2004, 10:20 PM
  5. Sams Teach Yourself in 24 hours.....
    By RealityFusion in forum C++ Programming
    Replies: 20
    Last Post: 08-21-2003, 10:55 PM