![]() |
| | #1 |
| Registered User Join Date: Mar 2010
Posts: 5
| Error in printf line,,,what's wrong? 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))) ;
}
|
| mft_ika is offline | |
| | #2 |
| Registered User Join Date: Sep 2007
Posts: 507
| Looks to me like you have an extra } before that printf(). |
| cas is offline | |
| | #3 |
| and the hat of Destiny Join Date: Aug 2001 Location: The edge of the known universe
Posts: 22,495
| > 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. |
| Salem is offline | |
| | #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 ;
}
Last edited by kiruthika; 03-18-2010 at 11:00 PM. |
| kiruthika is offline | |
| | #5 | |
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 3,030
| Quote:
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.
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 There are no stupid questions, only stupid people. 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 | |
| hk_mp5kpdw is offline | |
| | #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") ;
}
Last edited by mft_ika; 03-19-2010 at 06:57 PM. |
| mft_ika is offline | |
| | #7 |
| Registered User Join Date: Feb 2010 Location: London, United Kingdom
Posts: 1,062
| 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);
The result? ---- NUCLEAR MELTDOWN. DO NOT EVER use scanf and input incompatible types. |
| claudiu is offline | |
| | #8 |
| Registered User Join Date: Feb 2010 Location: London, United Kingdom
Posts: 1,062
| 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. |
| claudiu is offline | |
| | #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. |
| mft_ika is offline | |
| | #10 | |
| Registered User Join Date: Mar 2010
Posts: 13
| Quote:
Code: scanf ("%lf",&coef) ;
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);
Or just scanf into strings, then examine the strings to see what you've got. | |
| smokeyangel is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help me with my project | ewic0190 | C Programming | 17 | 03-17-2010 06:56 PM |
| get keyboard and mouse events | ratte | Linux Programming | 10 | 11-17-2007 05:42 PM |
| I need help on this particular Linked List problem | sangken | C Programming | 11 | 08-06-2006 12:26 AM |
| Double to Int conversion warning | wiznant | C Programming | 15 | 09-19-2005 09:25 PM |
| Contest Results - May 27, 2002 | ygfperson | A Brief History of Cprogramming.com | 18 | 06-18-2002 01:27 PM |