Thread: Problem with simple C program....

  1. #1
    Registered User OC2233's Avatar
    Join Date
    Sep 2007
    Posts
    2

    Problem with simple C program....

    Hello. I am not very experienced with C so this may be a simple thing to fix, but I am writing a C program that converts an inputted temperature to either Fahrenheit or Centigrade. The program compiles, but when it runs, it simply converts the temp. only to Centigrade, never Fahrenheit. If anyone can see where my problem is I'd greatly appreciate it.

    Here is the code that I have:


    Code:
    #include <stdio.h>
    
       int c2f(int);
       int f2c(int);
      
       main()
       {
      
                int temp1=0;
                char x;
      
                while(temp1 != -99){
      
                        printf("Please input a temperature (-99 to exit):\n");
                        scanf("%d", &temp1);
      
                        if(temp1 != -99){
                                printf("Would you like to convert to Centigrade or Fahrenheit (C or F)\n");
                                scanf("%1s", &x);
        
                                if(x == 'c' || 'C')
                                        printf("The temperature in Centigrade is: %d\n\n", f2c(temp1));
                                else{
                                        if(x == 'f' || 'F')
                                                printf("The temperature in Fahrenheit is: %d\n\n", c2f(temp1));
                                        else
                                                printf("Invalid Input\n");
                                }
                        }
                }
        }
        
        int f2c(int x)
        {
                int y;
        
                y=(x-32) * 5 / 9;
                return y;
        }
        
        int c2f(int x)
        {
                int y;
        
                y=(x*9/5) + 32;
                return y;
        }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that you want:
    Code:
    if(x == 'c' || 'C')
    To be:
    Code:
    if(x == 'c' || x == 'C')
    Basically, 'C' is a non-zero number, and thus in a boolean context 'C' is treated as a true value. As such, your original expression always evaluates to true.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User OC2233's Avatar
    Join Date
    Sep 2007
    Posts
    2
    Awesome, thanks for your help! Program works perfectly now.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You're welcome. I hope you made the corresponding change to:
    Code:
    if(x == 'f' || 'F')
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You should probably also switch to using doubles instead of integers and to using floating-point division instead of integer division.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    scanf("&#37;1s", &x);
    scanf will put a nul-character into the string - you you do not have the space for it. Use %c format for reading one char

    do not forget to specify the return value of the main explicetely - it is int main
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  3. Running Program Problem
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2007, 02:02 PM
  4. simple login program problem
    By suckss in forum C Programming
    Replies: 11
    Last Post: 11-11-2006, 05:02 PM
  5. Problem with a simple program
    By Salgat in forum C Programming
    Replies: 10
    Last Post: 06-15-2006, 05:57 PM