C/how to repeat the program ?
Expert: Zlatko - 2/19/2010
Questionhi, below attached is my code for my program .
would like to ask wat should i do if i want the program to repeat by user input eg) Y/N
my code :
#include <stdio.h>
#include<conio.h>
#include<string.h>
char t;
double F;
int within_x_percent(double ref,double data,double x) ;
double ref;
double data;
double x = .05;
void main()
{
{ printf("Temperature Input"); /* Choose of the temperature input*/
printf("
[C] for Celsius
");
printf("[F] for Fahrenheit
");
printf("
Choose unit
");
scanf("%c", &t);
if (islower(t))
t = toupper(t);
switch(t)
{
case 'F' :
printf("
Enter the temperature in Fahrenheit:
");
scanf("%lf", &F);
data = (5.0/9.0)*(F-32.0);
printf("
T= %0.2lf C
",data);
break;
case 'C' :
printf("
Enter the temperature in Degree Celsius:
");
scanf("%lf", &data);
printf("
T= %0.2lf C
",data);
}
}
{
if (within_x_percent(100,data,x))
printf("Your substance is Water.
");
else if (within_x_percent(357,data,x))
printf("Your substance is Mercury.
");
else if (within_x_percent(1187,data,x))
printf("Your substance is Copper.
");
else if (within_x_percent(2193,data,x))
printf("Your substance is Silver.
");
else if (within_x_percent(2660,data,x))
printf("Your substance is Gold.
");
else
printf("Substance Unknown
");
}
}
int within_x_percent(double ref,double data,double x)
{
if (data >=( ref - x * ref) && data <= (ref + x * ref))
return 1;
else
return 0;
}
AnswerHello chuan jian
What you need to do is put the entire code into a loop and at the end of the loop, ask the user if they want to continue. I'll bet you already tried that, but you probably had trouble with the user input.
Getting user input can be quite complicated because the user can be so unpredictable. In a really robust program, you need to make sure that:
* your input buffers do not overflow
* the user enters the type of data the program needs
* any extra input on the line is consumed before the next user input is done.
In your case we'll keep it simple. Its always good to start simple when learning. The problem with your program is with the scanf.
scanf("%c", &t)
reads one character into t, but there is still the newline or return character left in the input buffer.
The same is true for scanf("%lf"...)
If you try to read another character from the input buffer after the scan("%lf"), like this:
scanf("%c"), the scanf immediately returns as it reads the newline.
So, if you intend to use scanf, make sure you always read the newline character as well. You would write:
scanf("%c%c", &t, &newline);
scanf("%lf%c", &F, &newline);
and
scanf("%lf%c", &data, &newline);
This will work if the user is co-operative and doesn't input any extra characters.
Lets assume the user is co-operative.
Below is your code, with the processing in a loop. At the end of the loop, is a call to a function tryAgain. The function asks the user for a response, and return 1 if the user responds with 'y'.
Before getting to the code, you can educate yourself more on the problems of scanf at
http://www.gidnetwork.com/b-59.html
Keep practicing!
Best regards
Zlatko
#include <stdio.h>
#include <conio.h>
#include <string.h>
char t;
char newline;
double F;
int within_x_percent(double ref,double data,double x) ;
double ref;
double data;
double x = .05;
int tryAgain(void);
void main()
{
while(1)
{
printf("Temperature Input"); /* Choose of the temperature input*/
printf(" [C] for Celsius ");
printf("[F] for Fahrenheit ");
printf(" Choose unit ");
scanf("%c%c", &t, &newline);
if (islower(t))
t = toupper(t);
switch(t)
{
case 'F' :
printf(" Enter the temperature in Fahrenheit: ");
scanf("%lf%c", &F, &newline);
data = (5.0/9.0)*(F-32.0);
printf(" T= %0.2lf C ",data);
break;
case 'C' :
printf(" Enter the temperature in Degree Celsius: ");
scanf("%lf%c", &data, &newline);
printf(" T= %0.2lf C ",data);
}
if (within_x_percent(100,data,x))
printf("Your substance is Water. ");
else if (within_x_percent(357,data,x))
printf("Your substance is Mercury. ");
else if (within_x_percent(1187,data,x))
printf("Your substance is Copper. ");
else if (within_x_percent(2193,data,x))
printf("Your substance is Silver. ");
else if (within_x_percent(2660,data,x))
printf("Your substance is Gold. ");
else
printf("Substance Unknown ");
if (!tryAgain()) break;
}
}
int within_x_percent(double ref,double data,double x)
{
if (data >=( ref - x * ref) && data <= (ref + x * ref))
return 1;
else
return 0;
}
int tryAgain(void)
{
char response;
char newline;
printf("\nTry Again ? [y/n] ");
scanf("%c%c", &response, &newline);
if (response == 'y') return 1;
return 0;
}