I am making a calculator that works with imaginary numbers. When I run it in the terminal I am able to put the first two inputs however the second one glitches out.(Look at screenshot for reference) Here is my Handler with get_complex_input() that runs at this part and Complex file.
#include
using namespace std;
#pragma onceclass Complex {
private:
float real;
float imaginary;public:
Complex() : real(0), imaginary(0) {}Complex(float r, float i) : real(r), imaginary(i) {}
Complex operator+(Complex rhs) {
return Complex(this->real + rhs.real, this->imaginary + rhs.imaginary);
}
Complex operator-(Complex rhs) {
return Complex(this->real – rhs.real, this->imaginary – rhs.imaginary);
}
Complex operator*(const Complex& other) const {
float new_real = (this->real * other.real) – (this->imaginary * other.imaginary);
float new_imaginary = (this->real * other.imaginary) + (this->imaginary * other.real);
return Complex(new_real, new_imaginary);
}Complex operator/(const Complex& other) const {
float denominator = (other.real * other.real) + (other.imaginary * other.imaginary);
float new_real = ((this->real * other.real) + (this->imaginary * other.imaginary)) / denominator;
float new_imaginary = ((this->imaginary * other.real) – (this->real * other.imaginary)) / denominator;
return Complex(new_real, new_imaginary);
}Function to find the conjugate of a complex number
Complex conjugate() {
return Complex(this->real, -(this->imaginary));
}A helper function to print the complex number
void print() const {
if (imaginary >= 0)
cout << real << ” + ” << imaginary << “i” << endl;
else
cout << real << ” – ” << -imaginary << “i” << endl;
}};
#include
#include “Complex.h”
#pragma once
using namespace std;class Handler {
public:
Function to display the menu and get the user’s choice
static int print_and_get_choices() {
int choice;
cout << “Complex Numbers Calculator” << endl;
cout << “1. Add two complex numbers” << endl;
cout << “2. Subtract two complex numbers” << endl;
cout << “3. Multiply two complex numbers” << endl;
cout << “4. Divide two complex numbers” << endl;
cout << “5. Find the conjugate of a complex number” << endl;
cout << “6. Exit” << endl;
cout << “Enter your choice: “;
cin >> choice;
return choice;
}Function to add two complex numbers
static void add_two_complex_numbers() {
Complex num1 = get_complex_input();
Complex num2 = get_complex_input();
Complex result = num1 + num2;
cout << “Result of addition: “;
result.print();
}Function to subtract two complex numbers
static void subtract_two_complex_numbers() {
Complex num1 = get_complex_input();
Complex num2 = get_complex_input();
Complex result = num1 – num2;
cout << “Result of subtraction: “;
result.print();
}Function to multiply two complex numbers
static void multiply_two_complex_numbers() {
Complex num1 = get_complex_input();
Complex num2 = get_complex_input();
Complex result = num1 * num2;
cout << “Result of multiplication: “;
result.print();
}Function to divide two complex numbers
static void divide_two_complex_numbers() {
Complex num1 = get_complex_input();
Complex num2 = get_complex_input();
Complex result = num1 / num2;
cout << “Result of division: “;
result.print();
}Function to find the conjugate of a complex number
static void find_conjugate_of_a_complex_number() {
Complex num = get_complex_input();
Complex result = num.conjugate();
cout << “Conjugate: “;
result.print();
}private:
Helper function to get user input for a complex number
static Complex get_complex_input() {
float real, imaginary;
cout << “Enter the real part: “;
cin >> real;
cout << “Enter the imaginary part: “;
cin >> imaginary;
return Complex(real, imaginary);
}
};
I just want to make the inputs work to check my functions. Also, using ./a.out on the folder terminal if that means anything
1