פורסם 2009 בנובמבר 2716 שנים כתבתי תוכנית קטנה בc++ ואני נתקל בבעיה בהרצה.התוכנית מקבל קלט של 3 משתנים - תו ושני מספרים שלמים. כאשר הוא נכנסת לswitch היא מבצעת פעולה בהתאם לתו.הפלט שמתקבל אחרי שמכניסים את המשתנים פעם ראשונה הוא תקין. אבל מהפעם השניה מודפסת כל פעם ההודעת שנמצאת בdefault. למרות שבשאר הcase יש break;בנוסף לכך מודפסת על פעם ההודעה "enter one character following by two integer numbers:" שנמצאת בתחילת הלולאה do-while.צירפתי בתחתית ההודעה סקרין שוט של הפלט למיקרה שאני לא מובן כל כך.#include<stdio.h>#include<conio.h>#include<math.h>int main(){int x, y;char ch;float avg, power;clrscr();do { printf("enter one character following by two integer numbers: "); scanf("%c%d%d", &ch,&x,&y); switch (ch) { case 'Q': printf("Finish!\n\n"); break; case 'a': avg=(((float)x+(float)y)/2); if (((x+y)%2)==0) printf("the avarage of %d and %d is %0.2f - it is an integer\n\n",x,y,avg); else printf("the avarage of %d and %d is %0.2f - it is a fraction\n\n",x,y,avg); break; case 'm': if (x!=y) printf("%d is the minimum \n\n", (x<y)? x:y); else printf("%d and %y are equale \n\n", x,y); break; case 'M' : if (x!=y) printf("%d is the maximum \n\n", (x>y)? x:y); else printf("%d and %y are equale \n\n", x,y); break; case '*' : printf("%d*%d=%d \n\n", x,y, x*y); break; case '^' : power=pow((float)x,(float)y); printf("%d^%d=%0.0f \n\n", x,y, power); break; default: printf("ERROR\n\n"); }} while (ch!='Q');getch();return 0;}מקווה שמישהו יוכל לעזור לי כאן.תודה מראש :-X[attachment deleted by admin]
פורסם 2009 בנובמבר 2716 שנים הבעיה נמצאת ב-scanf.שים לב ש-c% לא מדלג על תווי רווח, טאב, או שורה חדשה כמו d%.באיטרציה הראשונה של הלולא שלך אין תווים במחסנית ה-Input והתוכנית קוראת את הקלט בצורה תקינה. אבל היא לא מנקה את המחסנית עד הסוף. היא לא מנקה את התו השורה החדשה שנכנסה כתוצאה מלחיצה על Enter לאחר הזנת שורת הקלט הראשונה. באיטרציה השנייה של הלולאה, scanf קוראת ל-ch את התו שנשאר מהאיטרציה הקודמת ומשם הכל מתדרדר...שמעון
פורסם 2009 בנובמבר 2716 שנים מחבר תודה רבה לשנייכם, עזרתם לי מאוד! במקום להשתמש בפקודת flushall יכלתי לקלוט את המשתנים בנפרד? לדוגמא ch=getchar();scanf("%d", &x);scanf("%d",&y); או שעדיין האנטר אחרי קליטת הchar הראשון יחשב כתו שנקלט ע"י המשתמש בהרצה הבאה?
פורסם 2009 בנובמבר 2716 שנים אם אתה מתכנת תחת C++ תשתמש בפונקציות הקלט\פלט ש C++ במקום באלה של C.הן הרבה יותר טובות (גם מבחינת קלות השימוש \ בטיחות \ תמיכה בהרבה סוגי stream עם ממשק זהה)חפש על cout/cin
פורסם 2009 בדצמבר 815 שנים מחבר עריכה:שיניתי את השם של הפונקציה power לpower2 וזה עובד.power הוא מונח שמור?שאלה נוספת בנוגע לאותו תרגיל(מקווה שזה בסדר להמשיך באותו שרשור ולא לפתוח דיון חדש..)בכל מיקרה, ניתבקשתי לכתוב את אותו תרגיל כאשר בכל case יש קריאה לפונקציה שמבצעת את הפעולותהתוכנית לא רצה ומחזירה שגיאת קומפילציה עבור הפוקנציהvoid power(int, int);הינה הקוד המעודכן:#include<stdio.h>#include<conio.h>#include<math.h>void avarage (int, int);void multiply (int, int);void minimum (int, int);void maximum (int, int);void gcd (int, int);void power (int, int);int main(){int x, y;char ch;float avg, power;clrscr();do { flushall(); printf("enter one character following by two integer numbers: "); scanf("%c%d%d", &ch,&x,&y); switch (ch) { case 'Q': printf("Finish!\n\n"); break; case 'a': avarage(x, y); break; case 'm': minimum(x,y); break; case 'M' : maximum(x, y); break; case '*' : multiply(x, y); break; case '^' : power(x, y); break; case 'g' : gcd(x, y); break; default: printf("ERROR\n\n"); }} while (ch!='Q');getch();return 0;}void avarage (int x, int y){ float avg; avg=(((float)x+(float)y)/2); if (((x+y)%2)==0) printf("the avarage of %d and %d is %0.1f - it is an integer\n\n",x,y,avg); else printf("the avarage of %d and %d is %0.1f - it is a fraction\n\n",x,y,avg);}void multiply (int x, int y){ printf("%d*%d=%d \n\n", x,y, x*y);}void minimum (int x, int y){ if (x!=y) printf("%d is the minimum \n\n", (x<y)? x:y); else printf("%d and %d are equale \n\n", x,y);}void maximum (int x, int y){ if (x!=y) printf("%d is the maximum \n\n", (x>y)? x:y); else printf("%d and %d are equale \n\n", x,y);}void power (int x, int y){ float power; power=pow((float)x,(float)y); printf("%d^%d=%0.5f - %s \n\n", x,y, power, ((pow((float)x,(float)y))<1)? "the result is a fraction":"the result is an integer");}void gcd (int m, int n){ int r, q, temp=m; if ((m==0) || (n==0)) printf("gcd is 0"); else { if (m<n) { m=n; n=temp; } while (((r==0)&&(n==1)) || ((r!=0)&&(n>1))) n=(m/n); r=m%n; m=n; n=r; ((r==0)&&(n==0))? (printf("no common devider\n")): (printf("common devider is %d/n", n)); } }השגיאה המתקבלת היא:Error call of nunfunctionאם מישהו יכול לומר לי מה הבעיה בפונקציה אני אודה לו מאוד
פורסם 2009 בדצמבר 815 שנים הגדרת משתנה בשם power בתוך ה-main, שדורס את הפונקציה. אז כשאתה קורא ל-power הוא חושב שאתה מנסה לקרוא למשתנה כאילו הוא פונקציה.
פורסם 2009 בדצמבר 815 שנים הייתי גם מוריד את ה getch();כי התוכנית במילא רצה בלולאה ככה כשתנסה לצאת אתה צריך ללחוץ Q אנטר ואז הוא יחכה לעוד תו.... מיותר
ארכיון
דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.