files problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • greek
    New Member
    • Nov 2006
    • 17

    files problem

    Hello
    Ive written a prg 4 the following question:

    Develop a piece of C++ code which opens a file called temp.txt, containing a list of
    integer values representing temperatures in the Fahrenheit scale. Your code should
    convert each temperature to its Celsius equivalent and write it to a file called celsius.txt
    containing the converted temperatures. You should give declarations for any variables
    you use.

    The formula for converting Fahrenheit to Celsius is:
    celsius = 5/9 (fahrenheit - 32)

    The programming is runnig but the problem is that the celcius.txt files contains only a list of zeros..i think that the code is not coverting the temperatures to celcius.. 'ive cout the values of temperature in celcius and farenheit on screen to check .. but the celcius temperatures remain ) !! why? can anybody help plz
    thx
  • greek
    New Member
    • Nov 2006
    • 17

    #2
    Originally posted by greek
    Hello
    Ive written a prg 4 the following question:

    Develop a piece of C++ code which opens a file called temp.txt, containing a list of
    integer values representing temperatures in the Fahrenheit scale. Your code should
    convert each temperature to its Celsius equivalent and write it to a file called celsius.txt
    containing the converted temperatures. You should give declarations for any variables
    you use.

    The formula for converting Fahrenheit to Celsius is:
    celsius = 5/9 (fahrenheit - 32)

    The programming is runnig but the problem is that the celcius.txt files contains only a list of zeros..i think that the code is not coverting the temperatures to celcius.. 'ive cout the values of temperature in celcius and farenheit on screen to check .. but the celcius temperatures remain ) !! why? can anybody help plz
    thx

    SORRY 4got to give my code
    here it is:

    #include<iostre am.h>
    #include<conio. h>
    #include<fstrea m.h>
    void main()
    {clrscr();
    float x,y;

    ifstream fin;
    fin.open("temp. txt");

    ofstream fout;
    fout.open("celc ius.txt");

    while(!fin.eof( ))
    {fin>>x;
    cout<<x<<" ";

    y=5/9*(x-32);
    fout<<y<<" ";
    }

    fin.close();
    fout.close();
    getch();
    }

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      In your calculations, try changing this statement

      Code:
      y = 5/9*(x-32);
      to

      Code:
      y = (5 / 9) * (x - 32);
      Your compiler may be subtracting 32, multiplying by nine, and then dividing 5 by that result, which would give you an incorrect answer close to 0.

      Comment

      • greek
        New Member
        • Nov 2006
        • 17

        #4
        Originally posted by Ganon11
        In your calculations, try changing this statement

        Code:
        y = 5/9*(x-32);
        to

        Code:
        y = (5 / 9) * (x - 32);
        Your compiler may be subtracting 32, multiplying by nine, and then dividing 5 by that result, which would give you an incorrect answer close to 0.

        Nope thats not the problem

        Comment

        Working...