Program is weird...

This is a discussion on Program is weird... within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> double instructions_white(); double instructions_sweet(); double instructions_double(); double instructions_manual(); double calc_baking_time(); int main() { char bread_type; char loaf; ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    140

    Program is weird...

    Code:
     #include <stdio.h>
       double instructions_white();
       double instructions_sweet();
       double instructions_double();
       double instructions_manual();
       double calc_baking_time();
       int main()
       {
              char bread_type;
              char loaf;
              char manual;
              printf("Are you making white or sweet bread (w or s)?\n");
              scanf(" %c",&bread_type);
      
              printf("Is this a single or double loaf (s or d)?\n");
              scanf(" %c",&loaf);
      
              printf("Are you going to bake manually (y or n)?\n");
              scanf(" %c",&manual);
      
             if(bread_type=='w'||bread_type=='W')
              {
                      double instructions_white();
              }
              else
              if(bread_type=='s'||bread_type=='S')
              {
                      double instructions_sweet();
              }
      
              return 0;
      }
              double instructions_white()
      {
              printf("Op");
      
              return 0;
    So basically calling a function from within an If statement doesn't work?
    Last edited by tmac619619; 10-23-2012 at 02:04 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Can you please stop posting code with your own line numbers.
    In case you didn't notice, the board gives you line numbers for free.

    Compare
    22 if(bread_type=='w'||bread_type=='W')
    vs.
    27 if(bread_type=='s'||'S')

    Also, read your book on the difference between a function prototype and a function call.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,425
    After you type the 'w' or 's', you hit enter, which leaves a new line in the buffer. Read this link for details. A quick fix, put a space before the %c, like " %c", to skip over leading white space (like space, tab and newline). There are more robust ways in the link I provided, or in our FAQ (link).

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    140
    Wow that solved the problem!

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    140
    Quote Originally Posted by Salem View Post
    Can you please stop posting code with your own line numbers.
    In case you didn't notice, the board gives you line numbers for free.

    Compare
    22 if(bread_type=='w'||bread_type=='W')
    vs.
    27 if(bread_type=='s'||'S')

    Also, read your book on the difference between a function prototype and a function call.
    Hmm i see.

    So if i try to call a function from within an If statement, what happens?
    I entered W for bread_type, and theoretically my program should print "Op". But instead it terminates after the last prompt "Are you going to bake manually?"

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    It's nothing to do with it being if or while or for or anything else

    // This is a prototype
    double instructions_white();

    // This is a CALL
    instructions_white();

    Even when you've fixed your if statement logic problem, unless you actually CALL the function, it still won't show anything on screen.

    I mean, you manage to CALL printf and scanf without any problems.
    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.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    140
    Quote Originally Posted by Salem View Post
    It's nothing to do with it being if or while or for or anything else

    // This is a prototype
    double instructions_white();

    // This is a CALL
    instructions_white();

    Even when you've fixed your if statement logic problem, unless you actually CALL the function, it still won't show anything on screen.

    I mean, you manage to CALL printf and scanf without any problems.
    Damn i must be super tired or something cause i knew that...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird command prompt error, program not working..
    By Shamino in forum C++ Programming
    Replies: 18
    Last Post: 10-30-2007, 11:32 AM
  2. simple program weird result
    By nav301 in forum C Programming
    Replies: 3
    Last Post: 12-20-2006, 01:07 PM
  3. Weird output simple program.
    By omnificient in forum C++ Programming
    Replies: 7
    Last Post: 11-03-2005, 12:53 PM
  4. Help! Something weird going on with my program...
    By Hapster in forum C Programming
    Replies: 2
    Last Post: 04-28-2005, 08:29 AM
  5. Weird error when running program
    By Rizage in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2002, 06:41 AM

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