Two functions - One working, one not.

Hi all

I'm having an issue with my program utilizing two functions. Both functions do essentially the same thing. They're both supposed to do the calculations and one is supposed to print the results to the output file WITHOUT the header information and one prints the results to a second output file WITH the header information I want.

The first function works flawlessly and prints just the way I want it to.

The second function, however, isnt acting the way I want it to. It doesnt run through the calculations it seems. It will print an output file with the header information I want, but without the results below it.

What am I doing wrong here? Here is my code:
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
 #include<iostream>
 #include<fstream>
 #include<iomanip>
 #include<cmath>
 using namespace std;
 
 const double e=2.71828183, c1=374200000, c2=14390;
 double E, frac, wave_start, wave_final, wave_inc, temp;
 
 double for_loop_dirty();
 double for_loop_clean();

 int main ()
 {
     cout<<"Please enter a starting value for the wavelength range: ";
     cin>>wave_start;
     cout<<"Please enter an ending value for the wavelength range: ";
     cin>>wave_final;
     cout<<"Please enter a value for the wavelength increment: ";
     cin>>wave_inc;
     cout<<"Please enter a value for temperature in Kelvin: ";
     cin>>temp;
     
    for_loop_clean();
    for_loop_dirty();
    
system("pause");
return(0);
}

double for_loop_clean()
{
     string filename;
     ofstream outfile;
     cout<<"What is the name of the clean output file? (be sure to include extension)";
     cin>>filename;
     outfile.open(filename.c_str());
     
     cout<<"\n\t\t  Energy report for T = "<<temp<<endl;
     cout<<"\n\t\t Wavelength";
     cout<<"\t\t  E "<<endl;
     cout<<"\t\t ---------- \t\t ---"<<endl;  
   
     for (wave_start; wave_final>=wave_start; wave_start=wave_start+wave_inc)
     {
       frac=(c2/(wave_start*temp));
       E=c1/((pow(wave_start,5))*((pow(e,frac))-1));
       if (E>0.0001)
       {
          cout<<"\t\t    "<<setw(4)<<wave_start<<"\t\t      "<<E<<endl;
          outfile<<"\t\t    "<<setw(4)<<wave_start<<"\t\t      "<<E<<endl;
       }
       else
       {
       }
     }
     
     outfile.close();
}

double for_loop_dirty()
{
     string filename2;
     ofstream outfile2;
     cout<<"What is the name of the dirty output file? (be sure to include extension)";
     cin>>filename2;
     outfile2.open(filename2.c_str());
     
     outfile2<<"\n\t\t  Energy report for T = "<<temp<<endl;
     outfile2<<"\n\t\t Wavelength";
     outfile2<<"\t\t  E "<<endl;
     outfile2<<"\t\t ---------- \t\t ---"<<endl;
      
      for (wave_start; wave_final>=wave_start; wave_start=wave_start+wave_inc)
     {
       frac=(c2/(wave_start*temp));
       E=c1/((pow(wave_start,5))*((pow(e,frac))-1));
       if (E>0.0001)
       {
          cout<<"\t\t    "<<setw(4)<<wave_start<<"\t\t      "<<E<<endl;
          outfile2<<"\t\t    "<<setw(4)<<wave_start<<"\t\t      "<<E<<endl;
       }
       else
       {
       }
     }
     
     outfile2.close(); 
}


I know that there is probably an easier way to go about what Im trying to accomplish, but I'd rather keep it in this format for now. However, if what I'm trying to do requires me to change alot, then I am open to that as well. Thanks!
You need to reset wave_start to the value you put in within main() before running the "dirty" function.

Nice name, btw.

-Sinusoidal Albatross
Last edited on
Not totally sure what you meant by that. Regardless, I have figured out a way to make it work. Here is my code as it stands 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
 #include<iostream>
 #include<fstream>
 #include<iomanip>
 #include<cmath>
 using namespace std;
 
 const double e=2.71828183, c1=374200000, c2=14390;
 double E, frac, wave_start, wave_final, wave_inc, temp;
 char h_or_n;
 
 double for_loop_dirty();
 double for_loop_clean();

 int main ()
 {
     cout<<"Please enter a starting value for the wavelength range: ";
     cin>>wave_start;
     cout<<"Please enter an ending value for the wavelength range: ";
     cin>>wave_final;
     cout<<"Please enter a value for the wavelength increment: ";
     cin>>wave_inc;
     cout<<"Please enter a value for temperature in Kelvin: ";
     cin>>temp;
     cout<<"Would you like to creat an outfile with (h) or without (n) a header?:";
     cin>>h_or_n;
     
    if (h_or_n=='n' || h_or_n=='N')
    {
         for_loop_clean();
    }
    else if (h_or_n=='h' || h_or_n=='H')
    {
         for_loop_dirty();
    }
    else
    {
         cout<<"You have not entered one of the two options. Please try again"<<endl;
    }
    
system("pause");
return(0);
}

double for_loop_clean()
{
     string filename;
     ofstream outfile;
     cout<<"What is the name of the clean output file? (be sure to include extension)";
     cin>>filename;
     outfile.open(filename.c_str());
     
     cout<<"\n\t\t  Energy report for T = "<<temp<<endl;
     cout<<"\n\t\t Wavelength";
     cout<<"\t\t  E "<<endl;
     cout<<"\t\t ---------- \t\t ---"<<endl;  
   
     for (wave_start; wave_final>=wave_start; wave_start=wave_start+wave_inc)
     {
       frac=(c2/(wave_start*temp));
       E=c1/((pow(wave_start,5))*((pow(e,frac))-1));
       if (E>0.0001)
       {
          cout<<"\t\t    "<<setw(4)<<wave_start<<"\t\t      "<<E<<endl;
          outfile<<"\t\t    "<<setw(4)<<wave_start<<"\t\t      "<<E<<endl;
       }
       else
       {
       }
     }
     
     outfile.close();
}

double for_loop_dirty()
{
     string filename2;
     ofstream outfile2;
     cout<<"What is the name of the dirty output file? (be sure to include extension)";
     cin>>filename2;
     outfile2.open(filename2.c_str());
     
     cout<<"\n\t\t  Energy report for T = "<<temp<<endl;
     cout<<"\n\t\t Wavelength";
     cout<<"\t\t  E "<<endl;
     cout<<"\t\t ---------- \t\t ---"<<endl; 
     
     outfile2<<"\n\t\t  Energy report for T = "<<temp<<endl;
     outfile2<<"\n\t\t Wavelength";
     outfile2<<"\t\t  E "<<endl;
     outfile2<<"\t\t ---------- \t\t ---"<<endl;
      
      for (wave_start; wave_final>=wave_start; wave_start=wave_start+wave_inc)
     {
       frac=(c2/(wave_start*temp));
       E=c1/((pow(wave_start,5))*((pow(e,frac))-1));
       if (E>0.0001)
       {
          cout<<"\t\t    "<<setw(4)<<wave_start<<"\t\t      "<<E<<endl;
          outfile2<<"\t\t    "<<setw(4)<<wave_start<<"\t\t      "<<E<<endl;
       }
       else
       {
       }
     }
     
     outfile2.close(); 
}


This works quite well for my purposes. I have one small problem, though. I am not totally sure how to let the user input one of the two options (h or n) again if he/she didnt input a correct choice (ex: lets say he inputs 'f' instead. how do I let him choose again?). Thanks!
Last edited on
The dirty method:
Use goto. This will certainly make quite a few of the elites in this forum and maybe also myself cry.

The clean method:
Put this whole thing in a while loop, and have your functions return some value if they completed successfully. If that value is found in the while loop in a certain variable, terminate the loop using break.

EDIT2: I hope you could understand that.

EDIT: Check my post. I edited it, explaining how you can use both functions if you want to.

-Albatross
Last edited on
Thanks for the input (and the compliment, btw). Seeing as this is an important project and I'd like it to be as clean as possible, I'll give the while loop option a try. I'll let you know if I run into any problems!
Topic archived. No new replies allowed.