Complex No: Input from user

I am writing a programme for my circuit class that can find determinent of complex no. 3x3 matrix. for it i want complex input from user.. How to achieve..
I dont want to add real and imaginary parts separately...

i dont know if this is what you want, but if you want the user to input a number from the keyboard, you need to define the variable and use the cin command.

Example:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

void main()
{
       int x;

       cout << "Enter a number: ";
       cin >> x;
}
You can then get it as string from user. and then convert the real and imaginary parts into numbers .
can you type an example code.

i think i would have to use atoi for that if i am not wrong .
and cin.getline(); for the string input. Right ?
Yes , you can use cin.getline() for string input . and atoi() can be used for conversion.

Check the sample code:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std ;

int main(){
	string s = "1+i3";
	int index = s.find_first_of("+i");
	int real = atoi(s.substr(0,index).c_str()) ;
	int img = atoi(s.substr(index+2,3).c_str()) ;
	cout << real << " " << img << endl;	 
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <complex>
#include <iostream>

int main()
{
    std::complex<double> c ;

    std::cin >> c ; // enter: (21.2,7.6)
    std::cout << c << '\n' ; // (21.2,7.6)

    std::cin >> c ; // enter: (14.3)
    std::cout << c << '\n' ; // (14.3,0)

    std::cin >> c ; // enter: 99.43
    std::cout << c << '\n' ; // (99.43,0)
}
@JLBorges

gives the following output

(4.92187e-270,0)
(5.05798e-270,0)
(5.05798e-270,0)
@Raman00:

thanks.. works.
Topic archived. No new replies allowed.