בעיה עם מספר if

danshk

New member
בעיה עם מספר if

אני מנסה לעשות תוכנה ששבתוך התנאי if האופציות יתפצלו שוב לשניים (ז"א עוד if) אך המהדר dev c++ כותב לי שיש שגיאה
if (a==0) { if (b!=0) printf("the root for this equation is x=%lf", -c/b); else { if (c==0) printf("There are endless zero point for your equation\n"); else (c!=0) printf("There aren't square roots for the above parameters.\n"); } } else​
האם משהו לא נכון במבנה הקוד? (הקוד המלא) #include <stdio.h> #include <math.h> void main() { double a,b,c,f,m; printf("Welcome To The Square Root Program\n"); printf("This program designed to calculate the square root of the square equation\n"); printf("ax^2+bx+c\n"); printf("Please enter the parameter a:\n"); scanf("%lf", &a); printf("Please enter parameter b:\n"); scanf("%lf", &b); printf("Please enter parameter c:\n"); scanf("%lf", &c); if (a==0) { if (b!=0) printf("the root for this equation is x=%lf", -c/b); else { if (c==0) printf("There are endless zero point for your equation\n"); else (c!=0) printf("There aren't square roots for the above parameters.\n"); } } else { if (b*b-4*a*c<0) { printf("There aren't square roots for the above parameters.\n"); printf("Press any key..."); scanf("%lf", &f); } if ((b*b-4*a*c)==0) { printf("There is only one square root for the above parameters."); printf("x=%lf \n",(-b+sqrt(b*b-4*a*c))/(2*a)); } else { printf("The first x (x1) is: %lf\n",(-b+sqrt(b*b-4*a*c))/(2*a)); printf("The second x (x2) is: %lf\n",(-b-sqrt(b*b-4*a*c))/(2*a)); } getchar(); getchar(); } getchar(); getchar(); }
 

alexrait1

New member
תסתכל על הelse שלך

כתבת משהו בסגנון else() ddd לא כותבים כך. אני לא רוצה להתעמק בתוכנה עכשיו, אין לי כח לזה אפילו שאני מניח שאתה מנסה לפתור משוואה ריבועית, אבל כותבים else if ואז תנאי.
 

selalerer

New member
הבעיה שלך:

כתוב:
else (c!=0)​
במקום:
else if (c!=0)​
ובאופן כללי, האינדנטציה שלך ממש גרועה, שזה יכול היה לפתור לך את הבעיה. כל בלוק שאתה פותח עם פקודות ששיכות לif (או כל פקודה/לולאה אחרת) את הפקודות שלו תקדם tab אחד ואל תעשה else if בשורה אחת, האינדנטציה הולכת לאיבוד ככה ולא תמיד ברור מה מוכל בתוך מה.
 

alexrait1

New member
האינדנטציה

זה בעיה של הדבקה. כשמדביקים קוד לכאן קורים כל מיני דברים מוזרים.
 

danshk

New member
ניסיון שני

שיניתי את זה למה שאתם רואים עכשיו, המהדר אומר שאין בעיות בתוכנה אבל שאני מנסה את הפרמטרים שאמורים להוביל לחלק הזה, התוכנה כאילו מדלגת על החלק הזה
if (a==0) { if (b!=0) printf("the root for this equation is x=%lf", -c/b); else { if(c==0) printf("There are endless zero point for your equation\n"); else printf("There aren't square roots for the above parameters.\n"); } }​
 
למעלה