-2

I am trying to take a complex number input from the user in a form of a char or string like: 88.90-55.16i or -3.67+5i

and then convert it to float keeping the same format as above. Not (x,y).

char user[100];
vector < float > V;


for (int y = 0; y < 5; y++)
{
    cout << "Enter a complex number:" << endl;
    cin >> user;

    float newfloat = atof(user);


    cout << newfloat << endl;


}

Currently its not converting the whole number. Just 88 or -3 from above input examples.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
Bob Jack
  • 3
  • 2
  • 1
    How do you propose storing a complex-number (2 dimensional) in a `float` (1 dimensional)? – Dai Feb 03 '15 at 04:14
  • For storing a complex number create a structure or a class. – IrineK Feb 03 '15 at 08:21
  • @IrineK: That would be a bad idea, since we already have the type (std::complex) – MSalters Feb 03 '15 at 11:08
  • @MSalters, to use the ready-made solutions properly, a student must have written a couple of his/her own. Let them be far from the ideal, but in doing we learn. – IrineK Feb 03 '15 at 16:37
  • @IrineK: Do you have evidence to back that up? I find that students struggle much less with `std::string` then they do writing their own string class. – MSalters Feb 03 '15 at 21:13
  • @MSalters, if students were able to develop their own tools right after using somebody's ready-made solutions, it would be great. But I doubt that it works. – IrineK Feb 04 '15 at 06:47
  • @IrineK: Students should never be developing their own strings or complex numbers. That's pointless, as well as learning a bad habit they'd have to unlearn later. In the Advanced Datastructures course, there's plenty of opportunity to develop the relevant skills. – MSalters Feb 04 '15 at 09:03
  • Well, I prefer curious and stubborn students. – IrineK Jul 12 '16 at 15:57

3 Answers3

2

You would need to store the real and imaginary components of the complex number in separate float variables, which may or may not be grouped using a struct/class or std::pair<>. Input should be something like this:

std::cout << "Enter a complex number:\n";
float real, imaginary;
char i;
if (std::cin >> real >> imaginary >> i && i == 'i')
{
    ...do something with real & imaginary...
}
else
{
    std::cerr << "unable to parse input as a complex number\n";
    exit(1);
}

(FWIW, this is very obviously related to this earlier question - either the same person using a new username, or someone doing the same assignment. I provide an example program using std::complex<> in my answer there.)

Community
  • 1
  • 1
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
2

Float is not capable of storing that type of number. A float is designed to hold a single floating point number, i.e. 10.3 or 5.2. Not 1+2i. That is a two-dimensional quantity.

I would recommend you use the complex<float> template for your data type or create your own class. Look here for more: https://msdn.microsoft.com/en-us/library/5de6f0bw.aspx about the complex<float> It comes with built in tools to convert strings to complex types.

Iowa15
  • 3,027
  • 6
  • 28
  • 35
1

I have never used C++ before but I am sure float does not fit your expectations. Imaginary number is not a subset of float so you can't do that.

I guess one possible solution is to write your own imaginary class, or use a build-in one if there is one.

Also write a fromString()/toString() in your custom imaginary class.

Benson Zhang
  • 135
  • 1
  • 1
  • 9
  • 1
    There is `std::complex` (especially common for `T` to be `float` or `double`) – Ben Voigt Feb 03 '15 at 04:18
  • That sounds more like a comment than an answer. – R Sahu Feb 03 '15 at 04:18
  • :/ Sorry I didn't read the rules about comments and answers so I didnt know the differences between them. – Benson Zhang Feb 03 '15 at 04:21
  • Seems a reasonable answer to me (hence my earlier upvote)... that a `float` isn't sufficient is crucial information - directly addressing the question, and the alternatives are reasonable though the information that a `class` helps because it can store multiple floats isn't explicitly provided. – Tony Delroy Feb 03 '15 at 04:25