Thread: streaming - error

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    43

    streaming - error

    Hi all,
    I have written this piece of code: it is supposed to recieve an istream with a string which is used to create a graph.
    the istream is of the follwing format:
    |vertex1,vertex2, .. ,vertexn|(v1->v2),(vi->vj)..|
    example:
    |1,2,3|(1->2),(2->3)|
    will create a graph, where 1,2,3 are vertices, and there are edges 1->2 and 2->3

    however this code fails: it creates the vertices, assigns the right "source" for the edge, but put gibrish into "dest"
    below is the code, I have marked the problematic line
    Please help!!
    Code:
    istream& operator >> (istream& in,Graph & g1)
    {
    	//local variables
    	char string[MAX];
    	char buff;
    	int curr=0,temp,source,dest;
    	Vertex v1;
    	stringstream ss;
    	
    /**************GET VERTECIES FROM STREAM**************************************/
    	//get rid of first '|'
    	in >> (in, buff);
    	//get first num
    	in >> (in, buff);
    
    	while (buff!='|')
    	{
    		
    		//while still reading digits (not comma)
    		while (isdigit(buff))
    		{
    			string[curr]=buff;
    			in >>(in, buff);
    			curr++;
    		}
    		//have now read comma or |(is value of buff)
    		
    		//set ss to empty string stream
    		ss.str(string);
    
    		//convert to integer
    		ss >> temp;
    
    		//add new vertex with value
    		g1.addVer(temp);
    
    		//set pos in string to 0
    		curr=0;
    		string[curr]='\0';
    		//read next digit
    		if (buff!='|')
    			in >>(in, buff);
    	}
    /*********************GET EDGES FROM STREAM***********************************/
    	//get first "("
    	in >> (in, buff);
    	while (buff!='|')
    	{
    		//for each edge!!!!!
    		//flush stream
    		ss.str("");		
    		/********* GET SOURCE*************************/
    		in >> (in, buff);
    		while (isdigit(buff))
    		{
    			string[curr]=buff;
    			in >>(in, buff);
    			curr++;
    		}
    		//add source to int
    		string[curr]='\0';
    		ss.flush();
    		ss.str(string);
    		ss >> source;
    		/*********FLUSH "->" ***************************/
    		in >> (in, buff) >> (in, buff);
    		//at this stage, buff contains first dig of dest vertex
    
    		curr=0;
    		while (isdigit(buff))
    		{
    			string[curr]=buff;
    			in >>(in, buff);
    			curr++;
    		}
    		//add dest to int
    		string[curr]='\0';
    		ss.flush();
    		ss.str(string);
    		ss >> dest; //HERE"S THE PROBLEM: DOESNT ASSIGN
    		g1.addEd(source,dest);
    		//at this stage buff contains the final ) of edge
    		//get char after bracket (comma or |)
    		in >> (in, buff);
    		if (buff == ',')
    			in >> (in, buff) >> (in,buff);
    	}
    
    	return in;
    
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    43
    i'm probably just wasting peoples' time.
    I decided to use "atoi" for the conversion instead...
    i'm not sure if it's a legit function, but i've just had enough...

  3. #3
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    atoi is standard. itoa is not.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I prefer strtol() instead of atoi() as it allows some more error checking.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM