Thread: Complex input

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    19

    Complex input

    Hi, I am writing a program to perform nodal analysis and I need complex input for phasors and impedances. I have declared the variable and I want to read its value from standard input:
    Code:
    complex <float> VoltageSource;
    cout << "Enter voltage source value: " << endl;
    cin >> source;
    Hovever the only way for it to accept a complex input is if I write it in the form:
    (real,imag).
    My question: is there any way to make it read "2 3" or "2+3i" to give me 2+3i? Is there a way to read only to the real or imaginary part of a given complex variable? VoltageSource.real and VoltageSource.imag give me an error, so I concluded that they are probably used for output only.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Code:
    int main(int argc, char** argv)
    {
    	std::complex<float> complex;
    	std::cin >> complex._Val[0] >> complex._Val[1];
    	std::cout << complex << std::endl;
    }
    Allows for the first form "1.2 6.2" for instance. For the second form, read in a string and parse it.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Is there a guarantee that the members are public? I couldn't find one, but I may be looking in the wrong place.

    It's tempting to tell your users "write your input the right way darn it".

    Silly code example because why not:
    Code:
    complex<float> bob;
    float x, y;
    cin >> x >> y;
    stringstream foo;
    foo << "(" << x << "," << y << ")";
    foo >> bob;
    cout << bob;

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Hmm you are correct in that i couldn't find a guarantee for them to be public, however temporary floats and using the constructor that takes two complex::value_type would serve the same purpose.

    To set the real and imag parts just use operator= with a temporary std::complex where you have set real and imag to the value you either want to keep or to change.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    std::complex overloads stream operators, so you should be able to do
    std::complex<float> complex_num;
    std::cout << "Enter complex number: ";
    std::cin >> complex_num;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. complex calculator
    By rafay_07 in forum C++ Programming
    Replies: 16
    Last Post: 11-07-2010, 04:40 AM
  2. complex numbers
    By Mastiff in forum C Programming
    Replies: 6
    Last Post: 09-12-2008, 08:43 AM
  3. how complex can you do?
    By wart101 in forum C++ Programming
    Replies: 10
    Last Post: 10-05-2005, 11:50 AM
  4. complex.h in g++ not working
    By amolh in forum C++ Programming
    Replies: 1
    Last Post: 08-14-2005, 01:46 AM
  5. more complex C++
    By EvBladeRunnervE in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2003, 06:07 PM

Tags for this Thread