Problem with simple C program....

This is a discussion on Problem with simple C program.... within the C Programming forums, part of the General Programming Boards category; Hello. I am not very experienced with C so this may be a simple thing to fix, but I am ...

  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
    22,256
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,256
    You're welcome. I hope you made the corresponding change to:
    Code:
    if(x == 'f' || 'F')
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,806
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 03: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, 06:57 PM

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