program?!
and I have another problem, I'm using fgets scanning but there is a problem after an input for numbers 1 to 4 the fgets captures NULL and pass thru the default in my switch statement how can I stop that?because my professor what to capture the spaces..lastly how can i check the values of the real and imaginary numbers using atof when the input is a string or a char and there is no range of values how can i do that?!Thank!!Code:
//This program prints the initialized value of two complex numbers and allows
//the user to change the values of the real and imaginary numbers of the two
//complex numbers. This program can also solve the sum and the product of the
//two complex numbers and display all the complex numbers in the program.
#include<stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
typedef struct CN{
double real;
double img;
}CN;
CN Sum(CN cx, CN cy){
CN cz;
cz.real=cx.real+cy.real;
cz.img=cx.img+cy.img;
return cz;
}
CN Mul(CN cx, CN cy){
CN cz;
double a,b,c,d;
a=cx.real*cy.real;
b=cx.real*cy.img;
c=cx.img*cy.real;
d=-1.0*(cx.img*cy.img);
cz.real=a+d;
cz.img=b+c;
return cz;
}
void DisplayCN();
int main(void){
CN cx,cy,cz;
char error[128];
int selection;
cx.real=1.0;
cx.img=-2.0;
cy.real=3.0;
cy.img=4.0;
do{
system("cls");
printf("The Starting Values:\n");
printf("CN1: %.2f%+.2fi\n",cx.real,cx.img);
printf("CN2: %.2f%+.2fi\n\n",cy.real,cy.img);
printf("Complex Number Selection Screen\n\n");
printf("First Complex Number:\n");
printf("1 - Change the Value of the Real Number\n");
printf("2 - Change the Value of the Imaginary Number\n\n");
printf("Second Complex Number:\n");
printf("3 - Change the Value of the Real Number\n");
printf("4 - Change the Value of the Imaginary Number\n\n");
printf("Complex Number Operations:\n");
printf("5 - Add the two Complex Numbers\n");
printf("6 - Multiply the two Complex Numbers\n");
printf("7 - Display all the Complex Numbers\n");
printf("8 - Exit\n");
fgets(error,128,stdin);
selection=atoi(error);
switch(selection){
case 1:
system("cls");
printf("Change the Value of the Real Number\n");
printf("CN1: %.2f%+.2fi\n",cx.real,cx.img);
printf("New Value for the Real Number: ");
scanf("%lf",&cx.real);
getche();
break;
case 2:
system("cls");
printf("Change the Value of the Imaginary Number\n");
printf("CN1: %.2f%+.2fi\n",cx.real,cx.img);
printf("New Value for the Imaginary Number: ");
scanf("%lf",&cx.img);
getche();
break;
case 3:
system("cls");
printf("Change the Value of the Real Number\n");
printf("CN2: %.2f%+.2fi\n",cy.real,cy.img);
printf("New Value for the Real Number: ");
scanf("%lf",&cy.real);
getche();
break;
case 4:
system("cls");
printf("Change the Value of the Imaginary Number\n");
printf("CN2: %.2f%+.2fi\n",cy.real,cy.img);
printf("New Value for the Imaginary Number: ");
scanf("%lf",&cy.img);
getche();
break;
case 5:
system("cls");
printf("Sum of the Complex Numbers\n");
cz=Sum(cx,cy);
printf("The Sum of the Complex Numbers is %.2f%+.2fi\n",cz.real,cz.img);
getche();
break;
case 6:
system("cls");
printf("Product of the Complex Numbers\n");
cz=Mul(cx,cy);
printf("The Product of the Complex Numbers is %.2f%+.2fi\n",cz.real,cz.img);
getche();
break;
case 7:
system("cls");
DisplayCN(cx,cy,cz);
break;
case 8:
exit(1);
break;
default:
system("cls");
printf("Invalid input. Please try again!!!\n");
printf("Please press the spacebar to continue!!");
getche();
break;
}
}while(selection!=8);
}
void DisplayCN(CN cx,CN cy,CN cz){
printf("The Values of the Complex Numbers:\n");
printf("CN1: %.2f%+.2fi\n",cx.real,cx.img);
printf("CN2: %.2f%+.2fi\n\n",cy.real,cy.img);
printf("Sum of the Complex Numbers\n");
cz=Sum(cx,cy);
printf("%.2f%+.2fi\n\n",cz.real,cz.img);
printf("Product of the Complex Numbers\n");
cz=Mul(cx,cy);
printf("%.2f%+.2fi\n",cz.real,cz.img);
getche();
}

