My problem here is trying to test a substr of a string. when compiling I get the following errors
c:\work\programming\nettest\nt.h(40) : error C2146: syntax error : missing ';' before identifier 'path'
c:\work\programming\nettest\nt.h(40) : error C2501: 'string' : missing storage-class or type specifiers
c:\work\programming\nettest\nt.h(40) : error C2501: 'path' : missing storage-class or type specifiers
c:\work\programming\nettest\nt.cpp(47) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conv
ersion)
c:\work\programming\nettest\nt.cpp(61) : error C2039: 'path' : is not a member of 'netData'
c:\work\programming\nettest\nt.h(34) : see declaration of 'netData'
c:\work\programming\nettest\nt.cpp(84) : error C2440: 'return' : cannot convert from 'struct netData *' to 'struct netData'
No constructor could take the source type, or constructor overload resolution was ambiguous
c:\work\programming\nettest\nt.cpp(91) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct netData' (or there is no acceptable conversion)
Error executing cl.exe.
Can anyone tell me whats wrong with the following line
if ( line.substr(index+6,1) == '1')
I've tried assigning the substr to a tmp string, then a char, casting it to a char. all have failed.
+++++++++++++++++++++++++
Additionally the line while(!netcsv.eof()) statemant causes an error. This has worked in a previous program with one major difference.
In the working program the code <ifstream netcsv(filename)> where filename was defined as an array of char. In this program it is defined as a string.
+++++++++++++++++++++++++
Thanks in advance for any help with these issues, most of the code is shown below.
I should point out that netData and the string header are defined in nt.h
Code:
#include <c:\work\programming\nettest\nt.h>
#include <fstream>
#include <iostream>
//#include <string>
#include <sstream>
using namespace std;
netData readNetcsv()
{
int index, startTime, timeStep, endTime, tmp;
char answer, temp;
string line, tstore, filename, path;
bool marker = false, again = true;
netData *lMember, *start_ptr, *end_ptr; //create object
start_ptr = end_ptr = NULL; //init pointer to list
do
{
cout << "Enter file path : " << endl; //get path details
cout << "(eg c:\\dir1\\dir2\\ " << endl;
cin >> path;
filename = path + "net.csv"; //build full filename
ifstream netcsv(filename.c_str()); //& open
if (netcsv) //process file
{
cout << filename << " opened successfully " << endl;
istringstream strin (tstore); //object constructor for int string ops
do
{
getline (netcsv, line);
if ((line[0] == 'T') && (line[1] == 'I')) //wait until colomn headers found
{
marker = true;
getline (netcsv, line);
}
if (marker == true) //start saving data
{
lMember = new netData; //create new record
index = line.find(",",1); //find first comma in line
tstore = line.substr(0,index); //get time field
strin >> lMember->time; //save as integer variable
tstore = line.substr(index+1,4); //probability now
strin >> tmp;
//or lMember->probability = strtod(tstore.c_str(), NULL);
lMember->prob = (int)(tmp*100 + 0.5f); //convert to threshold value
if ( line.substr(index+6,1) == '1') //alarm field
{
lMember->alarm = true;
}
else
{
lMember->alarm = false;
}
lMember->nxt = NULL;
if (start_ptr == NULL) //manage pointers
{
start_ptr = lMember; //first member in list
start_ptr->path = path; //file path details
end_ptr = start_ptr;
startTime = lMember->time; //hold first time point
}
else
{
end_ptr->nxt = lMember; //set pointer for previous record
end_ptr = lMember; //point to new end of list
if (timeStep == 0) timeStep = lMember->time - startTime;
}
}
}while(!netcsv.eof());
endTime = lMember->time; //hold last time point
again = false; //another loop not required
}
else
{ //file doesn't exist
cout << "File not found. " << endl;
cout << "Enter new path y/n (y) : " << endl;
cin >> answer;
if (answer == 'n') again = false; //allow prog to fail
}
}while(again);
return (start_ptr);
}