Celsius to Fahrenheit converter, function error

I get "segmentation fault", probobly the convert functions

Page 1 of 1

9 Replies - 6145 Views - Last Post: 24 April 2009 - 01:53 AM Rate Topic: -----

#1 Scorpiion  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 117
  • Joined: 23-April 09

Celsius to Fahrenheit converter, function error

Posted 23 April 2009 - 12:36 PM

Hi everyone! My name is Robert and this is my first post here at dreamincode it looks like a nice forum so I think I'll be around for a while. I have some problems with my C program. It is an Celsius to Fahrenheit converter but I think I have made something wrong with the functions... But I don't really know how to fix it..

#include <stdio.h>					 

float convertC(float converted);
float convertF(float converted);

int main(void)
{			 
	int temp; 
	int value;
	float converted;

	system("clear");
	printf("Welcome to the fahrenheit/celcius converter!\n");
	printf("Please tell me if you want:\n");
	printf("1. Convert from F to C\n");
	printf("or..\n");
	printf("2. Convert from C to F\n");

scan:
	scanf("%d", value);
	if (value == 1) {
		printf("Okey what temperature do you want to convert?\n");
		scanf("%d\n", temp);
		converted = convertF(temp);
	} else if (value == 2) {
		printf("Okey what temperature do you want to convert?\n");
		scanf("%d\n", temp);
		converted = convertC(temp);
	} else {
		printf("Now you must have type something wrong, try again!\n");
		goto scan;
	}
}


float convertC(float converted)
{
	float far;
	float cel;

	far = ((9.0 / 5.0) * cel + 32.0);

	return(far);

}

float convertF(float converted)
{
	float cel;
	float far;

	cel = ((9.0 / 5.0) * far + 32.0);

	return(cel);
}



So if someone could point it out for me that would be great! :)

Regards

Is This A Good Question/Topic? 0
  • +

Replies To: Celsius to Fahrenheit converter, function error

#2 joske  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 297
  • Joined: 04-September 07

Re: Celsius to Fahrenheit converter, function error

Posted 23 April 2009 - 12:52 PM

Use scanf("%d", &value); instead of scanf("%d", value); each time you use scanf in your program. This way you send a reference to the variable instead of the variable itself.

There is another error in both your conversion functions: you have a function parameter converted, but this parameter is never used in the functions...
Was This Post Helpful? 0
  • +
  • -

#3 redkid  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 32
  • Joined: 23-April 09

Re: Celsius to Fahrenheit converter, function error

Posted 23 April 2009 - 12:57 PM

Hi there, the first mistake I noticed were when you were scanning data.

scanf("%d", value);



You have to use '&' before a variable if you want to access its memory and store data in it (unless it were a pointer). So, it should be:


scanf("%d", &value);



This you repeated for several other statements.

Also another error, not in syntax, I suppose is:


far = ((9.0 / 5.0) * cel + 32.0);



Before using the variable "cel" you did not assign the value of "converted" to it. So, it probably has gibberish in it (or zero). This is there in the other function too.

I havn't compiled it, so update if that's got it all fixed :D

This post has been edited by redkid: 23 April 2009 - 12:59 PM

Was This Post Helpful? 0
  • +
  • -

#4 Scorpiion  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 117
  • Joined: 23-April 09

Re: Celsius to Fahrenheit converter, function error

Posted 23 April 2009 - 01:13 PM

Thanks for the help so far! But I still get errors at compile now... I have changed the code a little after the tips..

#include <stdio.h>					 

float convertC(float converted);
float convertF(float converted);

int main(void)
{			 
	int temp;
	int value;
	float converted;

	system("clear");
	printf("Welcome to the fahrenheit/celcius converter!\n");
	printf("Please tell me if you want:\n");
	printf("1. Convert from F to C\n");
	printf("or..\n");
	printf("2. Convert from C to F\n");

scan:
	scanf("%d", &value);
	if (value == 1) {
		printf("Okey what temperature do you want to convert?\n");
		scanf("%d\n", &temp);
		converted = convertF(temp);
	} else if (value == 2) {
		printf("Okey what temperature do you want to convert?\n");
		scanf("%d\n", &temp);
		converted = convertC(temp);
	} else {
		printf("Now you must have type something wrong, try again!\n");
		goto scan;
	}
}

float convertF(far)
{
	float far;
	float cel;

	cel = ((9.0 / 5.0) * far + 32.0);

	return(cel);
}

float convertC(cel)
{
	float cel;
	float far;

	far = ((9.0 / 5.0) * cel + 32.0);

	return(far);
}



I guess I'm doing some dum thing somewhere... Kind of new to C-programming so I guess I have some newbie errors.. :P :)
Was This Post Helpful? 0
  • +
  • -

#5 redkid  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 32
  • Joined: 23-April 09

Re: Celsius to Fahrenheit converter, function error

Posted 23 April 2009 - 01:25 PM

Hey again, it seems you changed everything hurriedly.
Well, take it one step back and go through the whole thing when you change something, because, as expected, your mistakes are not at the beginning.
See, your function prototypes don't match your function declarations.
float convertC(float converted);
float convertF(float converted);


And also, you did not initialize your variables in function declarations properly.
float convertF(far)...
...
float convertC(cel)...


I advise you go through it again. And do the error checking yourself too because that is really important as well.

Was This Post Helpful? 0
  • +
  • -

#6 joybhadra82  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 6
  • Joined: 19-April 09

Re: Celsius to Fahrenheit converter, function error

Posted 23 April 2009 - 01:30 PM

View PostScorpiion, on 23 Apr, 2009 - 11:36 AM, said:

Hi everyone! My name is Robert and this is my first post here at dreamincode it looks like a nice forum so I think I'll be around for a while. I have some problems with my C program. It is an Celsius to Fahrenheit converter but I think I have made something wrong with the functions... But I don't really know how to fix it..

#include <stdio.h>					 

float convertC(float converted);
float convertF(float converted);

int main(void)
{			 
	int temp; 
	int value;
	float converted;

	system("clear");
	printf("Welcome to the fahrenheit/celcius converter!\n");
	printf("Please tell me if you want:\n");
	printf("1. Convert from F to C\n");
	printf("or..\n");
	printf("2. Convert from C to F\n");

scan:
	scanf("%d", value);
	if (value == 1) {
		printf("Okey what temperature do you want to convert?\n");
		scanf("%d\n", temp);
		converted = convertF(temp);
	} else if (value == 2) {
		printf("Okey what temperature do you want to convert?\n");
		scanf("%d\n", temp);
		converted = convertC(temp);
	} else {
		printf("Now you must have type something wrong, try again!\n");
		goto scan;
	}
}


float convertC(float converted)
{
	float far;
	float cel;

	far = ((9.0 / 5.0) * cel + 32.0);

	return(far);

}

float convertF(float converted)
{
	float cel;
	float far;

	cel = ((9.0 / 5.0) * far + 32.0);

	return(cel);
}



So if someone could point it out for me that would be great! :)

Regards





the modified code:

#include <stdio.h>

float convertC2F(float);
float convertF2C(float);

int main(void)
{
	float temp,fer,cel;
	int x;
	clrscr();
	printf("Enter the Temp:");
	scanf("%f", &temp);
	printf("\n 1: Convert Cel 2 Fer");
	printf("\n 2: Convert Fer 2 Cel");
	printf("\n Enter ur option: ");
	scanf("%d", &x);
	switch(x)
	{
		case 1: fer=convertC2F(temp);
			printf("The corresponding Fer= %f", fer);
			break;

		case 2: cel=convertF2C(temp);
			printf("The corresponding Fer= %f", cel);
			break;
		case 3: exit(0);
	 }
	 getch();
	 return 0;
}


float convertC2F(float converted)
{
    float fer;
    fer = (((9 * converted) +160)/5);
    return(fer);

}

float convertF2C(float converted)
{
    float cel;
    cel = ((converted-32.0)*5)/9;
    return(cel);
}
//u have 2 increase ur knowledge of switch, function calling and calculation power..........



// if there are any confution regarding the code plz inform me,,,
Was This Post Helpful? 0
  • +
  • -

#7 redkid  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 32
  • Joined: 23-April 09

Re: Celsius to Fahrenheit converter, function error

Posted 23 April 2009 - 04:01 PM

View Postjoybhadra82, on 23 Apr, 2009 - 12:30 PM, said:

(all that)

Well, seems like you solved his problem, but hardly helped him.
Was This Post Helpful? 0
  • +
  • -

#8 Scorpiion  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 117
  • Joined: 23-April 09

Re: Celsius to Fahrenheit converter, function error

Posted 23 April 2009 - 04:06 PM

Hi again, I have rewritten my code now and now it works! :)

@joybhadra82 nice of you to give me some reference code, I did not copy any just used the info.. but some things did not work like clrscr() and getch(), since those where not included for me, or the library conio.h, I read that it was some borland specific but I'm not sure I use gcc and linux. Then the switch was not complete but maybe it was meant so that I got something to think about... :P

@redkid yeah I did but I could not really find the errors since I do not have enough knowledge, yet. But thanks for the help. :)

Here is my new working code with some added futures! :)

#include <stdio.h>

float convertC2F(float converted);
float convertF2C(float converted);

int main(void)
{
	float temp, cel, far;
	int x;
	system("clear");
	printf("Welcome to the fahrenheit/celcius converter!");
	printf("\nPlease tell me if you want:");
	printf("\n1. Convert from F to C");
	printf("\nor..");
	printf("\n2. Convert from C to F");

	printf("\nEnter you chooise ");
	scan: scanf("%d", &x);

	switch (x) {
	case 1: x = 1;
		printf("\nOkey what number do you want to want to convert?\n");
		scanf("%f", &temp);
		far = convertC2F(temp);
		printf("\nTemperature in Farenheit is: %.2f", far);
		printf("\n%.2f C is %.2f F\n", temp, far);
		break;

	case 2: x = 2;
		printf("\nOkey what number do you want to want to convert?\n");
		scanf("%f", &temp);
		cel = convertF2C(temp);
		printf("\nTemperature in Celcius is: %.2f", cel);
		printf("\n%.2f F is %.2f C\n", temp, cel);
		break;
	
	default:printf("Now you must have typed something wrong? Did you really type an 1 or 2?\n");
		goto scan;
	}

	return 0;
}

float convertC2F(float converted)
{
	float far;
	far = ((converted - 32.0) / (9.0 / 5.0));
	return(far);
}

float convertF2C(float converted)
{
	float cel;
	cel = ((9.0 / 5.0) * converted + 32.0);
	return(cel);
}



The only thing that could be different imho is that when I type in like "adfs" or "343k" or "21423" then it does into infinity loop... That's not that good but yeah as long as you type numbers it works as i should...
Was This Post Helpful? 0
  • +
  • -

#9 redkid  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 32
  • Joined: 23-April 09

Re: Celsius to Fahrenheit converter, function error

Posted 23 April 2009 - 06:30 PM

Hey Scorpiion, you are right... some of those functions are not in GCC compilers.
As for error correction...well now that you've seen how to fix these errors, maybe next time you find them, you'll fix them. :D

I'm no big programmer either, still learning, but error checking and solving those little problems by myself have helped me a lot. Of course, you ask for help when nothing else you can find works. :)

I think you have a mistake in the syntax of your switch.
switch (x) {
	case 1: x = 1;
	...
}


What you're doing is, checking if "x" has a value of 1, if so, case 1 (because that's the value of x) will be executed. But after that, you're assigning 1 to "x" again, for no reason, I understand you were doing it seeing the previous post. But this is not needed. (Good job learning the switch though! :))

What the syntax should be is:
switch (x) {
	case 1:
		printf("\nOkey what number do you want to want to convert?\n");
		//...other statements
		break;
	case 2:
		printf("\nOkey what number do you want to want to convert?\n");
		//...other statements
	default:
		printf("Now you must have typed something wrong? Did you really type an 1 or 2?\n");
		goto scan;
}


This is so because the 1 and 2 that you see next to "case" are the value of the argument (here "x") that you put in the declaration of the switch. So, to sum it up, the general syntax is:
switch (argument) {
	  case (value):
	  //statements;
	  break;
	  //...other cases
	  default:
	  //statements;
}


So now you can perfect your program :D

Was This Post Helpful? 0
  • +
  • -

#10 Scorpiion  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 117
  • Joined: 23-April 09

Re: Celsius to Fahrenheit converter, function error

Posted 24 April 2009 - 01:53 AM

Haha yeah about the switch... I changed it just after I had posted here.. :P :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1