Thread: string & eof handling problems

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    12

    string & eof handling problems

    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);
    }

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    Opps - this info will probably help
    <cpp(47) : error C2679> refers to <if ( line.substr(index+6,1) == '1')>

  3. #3
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by spudval View Post
    Can anyone tell me whats wrong with the following line

    if ( line.substr(index+6,1) == '1')
    The problem is that you're comparing a string to a character. You can either do this:
    Code:
    if (line.substr(index + 6, 1) == "1")
    Or even better, a substring of size 1 is just a character, so this will work too:
    Code:
    if (line[index + 6] == '1')
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    thanks again

    I originally had the first sugesttion in the code, which also produced an error.
    But good news the second did work

    On to the next error.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Note that your check for eof() is not the best way to solve that problem. You don't handle fail states (which are admittedly rare when using getline) and the eof bit is not set until the read attempts to read past the end of the file (also somewhat rare with getline).

    Ideally, you should check the return value of the read to verify that it worked, and then process the line you read in. For example, in your code instead of the do while loop, use a regular while that looks like this:
    Code:
    while (getline (netcsv, line))
    {
      // all your code
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  2. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM