I need help with my program. The program reads water temperatures and when they were recorded from an input file called " biodata2.txt." Then the output is displayed in a file called "results.txt" So the input file looks like this:
2 // number of readings taken
200707211245 // date recorded
F70.5 // Fahrenheit temperature
200708220812// date recorded
C19.97// Celsius temperature
So, the program rearranges the data to look like this:
Number of Recordings found: 2
C19.97-- recorded on 08/21/2007 at 08:12, etc. for any other data records.
I am also supposed to convert all Fahrenheit readings to Celsius. This is were the problem is, I read in the temperature in Fahrenheit using "inFile >> temp" and input it into the formula with result being "celsius" variable. The out put I get is -0 instead of the correct answer. Can anyone help me?

Maybe post your formula for converting F to C?

You didn't do 5/9 by any chance, which would be 0
As opposed to say 5.0/9.0 which is nearer to what you want.

Sorry forgot to post code:

[# include <iostream>
#include <fstream>
# include <string>

using namespace std;

int main(){
    
    ifstream inFile;
    string num_data;
    string record_time;
    double temp; 
    inFile.open("biodata2.txt");
    
    // test to see if file exists
    if(inFile)
       cout << "Opened File" << endl;
    else
      cout << "File not found" << endl;
    
    // reads how many data recordings exist in file  
    inFile >> num_data;
    cout << "Number of Recordings Found: " << num_data << endl;
    
    inFile >> record_time;
    string year = record_time.substr (0,4);
    string month = record_time.substr (2,2);
    string day = record_time.substr (6,2);
    string hour = record_time.substr (8,2);
    string minutes = record_time.substr (10,2);
    cout << "21.38C -- recorded on " << month << "/" << day << "/" << year 
    << " at " << hour << ":" << minutes << endl;
    
    //converts Fahrenheit temps in file to Celsius 
    inFile >> temp;
    double celcius = (9/5)*(temp-32);
    cout << celcius << endl;

0utput for celsius: -32

Error on conversion:

Sorry classic error:

int a=9;
int b=5;
 // this gives 1
std::cout<<"Int division "<<a/b<<std::end;

The code you have written gives the error because of 9/5 == 1.
If you write 9.0/5.0 or even 9*(temp-32)/5 you would have the correct answer.

[p.s. Sorry Salem was quicker to posting than me (and he correctly predicted the answer as well!)]

I tested the formula with (9.0/5.0); (5.0/9.0) and 9*(temp-32)/5. I still get -32 and more negative numbers for the Celsius temp. So, is the formula not right or maybe the data types I am using are wrong??

You really need to work on your order of operations.

You need to do the subtraction first, then divide that number by the constant of 9/5(1.8); not 1.8 divided by the difference.

Go to the library and check out a book on Algebra, then do a few brief reviews to see where you need to touch up on(if it's been awhile since you've used any). Programming follows many of these little laws in math, so keep them in mind, or write it down on a card/note-pad for reference.

Ok, Mosaic like this:celcius = (temp-32)/ 1.8. I am also getting a compile error: invalid operands of types `double(whatever data type I am using)' and `<unknown type>' to binary `operator<<.
So, it appears I am using the wrong data types for "temp" and "celsius". What do you think it is?

You said the temperature in the file is in the format
C23.4
F74.7

Where did you read the C or F?
Where did you test the C of F to decide if you need to convert?

Here is my code again:

# include <iostream>
#include <fstream>
# include <string>

using namespace std;

int main(){
    
    ifstream inFile;
    string num_data;
    string record_time;
    float temp; 
    inFile.open("biodata2.txt");
    
    // test to see if file exists
    if(inFile)
       cout << "Opened File" << endl;
    else
      cout << "File not found" << endl;
    
    // reads how many data recordings exist in file  
    inFile >> num_data;
    cout << "Number of Recordings Found: " << num_data << endl;
    
    inFile >> record_time;
    string year = record_time.substr (0,4);
    string month = record_time.substr (2,2);
    string day = record_time.substr (6,2);
    string hour = record_time.substr (8,2);
    string minutes = record_time.substr (10,2);
    cout << "21.38C -- recorded on " << month << "/" << day << "/" << year 
    << " at " << hour << ":" << minutes << endl;
    
    //converts Fahrenheit temps in file to Celsius 
    inFile >> temp;
    float celcius = (temp-32)/ 1.8 << endl;
    cout << celcius << endl;
 error: invalid operands of types `double' and `<unknown type>' to binary `operator<<'

The input file looks like this:2
200707211245 // date recorded
F70.5 // Fahrenheit temperature
200708220812 // date recorded
C19.97 // Celsius temperature
I had to arrange it this way because I can't use "substr" to find the temperature if it is on the same line as the date recorded.
So, anyone, help??

> float celcius = (temp-32)/ 1.8 << endl;
So what's the endl doing at the end of this calculation?

I guess "endl" has nothing to do with the program. I changed the code and inputed a while loop. So, I know what data type "temp" is but I don't know what data type to use for "celsius" variable(or whatever you call it). I am now getting an error message saying: no match for 'operator-' in 'temp - 32'. What does that mean? I really need to get this program done. :sweat:

# include <iostream>
#include <fstream>
# include <string>

using namespace std;

int main(){
    
    fstream inFile;
    string num_data;
    string record_time;
    string temp; 
    inFile.open("biodata2.txt");
    
    // test to see if file exists
    if(inFile)
       cout << "Opened File" << endl;
    else
      cout << "File not found" << endl;
    
    // reads how many data recordings exist in file  
    inFile >> num_data;
    cout << "Number of Recordings Found: " << num_data << endl;
    
    inFile >> record_time;
    string year = record_time.substr (0,4);
    string month = record_time.substr (2,2);
    string day = record_time.substr (6,2);
    string hour = record_time.substr (8,2);
    string minutes = record_time.substr (10,2);
    cout << "21.38C -- recorded on " << month << "/" << day << "/" << year 
    << " at " << hour << ":" << minutes << endl;
    
    //converts Fahrenheit temps in file to Celsius 
    inFile >> temp;
    while (inFile){
          float celsius = (temp - 32)/(1.8);
          cout << celsius << endl;

Well you did input temp as a string (this time), and not as a float (like last time).

Focus on what's being said, and stop making a bunch of random changes each time.

Ok, I won't change the data type for "temp" (stays a string) anymore. I just want to know how I can fix the error for the formula that converts Fahrenheit temperatures to Celsius temperatures. The error I get is:

no match for 'operator-' in 'temp - 32'

I will leave the "celsius" variable without a data type for confusion purposes:

# include <iostream>
#include <fstream>
# include <string>

using namespace std;

int main(){
    
    fstream inFile;
    string num_data;
    string record_time;
    string temp; 
    inFile.open("biodata2.txt");
    
    // test to see if file exists
    if(inFile)
       cout << "Opened File" << endl;
    else
      cout << "File not found" << endl;
    
    // reads how many data recordings exist in file  
    inFile >> num_data;
    cout << "Number of Recordings Found: " << num_data << endl;
    
    inFile >> record_time;
    string year = record_time.substr (0,4);
    string month = record_time.substr (2,2);
    string day = record_time.substr (6,2);
    string hour = record_time.substr (8,2);
    string minutes = record_time.substr (10,2);
    cout << "21.38C -- recorded on " << month << "/" << day << "/" << year 
    << " at " << hour << ":" << minutes << endl;
    
    //converts Fahrenheit temps in file to Celsius 
    inFile >> temp;
    celsius = (temp - 32)/(1.8);
    cout << celsius << endl;

So, if you can help me then go right ahead?

USE CODE BELOW
celsius=(5.00/9.00)*(fahrenheit-32)
And celsius and fahrenheit should be "float".

Formula to convert celsius into Fahrenheit is as below.
celsius = (5/9)*(temp-32);
Use float datatype for celsius & temp variables.

This question has already been answered. Start a new discussion instead.