Conversion between strings and floats <sstream>

For my assignment all we are required to do is stream in the .txt and just use whatever method we want, to output the correct data. I chose this way because I'm least familiar with this style. Any help would be greatly appreciated as I am new to coding.

Example of provided .txt
1
2
3
4
5
6
7
8
9
15.1,Chico,17.4
34.4,Seattle,30.5
22.9,Portland,26.1
12.3,New York,9.4
.5,Death Valley,1.1
3,Las Vegas,13.8
60,Alajuela,54.3
17.3,San Francisco,20.3
29.5,Dublin,30.8


Example of outcome .txt

1
2
3
City          Total
Seattle       64.92
etc..           ..


Generic start of my program.

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
#include <iostream>
#include <algorithm>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	int size = 9;
	ifstream fin;
	fin.open("inputfile.txt");
	ofstream fout;
	fout.open("outputfile.txt");
	
	string * rainFall;
	rainFall = new string[size];
	for(int i = 0; i < size; i++)
	{
//inputs the array with text
                getline(fin,rainFall[i]);

	}

	for(int i = 0; i < size; i++)
	{
//outputs the array
		fout << rainFall[i] << endl;
	}

	return 0;
}
Last edited on
[code]
[/cout <<"City Total"<<endl;
cout <<"=== ===="<endl;
for(i = 0;i < size; i++)
{
cout <<city[i]<<" <<rain1[i] + rain2[i]<<endl;
}
]

separate all the data in one line by a space, then it should work just fine
I dont understand your explanation at all..?
So this is what is commonly called a comma separated value or CSV file. This is pretty self explanatory but just in case, it means that each field is separated by a comma. The inclusion of the "Algorithm" header suggests that your teacher expects you to know about things like templates, if this is the case then you'll want to use "Count()" to determine the number of commas in each line and then dynamically create a string array based on that, then I would normally suggest using "strtof()" to convert the strings to floating points and determine which field is the label simultaneously but for that you would need to add the "cstdlib" header. Then it's just a matter of adding up the numbers and formatting the output.
This is now what I have, I had used <sstream> which i mildly grasp the concept of. I have parsed out the numbers that I need from the words, now I just need to figure how to use one of the conversions from a string to a float, such as atof. I commented certain situations that I found.
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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
	int size = 9;
	string delimiter = ",";
	size_t pos = 0;
	string token;
	ifstream fin;
	fin.open("inputfile.txt");
	//ofstream fout;
	//fout.open("outputfile.txt");
	
	string * rainFall;
	rainFall = new string[size];
	for(int i = 0; i < size; i++)
	{
		getline(fin,rainFall[i]);
		while ((pos = rainFall[i].find(delimiter)) != string::npos) 
		{
    		token = rainFall[i].substr(0, pos);
    		//prints out just rainfall before city name(w/o line 33)
    		//prints out everything correctly(w/ line 33)
    		cout << token << endl;
    		rainFall[i].erase(0, pos + delimiter.length());
		}
			cout << rainFall[i] << endl;
		//this alone prints out just city names
		//cout << token << endl;
		//this alone prints out city then the rainfall after last comma
		//cout << token << rainFall[i] << endl;
	}
	//outputs the end of first line after last dilemeter
	//parsed array isn't saved back into original array
	cout << rainFall[0] << endl;

	/*for(int i = 0; i < size; i++)
	{
		cout << rainFall[i] << endl;
	}*/

	return 0;
}

You look like you have a handle on it. I still think you should work std::count into there, that would make your dynamic arrays look more impressive. The 'token' string should probably be an array of some sort otherwise you're looping through and overwriting your previous values and the sample output suggests that you want to add those values together.
Ok, ignore all other questions, I was doing it the difficult way.. Now I have the correct look on the output besides the adding isn't working right. The first city adds correct then the others do as I commented in below. An example input file is posted above
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
#include <iostream> 
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
	string s;
	string city;
	float f1;
	float f2;
	ifstream fin;
	fin.open("inputfile.txt");
	getline(fin,s,',');
	cout << "City          " << "Total Rainfall" << endl;
	cout << "----------------------------" << endl;
	if(fin.is_open())
	{
		//first one adds fine, other cities such as seattle add last years average(seattle) and last years average(chico)
		while(fin.good())
		{
			istringstream(s) >> f1;
			getline(fin,s,',');
			city = s;
			getline(fin,s,',');
			istringstream(s) >> f2;
			float f3 = f1+f2;
			cout << city << "          " << f3 << endl;
		}
		fin.close();
	}
	return 0;
}
[code][/#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;

int main()
{
string s;
string city;
float f1;
float f2;
ifstream fin;
fin.open("inputfile.txt");
getline(fin,s,',');
cout << "City " << "Total Rainfall" << endl;
cout << "----------------------------" << endl;
if(fin.is_open())
{
//first one adds fine, other cities such as seattle add last years average(seattle) and last years average(chico)
while(fin.good())
{
istringstream(s) >> f1;
getline(fin,s,',');
city = s;
getline(fin,s,',');
istringstream(s) >> f2;
float f3 = f1+f2;
cout << setw(15) << left << city << f3 << endl;
}
fin.close();
}
return 0;
}

code]
By the way, how do i actually make my code look like a source code, I'm tired of writing illegible code.

The above program actually displays in the desired output, just a little tweaking
Thank you, now I just need to figure out the double output of the last city. And the code thing is easy, this site is I believe java based, so its [c0de] to start then [/c0de] to end, and of course replace the 0's with o's.
Topic archived. No new replies allowed.