Thread: I'm new to progamming. I'm not a student. not hom Just tring to see what i did wrong!

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    1

    Talking I'm new to progamming. I'm not a student. not hom Just tring to see what i did wrong!

    This is my code and i dont know what i did wrong.
    When I run it; It ask for a number the it says the result is celcius.
    If any one can please do. Feel free to try the code your self

    [html]
    [body]
    [tag]
    Code:
    //
    // Conversion - Program to convert temperature from
    //              Fahrenheit degreees into Celcius:
    //              Fahrenheit = Celcius  9/5 + 32 =
    //
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;int main (int nNumberofargs, char* pszArgs [] )
    {
      // enter the tempature in Fahrenheit
      int Fahrenheit;
      cout << "Enter the tepature in Fahrenheit:";
      cin >> Fahrenheit;
      // Use conversion factor for Fahrenheit
      // to Celcius
      int factor;
      factor = 5 + 32 ;
      // use conversion factor to convert Fahrenheit
      // into Celcius values
      int fahrenheit;
      fahrenheit = factor * Fahrenheit/5+32;
      // output the results (followed "by a Newline)
      cout << "Celcius value is:" ;
      cout >> celcius << endl;
      // wait until user is read before terminating program
      // to allow the user see the program results
      system("PAUSE");
      return 0;
     }
    [/tag]
    [/body]
    [/html]
    Attached Files Attached Files

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    No that's okay, we don't need to try your code. If I couldn't understand exactly what it did by just looking at it then I would not consider myself qualified to help you with it.

    What you are meant to do is explain what the problem is and post the exact compile errors.

    I can see at a glance that you haven't declared a variable called celcius. Instead you have declared two variables called fahrenheit, one with a capital F. You do not need two of them.
    You also have your calculation wrong. You seem to want to convert from F -> C but the formula in your comments is for converting back the other way. You need to apply algebra to rearrange the formula to Celcius = (Fahrenheit - 32) * 5 / 9. You do not need any variable called factor at all.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You will also not get very good results using integer math when doing the conversions. When all your values are integer values any fractional value is truncated. Example 1/2 will evaluate to zero, not .5. There are no fractions when using integer math. You should probably change your variables to a floating point type and insure all the constants are floating point by using a decimal point, example 1.0/2.0 would yield a floating point value (.5). I would suggest you use the type double.


    Jim

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > cout >> celcius << endl;
    Consistency in >> or << would help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    This code is an approximation of an example in the C++ for dummies book, I suggest you properly follow the code in there, the explanation and guide to exercise in the book, though it is possible you found it elsewhere i suppose.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new student
    By monu in forum C Programming
    Replies: 2
    Last Post: 07-25-2007, 12:47 AM
  2. error when tring to run program
    By hopeolicious in forum C++ Programming
    Replies: 3
    Last Post: 02-28-2005, 01:06 AM
  3. tring to sort file along with converting it from char to int
    By tony24tone in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2004, 06:39 AM
  4. new to c progamming any one can help
    By manalidris in forum C Programming
    Replies: 1
    Last Post: 05-13-2002, 05:05 AM
  5. tring again..
    By xlordt in forum C Programming
    Replies: 6
    Last Post: 02-17-2002, 05:01 PM