Thread: Problem with a script I wrote

1. Problem with a script I wrote

This is my first C script, it's a simple Temperature Conversion program.

Problem: It is either with the Pointers or the way I wrote the Order of Operations in the temperature conversion formulas. The program crashes when it attempts to calculate the answer.

Could someone please explain to me what the exact problem is, and explain how to fix this so I avoid making the same mistake in the future?

Code:
#include <stdio.h>

char name[15];
char choice;
int numbercelcius, numberfahreinheit, *pointerc, *pointerf;

int main()
{

scanf("%s",name);
printf("Hello, %s. This program was written in C.\n",name);
printf("It's purpose is to convert fahreinheit to celcius, and vice versa.\n");
while (1==1)
{
printf("So, %s... what would you like to convert?\n",name);
printf("Type '0' for Celcius to Fahreinheit\n");
printf("Type '1' for Fahreinheit to Celcius\n");
scanf("%d",&choice);
if ( choice == 0 )
{
printf("Type in the temperature Celcius: ");
scanf("%d",&numbercelcius);
numbercelcius = *pointerc;
printf("The temperature is ( *pointerc + 32 ) * ( 9 / 5 ) Fahreinheit.\n\n");
}
else if ( choice == 1 )
{
printf("Type in the temperature Fahreinheit: ");
scanf("%d",&numberfahreinheit);
numberfahreinheit = *pointerf;
printf("The temperature is ( *pointerf - 32 ) * ( 5 / 9 ) Celcius.\n\n");
}

}
return 0;
}

2. numbercelcius = *pointerc;

Ouch ouch ouch, I only skimmed the code briefly but to me it looks like you dereference an unintialized pointer, which basicly means you try to access memory you dont own, which is a very bad thing and is the most likely cause of the crash.

This line will probably not work as you intended it to work:
printf("The temperature is ( *pointerc + 32 ) * ( 9 / 5 ) Fahreinheit.\n\n");

Try with
printf("The temperature is %f\n\n", ((float)numbercelcius + 32.0f)*(9.0f/5.0f));

same with pointerf later on in the code and the fahrenheit conversion.

Now the big question to me is why do you use pointers?? Why not use the integer in the first place. And why do you use global variables??

And my last question is, if this is your first C-program, why are you dealing with pointers and things like that, that for a beginner is an advanced topic. I suggest you go through the tutorials at this site, starting with the first one and then working your way through them.

Edit: made a silly stupid typo

3. Haha, scary isn't it?

Thanks for the advice, I removed the pointers and the program works now. How stupid of me...I can't believe I forgot the % sign.

To answer your questions, I have a bad habit of biting off more than I can chew. It's easier for me to remember what I learn if I work problems out rather than remember what I read from a manual ( sounds like suicide, I know ), so in an attempt to understand pointers, I tried to implement them into my script. I used global variables because they seemed less confusing to me -- they are known throughout the entire program...is it faster to use local variables?

4. Originally Posted by Shakti
numbercelcius = *pointerc;

Ouch ouch ouch, I only skimmed the code briefly but to me it looks like you dereference an unintialized pointer, which basicly means you try to access memory you dont own, which is a very good thing and is the most likely cause of the crash.
Actually, being a "global" it is initialized to a null pointer. Of course, dereferencing this null pointer is very certainly bad.

Originally Posted by Shakti
And my last question is, if this is your first C-program, why are you dealing with pointers and things like that, that for a beginner is an advanced topic. I suggest you go through the tutorials at this site, starting with the first one and then working your way through them.
Seconded. My take at user input:
User Input: Strings and Numbers [C]
Code:
while (1==1)
http://c-faq.com/bool/booltype.html
http://c-faq.com/bool/bool2.html

5. Do not take bigger bites than you can chew. That indeed is suicide when you get into more advanced stuff. My advice still stands, go to www.cprogramming.com and learn from the tutorials there, start with the first one and work your way down. And for future reference, this is not a script. This is a program, it gets compiled into machinecode.

Now about the global vs local variables. By having local variables it is easier to keep track of the variables. In big programs if you use global variables only it will be near impossible to do anything, because you will soon run out of variablenames, and when you run out of good names you have to start making stuff like
int variableToCheckToSeeIfSomethingIsWrongInFunctionFo obar
(extreme case but still a valid reason) up.

Edit: Thanks for correcting me about the pointer pointing to an unknown location

6. Thanks Dave_Sinkula and Shakti. I'll take a look at all the guides you two recommended, I'm inspired to get better at this.

I appreciate the help.

7. It's not technically a "c script". It is a C source file.