Trying to convert from Celsius to Fahrenheit and then the other way

So I am trying to write a code that will allow the user with three options (figured that part out)

I am trying to base this off an example in my book. The hardest part I am having with just getting it to work is the problem I am having with the "Programer defined function" (noted in the comments in the code) no matter what I try to change it still comes up with errors and since I basically know nothing about programming I cannot trouble shoot at all.

The next step when this part is finished is to get the results to print out on their own new file. (yay :( )

Note: I am on xcode but the system("pause") and one or two other things are in there because my teacher needs it on his computer to make it work.

Thanks in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  #include <iostream>
#include <fstream>
#include <string>
using namespace std;
double celsiusToFahr(double celsius);
double FahrToCelsius(double fahrenheit);
void myMenu ()
{
    cout << "\n-------------------------------------------\nPlease Choose an option below\n"<<endl;
    cout<< "Enter 1 to Convert from Celsius to Fahrenheit\n" <<endl;
    cout<< "Enter 2 to Convert from Fahrenheit to Celsius\n"<<endl;
    cout<< "Enter 3 to Exit\n" <<endl;
    
}
int main()


{
int xin;
    myMenu();
    cin>>xin;
    system("CLS");

while (xin!=3)
{ if (xin==1)
{cout << "Celsius to Fahrenheit is initiated\n";
        ifstream fin;
        ofstream fout;
        string filename;
        double cels, fahr;;
        cout << "Enter File Name";
        cin>>filename;
        fin.open(filename.c_str());
        if(fin.fail())
        {cerr<< "could not open file"<<endl;}
        
        fout.open((filename + "ToFahr").c_str());
        fin>>cels;
        
        while (!fin.eof())
        {fahr=celsiusToFahr(cels);
            fout<<fahr<<endl;
            fin>>cels;}
        fin.close();
        fout.close();
        return 0;
    }
    double celsiusToFahr(double celsius);
    {double temp;
        temp=(9.5/5.0)*celsius+32;
        return temp;}
  
else if (xin==2)
    
{cout << "Fahrenheit to Celsius is initiated";
    ifstream in;
    ofstream out;
    string filename;
    double cels, fahr;;
    cout << "Enter File Name";
    cin>>filename;
    in.open(filename.c_str());
    if(in.fail())
    {cerr<< "could not open file"<<endl;}
    
    fout.open((filename + "ToCelsius").c_str());
    fin>>cels;
    
    while (!in.eof())
    {celsius=FahrToCelsius(cels);
        out<<celsius<<endl;
        in>>cels;}
    in.close();
    out.close();
    return 0;
}
    
    FahrToCelsius(double fahrenheit);
    {double tempc;
        tempc=(5.0/9.5)*fahrenheit-32;
        return tempc;}
    myMenu();
    
    cin>>xin;
    
    system("CLS");}

else
{cout << "Invalid entry. Please choose Option 1,2, or 3.";
    
    myMenu();
    
    cin>>xin;
    
    system("CLS");}





cout << "Exiting from the Program";

system("pause");

return 0;



}
I am having with just getting it to work is the problem I am having with the "Programer defined function" (noted in the comments in the code)

I see no comments in the code.

line 48-51. Several problems here.
1) You can't nest a function inside a function.
2) The ; on line 48 makes this a function prototype, not a function header.
3) Line 50: your formula is incorrect.
Move these lines in front of line 15.

Lines 78-81:
1) Ditto re nested function.
2) Ditto ; on line 78
3) Ditto regarding your formula being incorrect.
4) Line 78 should have a return type. i.e. It must match the prototype at line 6.

Other comments:
Line 35: If you can't open the file, you continue anyway.
Line 64: Ditto
Lines 82-86: These lines seem out of place. How can you even reach them?
Lines 40,69: Not a good idea to use ! eof() as your condition. Won't catch fail conditions. Better to use while (in)

Your code would have been clearer and easier to read if lines 26-47 and 55-76 were separate functions. e.g. ConvertFileCtoF() and ConvertFileFtoC ().




I really do not understand how to use the prototype or like how to right a function then call back on that function. Any help in that area?
This is what I have now


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
double celsiusToFahr(double celsius);
double FahrToCelsius(double fahrenheit);
void myMenu ()
{
    cout << "\n-------------------------------------------\nPlease Choose an option below\n"<<endl;
    cout<< "Enter 1 to Convert from Celsius to Fahrenheit\n" <<endl;
    cout<< "Enter 2 to Convert from Fahrenheit to Celsius\n"<<endl;
    cout<< "Enter 3 to Exit\n" <<endl;
    
}
double myc2f(double celsius)
{double temp;
    temp=(9.5/5.0)*(celsius+32);
    return temp;}

double myf2c(double fahrenheit);
{ double tempc; // here it keeps saying "expect unqualified identifier"
    tempc=(5.0/9.5)*fahrenheit-32;
    return tempc;}

int main()



{
int xin;
    myMenu();
    cin>>xin;
    system("CLS");// required for teachers computer

while (xin!=3)
{ if (xin==1)
{cout << "Celsius to Fahrenheit is initiated\n";
        ifstream fin;
        ofstream fout;
        string filename;
        double cels, fahr;;
        cout << "Enter File Name";
        cin>>filename;
        fin.open(filename.c_str());
        if(fin.fail())
        {cerr<< "could not open file"<<endl;}
        
        fout.open((filename + "ToFahr").c_str());
        fin>>cels;
        
        while (!fin.eof())
        {fahr=myc2f(cels);
            fout<<fahr<<endl;
            fin>>cels;}
        fin.close();
        fout.close();
    
    myMenu();
    cin>>xin;
    system("CLS");// required for teachers computer
}

   
  
else if (xin==2)
    
{ cout << "Fahrenheit to Celsius is initiated";
    ifstream in;
    ofstream out;
    string filename;
    double finbar, Celsius;;
    cout << "Enter File Name";
    cin>>filename;
    in.open(filename.c_str());
    if(in.fail())
    {cerr<< "could not open file"<<endl;}
    
    out.open((filename + "ToCelsius").c_str());
    in>>finbar;
    
    while (!in.eof())
    {
        Celsius=myf2c(finbar);
        out<<Celsius<<endl;
        in>>finbar;}
    in.close();
    out.close();
    
    myMenu();
    
    cin>>xin;
    
    system("CLS");}

else // it says "expected expression"
    {cout << "Invalid entry. Please choose Option 1,2, or 3.";
        
        myMenu();
        
        cin>>xin;
        
        system("CLS");// required for teachers computer
    }




cout << "Exiting from the Program";

system("pause");

return 0;



}
}
Last edited on
Line 20: You still haven't removed the ; from the end of the line. You must do so.

Line 15,20: Because these two function deinitions precede where they are called, there is no need for them to have function prototypes.

Consider if you had placed these two functions after line 117. When the compiler reaches lines 52 or 83, it does not know what the function signature is for these two functions. That is why we use function prototypes. A function prototype signature must match the function defintion signature exactly.

Your two function prototypes at lines 5 and 6 are useless because you have no function definitions by those names.
So how do I get the program to call those statements? Or is there a way to put it in the if loop not using function prototypes?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
double myc2f(double celsius)
{double temp;
    temp=(9.5/5.0)*(celsius+32);
    return temp;}

double myf2c(double fahrenheit)
{ double tempc;
    tempc=(5.0/9.5)*fahrenheit-32;
    return tempc;}
void myMenu ()
{
    cout << "\n-------------------------------------------\nPlease Choose an option below\n"<<endl;
    cout<< "Enter 1 to Convert from Celsius to Fahrenheit\n" <<endl;
    cout<< "Enter 2 to Convert from Fahrenheit to Celsius\n"<<endl;
    cout<< "Enter 3 to Exit\n" <<endl;
    
}


int main()



{
int xin;
    myMenu();
    cin>>xin;
    system("CLS");// required for teachers computer

while (xin!=3)
{ if (xin==1)
{cout << "Celsius to Fahrenheit is initiated\n";
        ifstream fin;
        ofstream fout;
        string filename;
        double celsius, fahr;;
        cout << "Enter File Name";
        cin>>filename;
        fin.open(filename.c_str());
        if(fin.fail())
        {cerr<< "could not open file"<<endl;}
        
        fout.open((filename + "ToFahr").c_str());
        fin>>celsius;
        
        while (!fin.eof())
        {fahr=myc2f(celsius);
            fout<<fahr<<endl;
            fin>>celsius;}
    ofstream Lawrence_fahrenheit_output;
    Lawrence_fahrenheit_output.open("Lawrence_fahrenheit_output.txt");
         Lawrence_fahrenheit_output << temp <<endl // Here is where I need it to call back on 
                                               //the function and spit out all the variables now in fahrenheit
        Lawrence_fahrenheit_output.close();
        fin.close();
        fout.close();
    
    myMenu();
    cin>>xin;
    system("CLS");// required for teachers computer
}

   
  
else if (xin==2)
    
{ cout << "Fahrenheit to Celsius is initiated";
    ifstream in;
    ofstream out;
    string filename;
    double fahrenheit, Celsius;;
    cout << "Enter File Name";
    cin>>filename;
    in.open(filename.c_str());
    if(in.fail())
    {cerr<< "could not open file"<<endl;}
    
    out.open((filename + "ToCelsius").c_str());
    in>>fahrenheit;
    
    while (!in.eof())
    {
        Celsius=myf2c(fahrenheit);
        out<<Celsius<<endl;
        in>>fahrenheit;}
    ofstream Lawrence_celsius_output;
    Lawrence_celsius_output.open("Lawrence_celsius_output.txt");
    Lawrence_celsius_output << tempc <<endl
    Lawrence_fcelsius_output.close();
    in.close();
    out.close();
    
    myMenu();
    
    cin>>xin;
    
    system("CLS");}

else // it says "expected expression"
    {cout << "Invalid entry. Please choose Option 1,2, or 3.";
        
        myMenu();
        
        cin>>xin;
        
        system("CLS");// required for teachers computer
    }




cout << "Exiting from the Program";

system("pause");

return 0;



}
}
When the user clicks option 1
I need to open the document "data_celsius.rtf" and convert the numbers to Fahrenheit and use those numbers to create a document called Lawrence_Fahrenheit_output.txt
When the user clicks option 2
I need to open the document "data_fahrenheit.rtf" and convert the numbers to Fahrenheit and use those numbers to create a document called Lawrence_celsius_output.txt

However, I really do not know how to get the numbers from the data files and put them into a function. That is my biggest problem Any suggestions on how to do that? If I can figure out how to do that I can create the if/while statements around it
Lines 51-53: You loop through the file reading celsius values. You convert the celsius to fahr one by one. When you exit the loop, fahr contains only the last value.

Line 56: You're writing temp to the output file. temp does not appear to have been declared. If you want to write each converted temp to the output file, you're going to need to do that within the previous loop (51-53) just like you're writing it to cout.

Lines 85-89: Ditto 53-56.




I do not understand, but I think its mostly because I do not know what command I can use to get it to do convert every number and keep every number. The temp is what it should save it as after going through the functions

double myc2f(double celsius)
{double temp;
temp=(9.5/5.0)*(celsius+32);
return temp;}

double myf2c(double fahrenheit)
{ double tempc;
tempc=(5.0/9.5)*fahrenheit-32;
return tempc;}
temp and tempc in the two above functions are local variables. They go out of scope (no longer exist) when myc2f and myf2c exit.

In your previous post, you opening two output files (per conversion).
One at line 47, and another one at line 55. Why are you openeing two output files? You should be writing to the first one just fine. It's the second one that is problematic. At line 58, you're expecting temp to be meaningful and it is not.


I need the output file to be labeled Lawrence_celsius_output.txt and Lawrence_fahrenheit_output.txt the other part in 47 and 55 is just what I was trying from an example in my book. I cannot even get this code to run :(
Topic archived. No new replies allowed.