I don't understand how to make a Pointer Structure can someone help me to modify my program?!:worried: 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(); }
as a start do the following Code: ........ void DisplayCN[COLOR=Red](CN,CN,CN);[/COLOR]//add this int main(void){ ......... insert a getchar(); after the scanf's before getche(); for example Code: .............. 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); [COLOR="Red"]getchar();[/COLOR] getche(); break; case 2: .............
isnumber function ============== check before atoi,atof the string you want to convert Code: #include <stdio.h> #include <ctype.h> #include <string.h> int isNumber(char []); int isNumber(char text[]){ int isit=1; for(int i=0;i<strlen(text);i++){ if (!isdigit(text[i])){ isit=0; break; } } return isit; } int main (){ char str1[]="1g776ad"; char str2[]="1776d"; char str3[]="g1776"; char str4[]="1776"; printf("\n<1=true , 0=false> isnumber=%d",isNumber(str1)); printf("\n<1=true , 0=false> isnumber=%d",isNumber(str2)); printf("\n<1=true , 0=false> isnumber=%d",isNumber(str3)); printf("\n<1=true , 0=false> isnumber=%d",isNumber(str4)); getchar(); return 0; }
the above code (isnumber function) is suitable only for atoi function to get an integer from string. for atof you need some more changes for instance you can only have one . in your string you can have one - but only as the first char for example -1.55 but if you want numbers like -10.5e-03 it wants some more things to implement. still only one . - can be 2 e can exist but it must be followed by + or - and at least one number after the signs. happy coding!!
I understand the isnumber function but the problem is that I don't know what are the initialized value inside the array and what how can I make a pointer structure in the inputs or in the printing, here is an example: Code: [COLOR=#000000]#include <stdio.h>[/COLOR] [COLOR=#000000]struct card [/COLOR][COLOR=#000000]{ [/COLOR] [COLOR=#ffffff] [/COLOR][COLOR=#7f0055][B]char [/B][/COLOR][COLOR=#000000]*face; [/COLOR] [COLOR=#ffffff] [/COLOR][COLOR=#7f0055][B]char [/B][/COLOR][COLOR=#000000]*suit; [/COLOR] [COLOR=#000000]}[/COLOR][COLOR=#000000];[/COLOR] [COLOR=#7f0055][B]int [/B][/COLOR][COLOR=#000000]main[/COLOR][COLOR=#000000]()[/COLOR] [COLOR=#000000]{ [/COLOR] [COLOR=#ffffff] [/COLOR][COLOR=#000000]struct card aCard; [/COLOR] [COLOR=#ffffff] [/COLOR][COLOR=#000000]struct card *cardPtr;[/COLOR] [COLOR=#ffffff] [/COLOR][COLOR=#000000]aCard.face = [/COLOR][COLOR=#2a00ff]"Ace"[/COLOR][COLOR=#000000]; [/COLOR] [COLOR=#ffffff] [/COLOR][COLOR=#000000]aCard.suit = [/COLOR][COLOR=#2a00ff]"Spades"[/COLOR][COLOR=#000000];[/COLOR] [COLOR=#ffffff] [/COLOR][COLOR=#000000]cardPtr = &aCard;[/COLOR] [COLOR=#ffffff] [/COLOR][COLOR=#000000]printf[/COLOR][COLOR=#000000]( [/COLOR][COLOR=#2a00ff]"%s%s%s\n%s%s%s\n%s%s%s\n"[/COLOR][COLOR=#000000], aCard.face, [/COLOR][COLOR=#2a00ff]" of "[/COLOR][COLOR=#000000], aCard.suit,[/COLOR] [COLOR=#ffffff] [/COLOR][COLOR=#000000]cardPtr->face, [/COLOR][COLOR=#2a00ff]" of "[/COLOR][COLOR=#000000], cardPtr->suit, [/COLOR] [COLOR=#ffffff] [/COLOR][COLOR=#000000]( [/COLOR][COLOR=#000000]*cardPtr [/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000].face, [/COLOR][COLOR=#2a00ff]" of "[/COLOR][COLOR=#000000], [/COLOR][COLOR=#000000]( [/COLOR][COLOR=#000000]*cardPtr [/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000].suit [/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]; [/COLOR] [COLOR=#ffffff] [/COLOR] [COLOR=#ffffff] [/COLOR][COLOR=#7f0055][B]return [/B][/COLOR][COLOR=#990000]0[/COLOR][COLOR=#000000];[/COLOR] [COLOR=#000000]}[/COLOR]
here is a small example for you. Code: [COLOR=YellowGreen]#include<stdio.h> #include<string.h> #include<stdlib.h>[/COLOR] struct employee{ char *name; int code; float salary; }*emp; int howmany=0; [COLOR=Blue]void show_employees(),free_memory();[/COLOR] [COLOR=Blue]int main()[/COLOR]{ char name[30]; int i=0,epilogi; name[0]='1'; printf("\nGive * for name to stop adding employees!"); while (name[0]!='*'){ i++; if (i==1) emp=(struct employee *) malloc(sizeof(struct employee)); else emp=(struct employee *) realloc(emp,i*sizeof(struct employee)); if (emp==0){ printf("\nnot enough memory!!"); name[0]='*'; } else{ printf ("\nEmployee No: %d\nGive name: ",i); scanf("%s",name); getchar(); } if (name[0]!='*'){ emp[i-1].name=(char *) malloc(strlen(name)+1); strcpy(emp[i-1].name,name); printf("Give code : "); scanf("%d",&emp[i-1].code); getchar(); printf("Give salary : "); scanf("%f",&emp[i-1].salary); getchar(); howmany=i; } } show_employees(); free_memory(); printf("end of program.\n"); getchar(); exit(0); return 0; }//end of main [COLOR=Blue]void show_employees()[/COLOR]{ int i; for (i=0; i<howmany; i++) printf ("|%5d | name:%20s | code:%8d | salary:%10.2f |\n",i+1,emp[i].name,emp[i].code,emp[i].salary); } [COLOR=Blue]void free_memory()[/COLOR]{ int i; for (i=0;i<howmany;i++){//free memory pointers free(emp[i].name); } free(emp); }
Sir can you help me figure out the problem in displaying all the complex numbers here is my new code 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 cxr,cyr,czr; double cxi,cyi,czi; }CN; CN Sum(CN CN1){ CN1.czr=CN1.cxr+CN1.cyr; CN1.czi=CN1.cxi+CN1.cyi; return CN1; } CN Mul(CN CN1){ double a,b,c,d; a=CN1.cxr*CN1.cyr; b=CN1.cxr*CN1.cyi; c=CN1.cxi*CN1.cyr; d=-1.0*(CN1.cxi*CN1.cyi); CN1.czr=a+d; CN1.czi=b+c; return CN1; } void DisplayCN(CN *p); int main(void){ CN *ptr,CN1,cz; ptr=&CN1; char error[128]; int selection; ptr->cxr=1.0; ptr->cxi=-2.0; ptr->cyr=3.0; ptr->cyi=4.0; do{ system("cls"); printf("The Starting Values:\n"); printf("CN1: %.2f%+.2fi\n",ptr->cxr,ptr->cxi); printf("CN2: %.2f%+.2fi\n\n",ptr->cyr,ptr->cyi); 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",ptr->cxr,ptr->cxi); printf("New Value for the Real Number: "); scanf("%lf",&CN1.cxr); getchar(); getche(); break; case 2: system("cls"); printf("Change the Value of the Imaginary Number\n"); printf("CN1: %.2f%+.2fi\n",ptr->cxr,ptr->cxi); printf("New Value for the Imaginary Number: "); scanf("%lf",&CN1.cxi); getchar(); getche(); break; case 3: system("cls"); printf("Change the Value of the Real Number\n"); printf("CN2: %.2f%+.2fi\n",ptr->cyr,ptr->cyi); printf("New Value for the Real Number: "); scanf("%lf",&CN1.cyr); getchar(); getche(); break; case 4: system("cls"); printf("Change the Value of the Imaginary Number\n"); printf("CN2: %.2f%+.2fi\n",ptr->cyr,ptr->cyi); printf("New Value for the Imaginary Number: "); scanf("%lf",&CN1.cyi); getchar(); getche(); break; case 5: system("cls"); printf("Sum of the Complex Numbers\n"); cz=Sum(CN1); printf("The Sum of the Complex Numbers is %.2f%+.2fi\n",CN1.czr,CN1.czi); getche(); break; case 6: system("cls"); printf("Product of the Complex Numbers\n"); CN1=Mul(CN1); printf("The Product of the Complex Numbers is %.2f%+.2fi\n",CN1.czr,CN1.czi); getche(); break; case 7: system("cls"); DisplayCN(ptr); 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 *p){ CN CN1; printf("The Values of the Complex Numbers:\n"); printf("CN1: %.2f%+.2fi\n",p->cxr,p->cxi); printf("CN2: %.2f%+.2fi\n\n",p->cyr,p->cyi); printf("Sum of the Complex Numbers\n"); CN1=Sum(CN1); printf("%.2f%+.2fi\n\n",CN1.czr,CN1.czi); printf("Product of the Complex Numbers\n"); CN1=Mul(CN1); printf("%.2f%+.2fi\n",CN1.czr,CN1.czi); getche(); }
see the changes below 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 cxr,cyr,czr; double cxi,cyi,czi; }CN; CN * Sum(CN *CN1){ CN1->czr=CN1->cxr+CN1->cyr; CN1->czi=CN1->cxi+CN1->cyi; return CN1; } CN * Mul(CN *CN1){ double a,b,c,d; a=CN1->cxr*CN1->cyr; b=CN1->cxr*CN1->cyi; c=CN1->cxi*CN1->cyr; d=-1.0*(CN1->cxi*CN1->cyi); CN1->czr=a+d; CN1->czi=b+c; return CN1; } void DisplayCN(CN *p); int main(void){ CN *ptr; char error[128]; int selection; ptr=(CN *) malloc(sizeof(CN)); ptr->cxr=1.0;//first number ptr->cxi=-2.0; ptr->cyr=3.0;//second number ptr->cyi=4.0; do{ system("cls"); printf("The Starting Values:\n"); printf("CN1: %.2f%+.2fi\n",ptr->cxr,ptr->cxi); printf("CN2: %.2f%+.2fi\n\n",ptr->cyr,ptr->cyi); 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",ptr->cxr,ptr->cxi); printf("New Value for the Real Number: "); scanf("%lf",&ptr->cxr);//CN1.cxr); getchar(); getche(); break; case 2: system("cls"); printf("Change the Value of the Imaginary Number\n"); printf("CN1: %.2f%+.2fi\n",ptr->cxr,ptr->cxi); printf("New Value for the Imaginary Number: "); scanf("%lf",&ptr->cxi);//CN1.cxi); getchar(); getche(); break; case 3: system("cls"); printf("Change the Value of the Real Number\n"); printf("CN2: %.2f%+.2fi\n",ptr->cyr,ptr->cyi); printf("New Value for the Real Number: "); scanf("%lf",&ptr->cyr);//CN1.cyr); getchar(); getche(); break; case 4: system("cls"); printf("Change the Value of the Imaginary Number\n"); printf("CN2: %.2f%+.2fi\n",ptr->cyr,ptr->cyi); printf("New Value for the Imaginary Number: "); scanf("%lf",&ptr->cyi);//CN1.cyi); getchar(); getche(); break; case 5: system("cls"); printf("Sum of the Complex Numbers\n"); ptr=Sum(ptr); printf("The Sum of the Complex Numbers is %.2f%+.2fi\n",ptr->czr,ptr->czi); getche(); break; case 6: system("cls"); printf("Product of the Complex Numbers\n"); ptr=Mul(ptr); printf("The Product of the Complex Numbers is %.2f%+.2fi\n",ptr->czr,ptr->czi); getche(); break; case 7: system("cls"); DisplayCN(ptr); 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 *p){ printf("The Values of the Complex Numbers:\n"); printf("CN1: %.2f%+.2fi\n",p->cxr,p->cxi); printf("CN2: %.2f%+.2fi\n\n",p->cyr,p->cyi); printf("Sum of the Complex Numbers\n"); p=Sum(p); printf("%.2f%+.2fi\n\n",p->czr,p->czi); printf("Product of the Complex Numbers\n"); p=Mul(p); printf("%.2f%+.2fi\n",p->czr,p->czi); getche(); }