Problem with a script I wrote

This is a discussion on Problem with a script I wrote within the C Programming forums, part of the General Programming Boards category; This is my first C script, it's a simple Temperature Conversion program. Problem: It is either with the Pointers or ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    3

    Question 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()
    {
        
    
    printf("Enter your name: ");
    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. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,200
    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
    Last edited by Shakti; 07-30-2006 at 01:43 PM.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    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. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote 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.

    Quote 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
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,200
    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
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  6. #6
    Registered User
    Join Date
    Jul 2006
    Posts
    3
    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. #7
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    It's not technically a "c script". It is a C source file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. In a game Engine...
    By Shamino in forum Game Programming
    Replies: 28
    Last Post: 02-19-2006, 10:30 AM
  3. array problem?
    By ssjnamek in forum C Programming
    Replies: 14
    Last Post: 02-08-2006, 05:03 PM
  4. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21