-2

I just started programming in C a few days ago and have a few questions:

The following program converts Celsius into Fahrenheit and vice versa. I am getting a Segmentation fault error.

#include<stdio.h>
#include <string.h>
#include<stdlib.h>

float c2f(float);
float f2c(float);

float Fahrenheit,Celsius;

int main(int argc, char *argv[])
{

/** 
 * 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"))
{
   // convert the string into a floating number
   char *check;
   float Celsius = strtod(argv[2], &check);

// process from celsius to fahrenheit
   Fahrenheit = c2f(Celsius);
   printf("%5.2f°C = %5.2f°F",Celsius, Fahrenheit);
}   
else if (!strcmp(argv[1], "->c"))
{
   // convert the string into a floating number
   char *check;
   float Fahrenheit = strtod(argv[2], &check);

   // process from fahrenheit to celsius
   Celsius = f2c(Fahrenheit);
   printf("%5.2f°F = %5.2f°C", Fahrenheit, Celsius);


}   
else
   printf("Invalid flag\n");
} // main


float c2f(float c)
{
  return 32 + (c * (180.0 / 100.0)); 
} 

float f2c(float f)
{
  return (100.0 / 180.0) * (f - 32);
}

Also, I want my output to be like this:

**> TemperatureConverter ->f 10.0

10.00°C = 50.00°F**

This should convert 10C into F.

For F to C, the output should be:

TemperatureConverter ->c 50.0

50.00°F = 10C**

Sarah97
  • 59
  • 1
  • 2
  • 10
  • 1
    It really is time to read the warning/error messages and try to do the work yourself before posting on SO. If a warning such as "implicit declaration of function ‘strcmp’" is baffling you, post a reduced program, else it certainly looks like you are obliging SO to the lion's share of work. – chux - Reinstate Monica Oct 16 '13 at 22:48
  • Hey chux, I just started programming in C 4 days ago and I am still pretty confused about the errors. – Sarah97 Oct 16 '13 at 22:54
  • It isn't that you are posting about problems, it is that you are not demonstrating your share of problem solving. Rather than post your whole code with multiple errors and say "I am not sure why", ask a specific question about a select aspect of your code. Show that you have tried to researched and figure out the answer too before stopping at SO. – chux - Reinstate Monica Oct 16 '13 at 23:04
  • I have researched and have finished writing my entire code. It's just that I'm getting some errors and I have no idea why I'm getting them and how to solve them. If you have any idea on how to fix the printf error and why I am getting it, you would be very helpful. – Sarah97 Oct 16 '13 at 23:07
  • *Started coding in C 4 days ago, just saying. – Sarah97 Oct 16 '13 at 23:09

3 Answers3

3

the error is if (!strcmp(argv[1], "->f")

it's missing a final parenthesis, should be

if (!strcmp(argv[1], "->f"))

and you made the same mistake twice. 1 paren for strcmp(), 1 for if()

you should include string.h. Also, you should put you functions f2c and c2f before main.

also you wrote

prinf

try with a t before the f

printf

finally you need

exit(0);

after the first if. eg

if (argc!=3)
{
    printf("Incorrect number of arguments");
    exit(0);
}

otherwise the rest of the program runs and you get seg fault. Welcome to programming.

AwokeKnowing
  • 7,728
  • 9
  • 36
  • 47
  • Thanks, I didn't see that. I corrected it, added string.h, f2c and c2f projections before main but I'm still getting the errors for some reason :( – Sarah97 Oct 16 '13 at 22:46
  • @MichaelBlue `#include ` as well for `strtod` – Kninnug Oct 16 '13 at 22:49
  • you did recompile right? :) J/K. so you're getting the exact same errors? – AwokeKnowing Oct 16 '13 at 22:57
  • Hey Kninnug! I did include #include but I'm still getting the printf errors for some reason :/ – Sarah97 Oct 16 '13 at 22:57
  • ok, edited answer. you typed prinf instead of prinf (or maybe you copied and pasted from somewhere). you did it twice – AwokeKnowing Oct 16 '13 at 23:02
  • Yes, I did recompile :) The stdlib errors are gone since I've included it into my code but the printf errors are still there :/ – Sarah97 Oct 16 '13 at 23:02
  • 2
    see my edit. you mistyped. Now mark this as the answer!!! and check your code more carefully! Stackoverflow isn't for finding your typos (but don't feel bad. the error messages are not helpful to beginners, I understand) – AwokeKnowing Oct 16 '13 at 23:04
0

Slight nitpicking:

float c2f(float);
float f2c(float);

While it's technically correct, remember to include the variable name as well in your function declarations. It makes it easier to read.

As an example

float c2f(float c);
Jesper Bangsholt
  • 544
  • 4
  • 11
0

I used this code :

/* Declare Initial library for functions */
    #include<stdio.h>
    #include<conio.h>

    /* Main function*/
    void main()
    {
     /* data type(float),variable(c,f)*/   
     float c, f;

    /* printf function from stdio.h library , for printing level*/
     printf("Enter temp. in Celsius: ");

    /* scanf for inserting data in variable*/
     scanf("%f",&c);

      /* Fahrenheit rules*/
     f = c * 9/5 + 32;

     /* Result will display in this line */
     printf("Temp. in Fahrenheit: %f",f);

    /* getch function from conio.h library, used to write a character to screen*/
     getch();
    }
Omar Faruk
  • 298
  • 5
  • 19
  • This question is looking for an *explanation*, not simply for working code. Your answer provides no insight for the questioner, and may be deleted. Please [edit] to explain what causes the observed symptoms. – Toby Speight Aug 01 '17 at 11:31
  • Now this answer is ok? – Omar Faruk Aug 01 '17 at 12:04