Declare a vector that include strings and floats

Hi,

I'm new to C++ so please be patient with me and explain everything in plain English. I'm trying to write a code that would read data from a file and store certain values for later use.
My code currently looks like this:

CData.cpp:
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
#include "CData.h"
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <vector>

using namespace std;
using std::vector;

string public_dir="../data/";

CData::CData(string line) {
	char temp[16];
	sscanf(line.c_str(), "%f", temp, &survey}

list<CData> CData::fileExplorer(string filename)
{
	//read the data file
	ifstream datafile(filename.c_str());
	if (!datafile.is_open())
		cout << "unable to open the data file";

	//prepare variables
	list<CData> photometry;
	//string line;
	//string datum;
	vector<CData> line;
	vector<CData> datum;

	//skip the 1st and 2nd lines
	getline(datafile, line);
	getline(datafile, line);

	//start to read the data file at the 3rd line
	while (datafile.good()) {
		getline(datafile, line);
		string line[0];
		float line[1];
		if (line[0] == "SURVEY:")
		    survey = line[1];
        string result = CData(survey)
	return result;
	}


Cdata.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <string>
#include <list>

#ifndef CDATA_H_
#define CDATA_H_

using namespace std;

class CData {
public:
	CData();
	virtual ~CData();

	float survey
	
	CData(string line);
	list<CData> fileExplorer(string filename);
};

#endif /* CHALLENGEDATA_H_ */


The errors that I'm getting are in the CData.cpp code and are as follows:
- getline(datafile, line): Invalid arguments
- string line[0]: 'line' has a previous declaration as "std::string line[0]"
- conflicting declaration "float line[1]"
- expected initializer before "if"

I don't know how to fix these errors. I've tried declaring "line" as a string before, but that would prompt errors with the if statement.
Thank you so much in advance for any help!
The standard version of getline is expecting a string (or char buffer) as the second argument.

You could write your own implementation which takes a vector<CData>, but that might not be the right way to go. You would still have to use the normal version of getline and then convert the input to CData instances.

string line[0] is redeclaring line, this time as an array of length (which is illegal, I think. though some compilers let you get away with it...)

float line[1] is declaring line yet again, this time as an array of floats with a single element.

And there's a missing semicolon.

I am unsure about what you are trying to do in your loop...
@andy: Thank you for replying.

If I don't have string line[0] and float line[1], then I get:

- no match for 'operator==' in 'line.std::vector<_Tp, _Alloc>::operator[] [with _Tp = ChallengeData, _Alloc = std::allocator<ChallengeData>, std::vector<_Tp, _Alloc>::reference = ChallengeData&, std::vector::size_type = unsigned int](0u) == "SURVEY:"'

- cannot convert 'ChallengeData' to 'float' in assignment

My mistake, the missing semicolon is from copy and pasting, it is in my code on my computer though.

The data file should look something like this:
SURVEY: abcd
ID: 1234

So in the loop I want to assign survey = abcd because I need to use survey for something later.
Based on your examples (SURVEY: abcd, ID: 1234) I would take a step back and reconsider your need for CData.

Where line is a string

1
2
3
4
	while (datafile.good()) {
		getline(datafile, line);
		if (0 == line.find("SURVEY:"))
		    survey = ...


string::find() returns the index at which a string is found. So here I'm checking to see if the string begins with "SURVEY:"

If it does you can split off the following, convert it if necessary, and store it. Maybe in a new type of CData?

Does you file contain one SURVEY entry, one ID entry, etc?
Yes, the string begins with SURVEY: and there's only one SURVEY entry, one ID entry, etc. in the data file.

I've tried what you said and it works! Thank you so much! I didn't know about line.find() before.
Cool!

Reading up about std::string (etc) might be a good thing for you to do then!
Topic archived. No new replies allowed.