0

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 I'm always showed up as zero.

string oneline;
double real, im;
char sign, ichar;
    
// fin opens a text file that has complex numbers written in the form of a+bi
while (!fin.eof()) {
    getline(fin, oneline);
    real = 0; 
    im = 0; 
    plusorminus = '\0'; 
    ichar = '\0';
    stringstream(oneline) >> real >> plusorminus >> im >> ichar;
    cout << im; //this is showing as 0 no matter what while instead it should read the imaginary part. The real part is being read fine.

    switch (plusorminus) {
        case '-': 
            im = -im; 
            break;
        case 'i': 
            im = real; 
            real = 0; 
            break;
        case '\0': 
            im = 0; 
            break;
    }
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
  • 1
    `double real, im; char sign, ichar, plusorminus; stringstream("3+5i 2-3i 11+22i") >> real >> plusorminus >> im >> ichar;` This much works fine for me. `real`: `3.0`, `plusorminus`: `-`, `im`: `5.0`, `ichar`: `i`. So maybe check your input. – Wyck Sep 20 '21 at 04:17
  • hi, I tried this in an online c compiler and it is working fine but it is not working correctly when I use it with the rest of my code in VS code. The input (the string online) is getting printed correctly. – scarlet_dragon Sep 20 '21 at 04:20
  • Help me reproduce your problem then. What value do I have to assign to `oneline` to reproduce your problem? – Wyck Sep 20 '21 at 04:21
  • I set oneline to "3.14-8i". Now, I ran the exact code in an online compiler and in VS code in my local machine (I'm using MacOS). But it gives an issue while running locally. – scarlet_dragon Sep 20 '21 at 04:23
  • 3
    Handy reading: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Sep 20 '21 at 04:25
  • Then the title of this is misleading then. This has nothing to do with your sstream or complex numbers. This is likely related to problems reading the file, and has nothing to do with parsing. Did you examine `oneline` in a debugger? Verify the values of `oneline` are the ones you expect. I suspect user4581301 is onto something. – Wyck Sep 20 '21 at 04:26
  • I set oneline as "3.14-8i" and tried the code, yet it is not working. – scarlet_dragon Sep 20 '21 at 04:30
  • ``` #include #include using namespace std; int main(){ double real, imag; char plusorminus, ichar; stringstream("3-4i") >> real >> plusorminus >> imag >> ichar; cout << real << endl; cout << plusorminus << endl; cout << imag << endl; cout << ichar << endl; return 1; } ``` This is what I tried in a separate file. yet the output is: 3 - 0 – scarlet_dragon Sep 20 '21 at 04:34
  • OK, so, again, help me reproduce your problem. That code outputs `3` `-` `4` `i` for me. Maybe you should be telling us compiler version, OS, file encoding, compiler settings, etc. Start over with a brand new project. Something is fishy with your story. I cannot reproduce your problem. [Here's the counterexample](https://godbolt.org/z/GsjYoa9dn) – Wyck Sep 20 '21 at 04:44
  • 1
    Instead of `while (!fin.eof()) { getline(fin, oneline); ...` do `while(getline(fin, oneline)) { ...` - Read the link @user4581301 provided above. – Ted Lyngmo Sep 20 '21 at 05:29
  • Hi, I tried that too. But it is not working. I tried it on another MacOS and it throwed the same error. It works fine on online compilers. Below is the output of the command, "gcc --version": Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1 Apple clang version 12.0.5 (clang-1205.0.22.11) Target: arm64-apple-darwin20.6.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin – scarlet_dragon Sep 21 '21 at 08:42

0 Answers0