Thread: Complex input

  1. #1
    Registered User
    Join Date
    Nov 2010

    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:
    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:
    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
    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
    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:
    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
    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
    Join Date
    Oct 2007
    Inside my computer
    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