Thread: Error in printf line,,,what's wrong?

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    Error in printf line,,,what's wrong?

    Hi all!

    I need help for the following code. When I try to compile it, the program blocks the line printf (" kA(T2-T1) \n") .
    I don't understand what's wrong with that line.

    Also, I don't know whether I use variable "counter" the right way. The idea is to count the input from the user. If it's not 5, that means one must be 0 (the one with the question mark inputted), and thus will go to the respective function. But if it's 5, the user must input again the data. At this point I don't understand how to loop it back to the first 6 steps (inquiring input from user).

    Anyone,,please help me with this.
    Thank you so much before. ^^

    Code:
    /* This program is used to solve simple conduction problems using various forms of the formula*/
    #include <stdio.h> /* define printf and scanf */
    #include <math.h> /* define mathematical operations */
    
    double calc_h (double coef, double thickness, double area, double temp1, double temp2) ; /* define function named calc_k */
    double calc_k (double heat_rate, double thickness, double area, double temp1, double temp2) ; /* define function named calc_k */
    double calc_a (double heat_rate, double thickness, double coef, double temp1, double temp2) ; /* define function named calc_a */
    double calc_x (double heat_rate, double coef, double area, double temp1, double temp2) ; /* define function named calc_x */
    double calc_t2 (double heat_rate, double thickness, double area, double temp1, double coef) ; /* define function named calc_t1 */
    double calc_t1 (double heat_rate, double thickness, double area, double coef, double temp2) ; /* define function named calc_t2 */
    
    
    main() 
    {
      /* define variables that will be used */     
      double heat_rate  ; 
      double coef ;
      double area ; 
      double temp1 ;
      double temp2 ;
      double thickness ;
      int counter ;
          
      /* mention the background and the purpose of the program */
      printf ("Respond to the the prompts with the data known, \n") ;
      printf ("Unknown quantity, enter a question mark (?). \n") ;
    
      printf ("Rate of heat transfer (unit = watts) ---> \n") ;
      scanf ("%lf",&heat_rate) ;
    
      printf ("Coefficient of thermal conductivity (unit = W/m-K) ---> \n") ;
      scanf ("%lf",&coef) ;
    
      printf ("Cross sectional area of conductor (unit = m^2) ---> \n") ;
      scanf ("%lf",&area) ;
    
      printf ("Temperature on the first side (unit = Kelvin) ---> \n") ;
      scanf ("%lf",&temp1) ;
    
      printf ("Temperature on the second side (unit = Kelvin) ---> \n") ;
      scanf ("%lf",&temp2) ;
    
      printf ("Thickness of conductor (unit = m) ---> \n") ;
      scanf ("%lf",&thickness) ;
      
      counter = scanf ("%lf %lf %lf %lf %lf %lf", &heat_rate, &coef, &area, &temp1, &temp2, &thickness) ;
      
      if (counter == 0)
      {
      if(heat_rate == 0)
      {
            calc_h (coef, thickness, area, temp1, temp2)      ; 
      }
      else if (coef == 0)
      {
            calc_k (heat_rate, thickness, area, temp1, temp2) ;
      }
      else if (area == 0)
      {
            calc_a (heat_rate, thickness, coef, temp1, temp2)  ;
      }
      else if (temp1 == 0)
      {
            calc_t1 (heat_rate, thickness, area, coef, temp2) ;
      }
      else if (temp2 == 0)
      {
            calc_t2 (heat_rate, thickness, area, temp1, coef) ;
      }
      else
      {
            calc_x (heat_rate, coef, area, temp1, temp2) ;
      }
      }else
      {
          printf ("Invalid input! You must input a question mark for the unknown variable. \n") ;
      }
      }
    
    
      printf ("                kA (T2-T1)   \n") ;
      printf ("           H = ------------  \n") ;
      printf ("                    x        \n") ;
    
      printf ("H = %lf W       T2 = %lf K" ,heat_rate, temp2) ;
      printf ("k = %lf W/m-K   T1 = %lf K", coef, temp1) ;
      printf ("A = %lf m^2     x = %lf m" , area, thickness) ;
     
      
      system ("PAUSE") ;
      return 0 ;
    }
    
    
    
    /* function named calc_k, used to calculate the coefficient of thermal conductivity */
    double calc_k (double heat_rate, double thickness, double area, double temp1, double temp2)
    {
           return ((heat_rate*thickness)/(area*(temp2-temp1))) ;
    }
    
    /* function named calc_a, used to calculate the cross sectional area */
    double calc_a (double heat_rate, double thickness, double coef, double temp1, double temp2)
    {
           return ((heat_rate*thickness)/(coef*(temp2-temp1))) ;
    }
    
    /* function named calc_x, used to calculate the thickness of the conductor */
    double calc_x (double heat_rate, double coef, double area, double temp1, double temp2)
    {
           return ((coef*area*(temp2-temp1))/heat_rate) ;
    }
    
    /* function named calc_h, used to calculate the rate of heat transfer */
    double calc_h (double coef, double thickness, double area, double temp1, double temp2)
    {
           return ((coef*area*(temp2-temp1))/thickness) ;
    }
    
    /* function named calc_t2, used to calculate the temperature on the second or furthest side of the conductor */
    double calc_t2 (double heat_rate, double thickness, double area, double temp1, double coef)
    {
           return (((heat_rate*thickness)/(coef*area))+temp1) ;
    }
    
    /* function named calc_t1, used to calculate the temperature on the first or nearest side of the conductor */
    double calc_t1 (double heat_rate, double thickness, double area, double coef, double temp2)
    {
           return (temp2-((heat_rate*thickness)/(coef*area))) ;
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Looks to me like you have an extra } before that printf().

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > printf ("H = %lf W T2 = %lf K" ,heat_rate, temp2) ;
    > printf ("k = %lf W/m-K T1 = %lf K", coef, temp1) ;
    > printf ("A = %lf m^2 x = %lf m" , area, thickness) ;
    Perhaps these lines too need a \n at the end of them.

    Or call
    fflush(stdout);
    when you're done.
    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.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    26
    Nothing big problem here.
    Actually you have closed the main() function before printf statement.
    So correct that one.
    It will work correctly.

    Problem in your code
    Code:
    }
    
    
      printf ("                kA (T2-T1)   \n") ;
      printf ("           H = ------------  \n") ;
      printf ("                    x        \n") ;
    
      printf ("H = %lf W       T2 = %lf K" ,heat_rate, temp2) ;
      printf ("k = %lf W/m-K   T1 = %lf K", coef, temp1) ;
      printf ("A = %lf m^2     x = %lf m" , area, thickness) ;
     
      
      system ("PAUSE") ;
      return 0 ;
    }
    Remove the curly bracket which is above to the statement printf (" kA (T2-T1) \n") ;
    Last edited by kiruthika; 03-18-2010 at 11:00 PM.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by mft_ika View Post
    Also, I don't know whether I use variable "counter" the right way. The idea is to count the input from the user. If it's not 5, that means one must be 0 (the one with the question mark inputted), and thus will go to the respective function. But if it's 5, the user must input again the data. At this point I don't understand how to loop it back to the first 6 steps (inquiring input from user).
    No, you're not using it correctly. That scanf would attempt to get more input from the user (they'd need to type everything in over again which defeats the purpose of asking it the first time around).

    Your overall I/O would need work if you truly expect to process question mark '?' characters by using scanf to read into doubles with %lf. On encountering a question mark, the scanf would fail (returning 0) and the stream would go into an error state. Before proceeding to the next input you'd have to clear the error state and flush the current contents of the buffer (otherwise the data that caused the error state would still be there polluting the next attempt at input). You would need to do this error checking each time you try and process user's input.

    Your if/else if block of code also assumes that an unsuccessful input attempt into an uninitialized variable magically sets the variable's contents to 0...

    ...it does not BTW.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    Thank you all for your help!
    I've revised the code. This time it works, but I think the logic is still wrong. because each time I input a question mark, the program will terminate and skip the steps of inquiring other data from the user. This is the code.

    Code:
    /* This program is used to solve simple conduction problems using various forms of the formula*/
    #include <stdio.h> /* define printf and scanf */
    #include <math.h> /* define mathematical operations */
    
    double calc_h (double coef, double thickness, double area, double temp1, double temp2) ; /* define function named calc_k */
    double calc_k (double heat_rate, double thickness, double area, double temp1, double temp2) ; /* define function named calc_k */
    double calc_a (double heat_rate, double thickness, double coef, double temp1, double temp2) ; /* define function named calc_a */
    double calc_x (double heat_rate, double coef, double area, double temp1, double temp2) ; /* define function named calc_x */
    double calc_t2 (double heat_rate, double thickness, double area, double temp1, double coef) ; /* define function named calc_t1 */
    double calc_t1 (double heat_rate, double thickness, double area, double coef, double temp2) ; /* define function named calc_t2 */
    void check (double var, double *temporary) ;
    
    main() 
    {
      /* define variables that will be used */     
      double heat_rate  ; 
      double coef ;
      double area ; 
      double temp1 ;
      double temp2 ;
      double thickness ;
      double counter ;
      counter = 1 ;
          
      /* mention the background and the purpose of the program */
      printf ("Respond to the the prompts with the data known, \n") ;
      printf ("Unknown quantity, enter a question mark (?). \n") ;
      
      printf ("Rate of heat transfer (unit = watts) ---> ") ;
      scanf ("%lf",&heat_rate) ;
      check (heat_rate, &counter) ;
    
      printf ("Coefficient of thermal conductivity (unit = W/m-K) ---> ") ;
      scanf ("%lf",&coef) ;
      check (coef, &counter) ;
    
      printf ("Cross sectional area of conductor (unit = m^2) ---> ") ;
      scanf ("%lf",&area) ;
      check (area, &counter) ;
    
      printf ("Temperature on the first side (unit = Kelvin) ---> ") ;
      scanf ("%lf",&temp1) ;
      check (temp1, &counter) ;
    
      printf ("Temperature on the second side (unit = Kelvin) ---> ") ;
      scanf ("%lf",&temp2) ;
      check (temp2, &counter) ;
    
      printf ("Thickness of conductor (unit = m) ---> ") ;
      scanf ("%lf",&thickness) ;
      check (thickness, &counter) ;
      
          
      if (counter == 0)
      {
      if(heat_rate == 0)
      {
            calc_h (coef, thickness, area, temp1, temp2)      ; 
      }
      else if (coef == 0)
      {
            calc_k (heat_rate, thickness, area, temp1, temp2) ;
      }
      else if (area == 0)
      {
            calc_a (heat_rate, thickness, coef, temp1, temp2)  ;
      }
      else if (temp1 == 0)
      {
            calc_t1 (heat_rate, thickness, area, coef, temp2) ;
      }
      else if (temp2 == 0)
      {
            calc_t2 (heat_rate, thickness, area, temp1, coef) ;
      }
      else
      {
            calc_x (heat_rate, coef, area, temp1, temp2) ;
      }
      }else
      {
          printf ("Invalid input! You must input a question mark for the unknown variable. \n") ;
      }
      
    
    
      printf ("                kA (T2-T1)   \n") ;
      printf ("           H = ------------  \n") ;
      printf ("                    x        \n") ;
    
      printf ("H = %lf W       T2 = %lf K \n" ,heat_rate, temp2) ;
      printf ("k = %lf W/m-K   T1 = %lf K \n", coef, temp1) ;
      printf ("A = %lf m^2     x = %lf m \n" , area, thickness) ;
     
      
      system ("PAUSE") ;
      return 0 ;
    }
    
    
    
    /* function named calc_k, used to calculate the coefficient of thermal conductivity */
    double calc_k (double heat_rate, double thickness, double area, double temp1, double temp2)
    {
           return ((heat_rate*thickness)/(area*(temp2-temp1))) ;
    }
    
    /* function named calc_a, used to calculate the cross sectional area */
    double calc_a (double heat_rate, double thickness, double coef, double temp1, double temp2)
    {
           return ((heat_rate*thickness)/(coef*(temp2-temp1))) ;
    }
    
    /* function named calc_x, used to calculate the thickness of the conductor */
    double calc_x (double heat_rate, double coef, double area, double temp1, double temp2)
    {
           return ((coef*area*(temp2-temp1))/heat_rate) ;
    }
    
    /* function named calc_h, used to calculate the rate of heat transfer */
    double calc_h (double coef, double thickness, double area, double temp1, double temp2)
    {
           return ((coef*area*(temp2-temp1))/thickness) ;
    }
    
    /* function named calc_t2, used to calculate the temperature on the second or furthest side of the conductor */
    double calc_t2 (double heat_rate, double thickness, double area, double temp1, double coef)
    {
           return (((heat_rate*thickness)/(coef*area))+temp1) ;
    }
    
    /* function named calc_t1, used to calculate the temperature on the first or nearest side of the conductor */
    double calc_t1 (double heat_rate, double thickness, double area, double coef, double temp2)
    {
           return (temp2-((heat_rate*thickness)/(coef*area))) ;
    }
    
    void check (double var, double *temporary) 
    {
    if (var == 0)
    *temporary = *temporary - 1 ; 
    else 
    printf ("\n") ; 
    }
    could anyone help me with this? thank you so much again!
    Last edited by mft_ika; 03-19-2010 at 06:57 PM.

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    If you want to simulate a variable input with the question mark you need to read your input as a string. If the string is equal to '?' then proceed to do whatever you need to do in this case. If not, convert the string to a double.

    If you do:

    Code:
    double x;
    scanf("%lf", &x);
    ... and input "?" then scanf will crash and so will your whole program.

    The result? ---- NUCLEAR MELTDOWN. DO NOT EVER use scanf and input incompatible types.

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    An easier way would be to do something like this.

    Ask the user if HE/SHE KNOWS the value for whatever variable you are asking and ask him to input y(yes) or n(no). Read the response char. If it is yes, proceed to do a regular scanf for a double value. If not, just assume the "?" case and do whatever you need to do.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    but the question asked me to do that with scanf. The question is telling us that an error state will occur because of entering question mark, but I don't know how to solve it.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by mft_ika View Post
    but the question asked me to do that with scanf. The question is telling us that an error state will occur because of entering question mark, but I don't know how to solve it.
    You could do it the hard way...

    Code:
    scanf ("%lf",&coef) ;
    It will try to match a float. Given a question mark, it'll fail to match anything. The coef variable is left alone, and the input stream is too. Effectively, it means nothing happened when the scanf was executed.

    scanf returns the number of values it was able to match. So this will return 0 if you put in "?". Then you need to clear the question mark from the stream since you're not doing anything with it.

    Code:
    char c;
    scanf("%c", &c);
    will do it. Only do that if you didn't read a float (scanf returned 0)



    Or just scanf into strings, then examine the strings to see what you've got.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my project
    By ewic0190 in forum C Programming
    Replies: 17
    Last Post: 03-17-2010, 06:56 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. I need help on this particular Linked List problem
    By sangken in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 12:26 AM
  4. Double to Int conversion warning
    By wiznant in forum C Programming
    Replies: 15
    Last Post: 09-19-2005, 09:25 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM