Inputting data from a text file.

Hi,

I'm trying to work out how to link up the numbers I have in my text file to the variables in my equation. In my text file I have 4 numbers, each on a new line. The code I have written for this so far is:

ifstream input_file("q_transinput.txt");
input_file >> Tmin >> Tmax >> Tinc >> mass;

The variables have been defined already. But I want the code to input the numbers I have in my text file to pair up with Tmin, Tmax, Tinc and the mass. This is so they can be used in the following equation:

for (double T=Tmin; T<=Tmax; T=T+Tinc) {
qT = pow((two_pi*mass*k*T/h2),1.5)*V;

If anybody could help I would be extremely grateful :)

But I want the code to input the numbers I have in my text file to pair up with Tmin, Tmax, Tinc and the mass.

I do not understand what you mean by this. What do you mean, "to pair up"?
Sorry for not being clear...
Say in my text file I have the numbers:
1
10
1
32

i want to make it so that the text file is read so that 1 = Tmin 10 = Tmax 1 = Tinc and 32 = mass
And the code you previously posted does exactly that
1
2
    ifstream input_file("q_transinput.txt");
    input_file >> Tmin >> Tmax >> Tinc >> mass;

Am I missing something here?
Oh, but when i run my output i just get a bunch of 0's.

1
2
3
4
ofstream output("qT_valuesinputresults.txt",std::ios::app);
  for (double T=Tmin; T<=Tmax; T=T+Tinc) {
    qT = pow((two_pi*mass*k*T/h2),1.5)*V;
    output << T << "\t" << qT << endl;


is there something wrong with my output?
What value is V?
Uninitialized variables may have garbage value -- but they also may be zero.

There's nothing obviously wrong (to me at least) in the code you posted.
Post some more code, maybe?
This 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
#include <math.h>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>

using namespace std;  

int main() {    
	
  const double two_pi = 2*3.14159265359;    
  double mass = 0.0;                      
  const double k = 1.3806505e-23;            
  double Tmin = 0.0;                         
  double Tmax = 0.0;                        
  double Tinc = 0.0;              	   
  const double h2 = 6.6260693e-34*6.6260693e-34;
  double V = 1.0;                        

  ifstream input_file("qtrans_input.txt");
	input_file >> Tmin >> Tmax >> Tinc >> mass;


  
  mass = mass*1.6605402e-27; // convert to SI units
  
  

  double qT = 0.0; 

  ofstream output("qT_valuesinputresults.txt",std::ios::app);
  for (double T=Tmin; T<=Tmax; T=T+Tinc) {
    qT = pow((two_pi*mass*k*T/h2),1.5)*V;
	output << T << "\t" << qT << endl; 
				      
  }

  return 0;

};
I had to reconstruct the missing code, but this works for me:
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
#include <iostream>
#include <fstream>
#include <cmath>

    using namespace std;

int main()
{
    const double two_pi = 6.2831853071795864769;
    double k  = 1;
    double h2 = 1;
    double V  = 1;

    double Tmin;
    double Tmax;
    double Tinc;
    double mass;

    ifstream input_file("input.txt");
    input_file >> Tmin >> Tmax >> Tinc >> mass;

    cout << "Tmin: " << Tmin << endl;
    cout << "Tmax: " << Tmax << endl;
    cout << "Tinc: " << Tinc << endl;
    cout << "mass: " << mass << endl;

    ofstream output("results.txt", std::ios::app);
    for (double T=Tmin; T<=Tmax; T=T+Tinc)
    {
        double qT = pow((two_pi*mass*k*T/h2),1.5)*V;
        output << T << "\t" << qT << endl;
    }

    return 0;
}

Output:
Tmin: 1
Tmax: 10
Tinc: 1
mass: 32

Input.txt:
1
10
1
32

Output.txt:
1	2850.98
2	8063.8
3	14814.1
4	22807.9
5	31875
6	41900.7
7	52801
8	64510.4
9	76976.6
10	90156
Thank you, i'm going to take a look at it all and see what I'm doing wrong... might be file location or something...
Resolved, input file was in the wrong location.
Sorry I am new to this.
Topic archived. No new replies allowed.