How to read string and ints from a file and access them?

Okay, I have a .csv file and have a bunch of code that is supposed to take the items and allow access to them when I need to. The problem is I get a build error saying "no suitable conversion function from "std::string" to "int" exists". So how do I get around this cause I need to access them by putting them into an array.

Thanks for any help!

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

using namespace std;

struct Forecast {
	string day;
	int highTemp;
	int lowTemp;
	int humidity;
	int avgWind;
	string avgWindDir;
	int maxWind;
	string maxWindDir;
	double precip;
};

int main()
{
	Forecast data;
	data.day;
	data.highTemp;
	data.lowTemp;
	data.humidity;
	data.avgWind;
	data.avgWindDir;
	data.maxWind;
	data.maxWindDir;
	data.precip;

	const int arrayLength = 984;
	Forecast weatherD[arrayLength];

	ifstream Forecast;
	Forecast.open("DATABOULDER.csv");
	if (Forecast.fail())
	{
	}
	else 
	{
		string weather;
		int lineIndex = 0;
		while (getline(Forecast, weather, '\n'))
		{
			stringstream ss;
			ss << weather;
			int weatherIndex = 0;
			while (getline(ss, weather, ','))
			{
				if (weatherIndex == 0)
				{
					weatherD[lineIndex].day = weather;
				}
				else if (weatherIndex == 1)
				{
					weatherD[lineIndex].highTemp = weather;
				}
				else if (weatherIndex == 2)
				{
					weatherD[lineIndex].lowTemp = weather;
				}
				else if (weatherIndex == 3)
				{
					weatherD[lineIndex].humidity = weather;
				}
				else if (weatherIndex == 4)
				{
					weatherD[lineIndex].avgWind = weather;
				}
				else if (weatherIndex == 5)
				{
					weatherD[lineIndex].avgWindDir = weather;
				}
				else if (weatherIndex == 6)
				{
					weatherD[lineIndex].maxWind = weather;
				}
				else if (weatherIndex == 7)
				{
					weatherD[lineIndex].maxWindDir = weather;
				}
				else if (weatherIndex == 8)
				{
					weatherD[lineIndex].precip = weather;
				}
			}
		}
	}
    return 0;
}

What is the purpose of lines 25-33?

As for the error you experience on compilation (where it would be usual for you to provide the exact error message including line numbers), you cannot assign an arbitrary string to an int. You must first verify the string can be converted to a numerical value and do the conversion.

http://en.cppreference.com/w/cpp/string/basic_string/stol

or perhaps you'd care to convert your string into a stream and extract an int from it.
http://en.cppreference.com/w/cpp/io/basic_stringstream

Or perhaps review the answers here:
http://stackoverflow.com/questions/7663709/convert-string-to-int-c

Last edited on
Okay sorry here is the full line of the error
 
no suitable conversion function from "std::string" to "int" exists


It is on lines 60, 64, 68, 72, 80, 88.

The other code on lines 25-33 were allow access to my struct as far as I know from my teacher.

I used stringstream as shown on line 49 so is there another step to this?
The other code on lines 25-33 were allow access to my struct as far as I know from my teacher.

Those lines do absolutely nothing but take up space.

I used stringstream as shown on line 49 so is there another step to this?

You are extracting strings from your stream. So, yes. There is another step to this. You need to obtain values of the correct type. You can do so either by extracting them directly or converting the strings you are currently obtaining from the stream via one of the methods previously linked to you.

It would be more idiomatic to initialize an istringstream (istringstream ss(weather);) rather than using a stringstream and performing both insertion and extraction operations on the stream.
Hello jlmccart01,

The quick fix I used is to change
weatherD[lineIndex].variable_name = weather;
to
weatherD[lineIndex].variable_name = atoi(weather.c_str();
That at least got rid of the error messages.

I do not know which version of VS you are using, but in my 2015 version every place that weather had a problem it was underlined in red. Once I made the changes to use atoi() the errors disappeared.

Not knowing what is in the input file I could not test the program. I did notice on line 10 you create a struct Forecast then on line 38 you redefine Forecast as an ifstream. It did not cause a compile error or warning for me, but might be a problem at run time. Anyway ifstream is better used as something like inFile which better describes what it is.

Hope that helps,

Andy
Topic archived. No new replies allowed.