0

I am attempting to read in a list of binomials, separated by line in a file given as a command line argument. The file looks like this...

numbers.txt

2.1x+1
3x-5.3
-24.1x+24.7
0x-15.5
-12.3x+0

Expected output:

input: 2.1x+1
real: 2.1
imag: +1
input: 3x-5.3
real: 3
imag: -5.3
input: -24.1x+24.7
real: -24.1
imag: +24.7
input: 0x-15.5
real: 0
imag: -15.5
input: -12.3x+0
real: -12.3
imag: +0

My Output:

input: 2.1x+1
real: 2.1
imag: 2.07545e-317
input: 3x-5.3
real: 3
imag: 2.07545e-317
input: -24.1x+24.7
real: -24.1
imag: 2.07545e-317
input: 0x-15.5
real: -24.1
imag: 2.07545e-317
input: -12.3x+0
real: -12.3
imag: 2.07545e-317

I'm having with getting the 'real' double variable value correct. sscanf() is also not detecting the leading zero value for one of the x-Coefficients, is there something I can do to sscanf() to fix this?

#include <iostream>
#include <istream>
#include <fstream>
#include <algorithm>
#include <sstream>
#include <vector>

using namespace std;

class binomial
{
public:
  double real;
  double imag;
  string usercmplx;
};

int main(int argc, char* argv[]){

  string word, input;
  vector <binomial> VecNums;
  binomial complexnum;

  // Inializes vector to size for speedup                                                                             
  VecNums.reserve(1000);

  ifstream file(argv[1]);

  // Parse txt file line by line                                                                                      
  while (file >> input) {
    // handle empty lines                                                                                             
    if (!input.empty()){

    // Saves the user input format                                                                                    
    complexnum.usercmplx = input;

    // This seperates the input into the real and imaginary parts                                                     
    sscanf(input.c_str(), "%lf %lf", &complexnum.real, &complexnum.imag);

    cout << "input: " << input << endl;
    cout << "real: " << complexnum.real << endl;
    cout << "imag: " <<complexnum.imag << endl;

    // Push binomials into Vector                                                                                       
    VecNums.push_back(complexnum);

    }
  }
  return 0;
}
Zach
  • 119
  • 3
  • 17

1 Answers1

0
sscanf(input.c_str(), "%lf %lf", &complexnum.real, &complexnum.imag);

This sscanf format string states: the input contains a double value, followed by some whitespace, followed by another double value.

You provide the following input:

 2.1x+1

Do you see here a double value, followed by some whitespace, and another double value? Of course not. The first double value is followed by the character 'x'. That's not whitespace.

Since the actual input does not match the expected input, sscanf() fails to parse the input and initialize the returned values. If you check the return value from sscanf(), you would know that.

One of the ways to fix this is to tell sscanf() that a character follows the double value.

char char_value;

sscanf(input.c_str(), "%lf%c%lf", &complexnum.real, &char_value, &complexnum.imag);
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • I see... the whitespace problem makes sense now. The problem with the leading zero before the char_value still occurs. Not understanding why – Zach Sep 06 '16 at 00:17
  • for `0x-15.5`, you need use `sscanf(str.c_str(), "%dx%lf", &real, &image);`. Note: `%d` not `%lf`. but i also want to know the reason. – BlackMamba Sep 06 '16 at 00:30