using sstream with complex numbers

So I am trying to read a list of complex numbers from a txt file given as:
3+5i
2-3i
11+22i
However when i use stringstream(oneline)>>real>>plusorminus>>im>>ichar; the real part is okay but the im part always shows up as 0. I tried to debug it by adding the cout<<im and no matter what i tried im always showed up as zero. Please and thank you for your help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
string oneline;
double real,im;
char sign,ichar;
	while (!fin.eof())
        {
	  getline(fin, oneline);
	  real=0; im=0; plusorminus='\0'; ichar='\0';
	  stringstream(oneline)>>real>>plusorminus>>im>>ichar;
	  cout<< im;
	  switch (plusorminus){
	  case	'-': im=-im; break;
	  case	'i': im=real; real=0; break;
	  case	'\0': im=0; break;
	}
Last edited on
tried to fill the blanks but could not reproduce your issue.
http://www.eelis.net/iso-c++/testcase.xhtml (especially point 6)


By the way, you shouldn't loop on eof(), loop on the reading operation
while( getline(fin, oneline) )
so on line 8 i want to read in the parts of the file separately for example for the first line in the txt file 3+5i, I want real= 3, plusorminus=+ , im=5 and ichar=i.
To verify this i put cout<<real on line 9 and it works fine. the plusorminus is also fine but when i put the I'm it always equals 0 when it should equal 5 in this case. Hope that makes it a little more clear.
I understand your problem, but I cannot reproduce it.
Your snip is incomplete, so I added things to make it compile, and it worked fine in my machine. So I guess that you did something different, and that's why I'm asking you to post enough code so we can compile, run and get the same errors (or equivalent) that you do.

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
#include <string>
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
int main(){

	string oneline;
	double real, im;
	char plusorminus, ichar;
	std::istream &fin = std::cin;
	while (!fin.eof())
	{
		getline(fin, oneline);
		real=0; im=0; plusorminus='\0'; ichar='\0';
		stringstream(oneline)>>real>>plusorminus>>im>>ichar;
		switch (plusorminus){
			case	'-': im=-im; break;
			case	'i': im=real; real=0; break;
			case	'\0': im=0; break;
		}

		std::cout << "Re: " << real << " Img: " << im << '\n';
	}
}



Not sure, but perhaps there is undefined behaviour. Try it without the unnamed temporary
1
2
stringstream foo(oneline);
foo>>real>>plusorminus>>im>>ichar;
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <complex>
#include <string>
#include <sstream>

int main()
{
    std::istringstream file( "3+5i \n 56+72ix \n +2-3i \n 45 \n 11+22i \n 11.1+22i \n 34++7i \n -11-22i \n 26+94j \n -0-0i \n" ) ;

    std::string line ;
    while( std::getline( file, line ) )
    {
        std::istringstream stm(line) ;
        int real, imag ;
        char ch ;
        if( stm >> real >> imag >> ch && ch == 'i' && !( stm >> ch ) )
            std::cout << std::complex<double>(real,imag) << '\n' ;
        else std::cerr << "badly formed number '" << line << "'\n" ;
    }
}

http://coliru.stacked-crooked.com/a/a159c5c6ea093a07
Topic archived. No new replies allowed.