Reputation: 69
I just started programming in C a few days ago and I want to improve my program but not sure how to do it.
This program is a Fahrenheit to Celsius and vice versa converter. I did it in the simplest way possible. But now I want to do it so that I have 2 functions c2f and f2c which take the temperature as a parameter and when I run the program I want do choose whether I want to covert from F to C or from C to F (something like TempConverter -f 32 this should only convert 32 into celsius and TempConverter -c 100 should covert 100 into Fahrenheit).
I think my functions should be something like this : float c2f (float c) and float f2c (float f)
But how exactly do it do it so when I run something like > TempConverter -f 50.0 I get something like this let's say? 10.00°C = 50.00°F
#include<stdio.h>
int main(void)
{
// Local Declarations
float Celsius, Fahrenheit, Fahrenheit_1, Celsius_2;
// Statements
printf("Enter the temperature in Fahrenheit: ");
scanf("%f", &Fahrenheit);
printf("Fahrenheit temperature is: %5.1f F\n\a", Fahrenheit);
Celsius = (100.0 / 180.0) * (Fahrenheit - 32);
printf("Celsius temperature is: %8.1f C\n\n\a", Celsius);
printf("Enter the temperature in Celsius: ");
scanf("%f", &Celsius_2);
printf("Celsius temperature is: %8.1f C\n\a", Celsius_2);
Fahrenheit_1 = 32 + (Celsius_2 * (180.0 / 100.0));
printf("Fahrenheit temperature is: %5.1f F\a", Fahrenheit_1);
return 0;
}
Current output:
*Enter the temperature in Fahrenheit: 20
Fahrenheit temperature is: 20.0 F
Celsius temperature is: -6.7 C
Enter the temperature in Celsius: 40
Celsius temperature is: 40.0 C
Fahrenheit temperature is: 104.0 F*
Desired Output
TempConverter -f 50.0
10.00°C = 50.00°F
Upvotes: 1
Views: 5908
Reputation: 1
#include<stdio.h>
main(int argc,char** argv)
{
int result;
printf("\nValue of Celcius: \n");
scanf("%i", &argc);
printf("\n%i Celcius = ", argc);
result = ((argc * 9) /5) + 32;
printf("%i Fahrenheit\n", result);
getchar();
getchar();
return 0;
}
Upvotes: 0
Reputation: 3094
You are looking to use commandline arguments that come as arguments to main
int main(int argc, char *argv[])
Here argc
corresponds to the number of arguments supplied and the arguments are stored in argv
. Note that the name of the program is also considered an argument and is stored in argv[0]
. Assuming you expect a flag -f
or -c
followed by a float
, you can use them inside main
as follows:
/**
* Check for the expected number of arguments (3)
* (0) program name
* (1) flag
* (2) temperature
*/
if (argc!=3)
printf("Incorrect number of arguments");
if (!strcmp(argv[1], "-f"))
// process from fahrenheit to celsius
else if (!strcmp(argv[1], "-c"))
// process from celsius to fahrenheit
else
printf("Invalid flag\n");
Note, whenever you are dealing with user input, it is very important to check that the input corresponds to what you expect. My suggested code gives some example, but you should think hard about how you can make it even more secure
Upvotes: 0
Reputation: 123448
The first thing you'll need to do is add command line arguments to your code, like so:
int main( int argc, char **argv )
argc
contains the number of parameters entered on the command line; it is always >= 1.
argv
contains pointers to each of the command line argument strings. argv[0]
always points to the string you used to start the program. argv[argc]
is always NULL.
So, if you called your program as
TempConverter -f 50.00
then
argc == 3
argv[0] == "TempConverter"
argv[1] == "-f"
argv[2] == "50.00"
argv[3] == NULL
Note that to use the last parameter in your conversion functions, you'll need to convert it from a string to a floating-point value. strtod
is the library function of choice for this:
char *check;
double intemp = strtod(argv[2], &check);
After the conversion, check
will point to the first character in the string that was not converted to a floating point value. If this character is something other than whitespace or 0, then the input value was not a valid floating-point string.
So, the general order of operations:
argc
. For your purposes, it needs to be 3.argv[2]
to a floating-point value using strtod
. Make sure the check character is either 0 or whitespace."-f"
or "-c"
(or whatever you decide on).Upvotes: 1