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.
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.)