Pass a .txt File Into a Vector

For my class, we need to read in values from a .txt file and input them into vectors to solve for a value. In my case, I read in values for grams, molecular weight, and liters of solvent to solve for molality. I have been able to read in a .txt file and complete a calculation, however, the program only solves for one line when I need it to solve for all of them.

.txt file

grams,molecular weight,Liters Solvent
100,50,2
35,100,3
765,86.7,3
1011,56.7,2

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
#include<string>
#include<vector>
#include<sstream>
#include<iostream>
#include<stdlib.h>
#include<fstream>

using namespace std;


int main()
{
	
	float f;
	
	//declaring vectors
	vector<float> molalVector;

	vector<float>  molsVector;

	vector<float> gramsVector;

	vector<float> molVector;

	vector<float> litVector;

	//declaring the string to read in lines
	string strLine;

	ifstream myfile("../results.txt");

	int i = 1;

	//checking the file and readin them into vectors
	if (myfile.is_open())
	{
		getline(myfile, strLine);

		while (myfile.good())
		{
			getline(myfile, strLine);

			stringstream ss(strLine);

			while (ss >> f)
			{
				if (i == 1)
				{
					gramsVector.push_back(f);
					i++;
				}
				else if (i == 2)
				{
					molVector.push_back(f);
					i++;
				}
				else if (i == 3)
				{
					litVector.push_back(f);
					i == 1;
				}
				else
				{
					i++;
					if (ss.peek() == '.')
						ss.ignore();
				}
			}
		}
	}

	//solving for mols
	for (int g = 0; g < molVector.size(); g++)
	{
		float A;
		A = gramsVector[g] / molVector[g];
		molsVector.push_back(A);
	}

	//solving for molality
	for (int h = 0; h < molsVector.size(); h++)
	{
		float B;
		B = molsVector[h] / litVector[h];
		molalVector.push_back(B);
	}

	//outputing results
	for (int e = 0; e < molalVector.size(); e++)
	{
		cout << gramsVector[e];
		cout << "The Concentration is:" << molalVector[e] << "    M\n";
	}

	system("pause");
}
Last edited on
Consider using a vector of structs rather than a set of parallel vectors of the same size.

I'm not convinced that you need the intermediate molsvector[] to hold the number of moles in your sample: it is only used as a precursor to finding the molality.

Consider range-based for when dealing with a whole container.

Simplify your input.


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
#include<string>
#include<vector>
#include<sstream>
#include<iostream>
#include<cstdlib>
#include<fstream>

using namespace std;


int main()
{
   // declaring vectors - CONSIDER USING A struct HERE; DANGEROUS TO USE molsvector AND molvector
   vector<double> molalVector, molsVector, gramsVector, molVector, litVector;

   // declaring the string to read in lines
   string strLine;

   ifstream myfile("../results.txt");
// ifstream myfile("results.txt");

   // checking the file and reading them into vectors
   if ( myfile.is_open() )
   {
      getline( myfile, strLine );                // ignore header line

      while ( getline(myfile, strLine ) )
      {
         double grams, mol, lit;
         char comma;
         stringstream( strLine ) >> grams >> comma >> mol >> comma >> lit;
         gramsVector.push_back( grams);
         molVector.push_back( mol );
         litVector.push_back( lit );
      }
   }
   else
   {
      cout << "File not opened";
      exit( 1 );
   }

   //solving for mols and molality
   for (int i = 0; i < molVector.size(); i++)                        // could use range-based for here ...
   {
      molsVector.push_back( gramsVector[i] / molVector[i] );
      molalVector.push_back( molsVector[i] / litVector[i] );
   }

   //outputing results
   for (int i = 0; i < molalVector.size(); i++)
   {
       cout << "Grams: " << gramsVector[i] << ";  ";
       cout << "Concentration: " << molalVector[i] << " M\n";
   }

// system("pause");
}


Grams: 100;  Concentration: 1 M
Grams: 35;  Concentration: 0.116667 M
Grams: 765;  Concentration: 2.94118 M
Grams: 1011;  Concentration: 8.91534 M
Last edited on
Line 60 has no effect. Change it to i = 1; // Note the single =
@lastchane

First off thank you so much this works great!

secondly, what do you mean by ranged based for line 44

I'm glad it works OK.

Now I look at it more carefully, you do need the index i for all the parallel vectors, so a range-based loop wouldn't help here. It can be left as it is.
Topic archived. No new replies allowed.