עזרה בשפת c (משהו קטן)

ronib25

New member
עזרה בשפת c (משהו קטן)

היי. התבקשתי בתרגיל מסויים לקלוט תו ולהדפיס הודעות בהתאם בקובץ מסויים.
כמו כן נדרשתי להתייחס לכל אפשרות של שגיאה בקלט(התייחסתי למקרה שישר מוקלד enter ולמקרה שמוקלד תו שהוא לא חלק מהתפריט), מה שנותר לי זה להתייחס למקרה בו הקלט הוא יותר מתו בודד. ואני דיי מסתבך עם זה בגלל הקליטה ב scanf. מצד שני אני יכול לקלוט ע״י gets אבל אז זה גם יכול לגרור לבעיות...
אשמח אם תוכלו קצת לכוון אותי לפיתרון האידיאלי מבחינתכם...כמו כן אוסיף חלק מהקוד המדובר שתבינו מה הולך ואת השאלה...
תודה לעוזרים.
קוד:
int main()
{

	FILE* fp;
	int n = 0;
	Uni U;
	char C;
	int i;
	int flag = 0;


	printf("Enter function <A,B,C,D,E> : ");                           // asking the user to pick a function
	scanf(" %c", &C);

	while (C == '\n')
	{
		fp = fopen("OUTPUT.txt", "w");
		fprintf(fp, "Not Available");                         // writing an error message to the file
		fclose(fp);
		printf("Enter function <A,B,C,D,E> : ");                           // asking the user to pick a function
		scanf(" %c", &C);
	}

	while (C != 'E')                                              // input option 'e' closes the menu loop and ends the program
	{
		if (C == '\n'){
			//	printf("\nEnter function <A,B,C,D,E> : ");                       // asking the user to pick the next function 
			flushall();
			scanf(" %c", &C);
		}
		if (C == 'A')
		{
			if (!(fp = fopen("INPUT.txt", "r")))                         // checking if the file was opened correctly, else reports it
				error("File Not open !");

			fseek(fp, 0, 2);                                           // checking if there are any student info in the file
			if (!ftell(fp))
				error("Input file is empty !");
			fseek(fp, 0, 0);										     // returns 'fp' to the beginning of the file  

			n = Function_A(fp, &U);									 // reading student info from file	and returns the number of students 									
			flag = 1;
			fclose(fp);												 //close file
		}

		if (C == 'B')
		{

			if (flag == 1)                                              // checking if the functions are being called in the right order
			{
				if (!(fp = fopen("OUTPUT.txt", "w")))				     // checking if the file was opened correctly, else reports it
					error("File Not open !");
				Function_B(fp, U, n);                           // calling function to perform option 'b' after checking that everything is ok to go
				flag = 2;                                              // setting the flag for the next function
				fclose(fp);
			}
			else                                                     // in case the functions weren't called in the right order
			{
				if (!(fp = fopen("OUTPUT.txt", "w")))                     // oprnin the file in writing format
					error("File Not open !");                         // showing an error message on screen
				fprintf(fp, "Not Available");                         // writing an error message to the file
				fclose(fp);
			}
		}

		if (C == 'C')
		{
			if (!(fp = fopen("OUTPUT.txt", "w")))                         // checking if the file was opened correctly, else reports it
				error("File Not open !");                         // showing an error message on screen
			if (flag == 2)                                              // chicking if the functions are being called in the right order
			{
				Function_C(fp, U, n);                                  // calling function to perform option 'c' after checking that everything is ok to go
				flag = 3;                                              // setting the flag for the next function
				fclose(fp);
			}
			else                                                     // in case the functions weren't called in the right order
			{
				fprintf(fp, "Not Available");                         // writing an error message to the file
				fclose(fp);
			}
		}

		if (C == 'D')
		{
			if (!(fp = fopen("OUTPUT.txt", "w")))  &nbs






 

ronib25

New member
הרגילות...

scanf
getchar
gets
אני חושב שבזה זה מסתכם...
אולי ליצור פשוט כקלט מחרוזת ולעשות בדיקה על פי אורך הקלט ?
 

BravoMan

Active member
הוא מתכוון כנראה שהאופציה שהצעת, לבדוק אורך,

היא פתרון אפשרי אחד.
&nbsp
פתרון אחר הוא להשתמש בפונקציה שחוזרת מיד אחרי קטל תו בודד, לא משנה מה התו ולא מאפשרת למשתמש להקליד עוד תווים.
&nbsp
פונקציה כזו תהיה עדיפה למימוש תפריט, כי היא תחסוך למשתמש את ה-enter.
 

ronib25

New member
אתה יכול לחדד את הדרך השנייה

אני צריך למשל להתייחס למקרה בו אני אקליד AC ביחד ואז בקובץ יודפס not availible האם באפשרות השנייה שציינת זה אפשרי ? כלומר A שייך לתפריט, אבל AC לא...
במידה וזה אפשרי אשמח להבין ממך איך לעשות זאת פחות או יותר כי לא עלה לי רעיון שכזה...
 

ronib25

New member
שאלה

הגדרתי מחרוזת בת 2 מקומות ונתתי תנאים למקום הראשון שבמידה והוא חלק מהתפריט ותנאים למקרה שהמקום ה2 הוא לא 0\. האם במידה ואני מכניס מחרוזת של 10 מקומות למשל תהיה איזה משמעות לכך ?
אם השאלה לא כל כך ברורה אני יכול להעלות את הקוד המדובר לבקשתכם..
 

BravoMan

Active member
אל תנחש! תבדוק.

אין שום סיבה לשאול בפורום או לנחש תשובות לדבר פשוט כ"כ שאתה יכול לבדוק בעצמך תוך שניה ולגלות, וגם ללמוד משהו על הדרך.
&nbsp
לא שקשה לי לענות לך, פשוט עדיף, בשבילך, שהקומפיילר שלך יענה על השאלה הזו.
&nbsp
לגבי מה שהצעתי:
בין הפונקציות שאתה מכיר יש אחת שמאפשרת להקליד רק תו אחד בודד ומחזירה אותו.
(לפחות, במימוש מיקרוסופטי שלה, ואאל"ט אתה עובד עם VS).
&nbsp
אז, אם תשתמש בה, מן הסתם לא תצטרך לשבור את הראש בשאלה "מה אם המשתמש הקליד יותר תווים".
 

ronib25

New member
אני הרצתי וזה עובד..

אני שואל מכיוון שכאן יודעים את התאוריה פי כמה וכמה ממני...
הכוונה שלי היא שמה שציינתי יכול מאוד להיות שמישהו כאן יודע ישר לראות איזה שגיאה תעקוף את מה שחשבתי, ואני לא אעלה עליה בהרצות כאלה ואחרות...
האמת שניסיתי עם getchar...מה הכוונה בדבריך מחזירה אותו ??
ואתה לא טועה אני עובד עם VS..
 

BravoMan

Active member
נו, מה getchar עושה?

אם התאוריה מעניינת אותך, אז, קלט שגוי, מבחינתך, מתחלק 2 אפשרויות:
&nbsp
1. המשתמש לחץ על מקש בודד, אבל המקש אינו אחת מהאופציות.
2. המשתמש לחץ על הרבה מקשים (ואז לא משנה אם הראשון הוא אופציה בתוקף או לא).
&nbsp
המקרה הראשון פשוט: תבדוק איזה תו קיבלת.
במקרה השני, יש לך 2 אפשרויות:
או שתבדוק כמה תווים קיבלת ואם זה יותר מ-1 תחליט שהקלט לא חוקי,
או תכתוב את התוכנה כך שבכלל לא תאפשר למשתמש להקליד תווים מרובים.
&nbsp
וזו כל התורה...
&nbsp
"מזירה אותו" הכוונה לכך שב-getchar אתה מקבל ערך חוזר מפונקציה שהוא התו הנקלט, במקום להעביר לה מצביע למקום שבו היא תשים את התו כפי שאתה עושה עם scanf למשל.
 

ronib25

New member
הבנתי

getchar קולטת תן בודד...
מה שציינת זה מה שעשיתי...
רק הוספתי בדיקה נוספת אם לוחצים ישר אנטר ללא כלום...
אם יש תו כלשהו ואחרי 0\ ועוד בדיקה אם התו הוא לא מהתפריט...
תודה רבה פתרתי את התרגיל. :)
 

ronib25

New member
עליתי על עוד בעיה ואני לא יודע כיצד לפתור אותה בלי להפוך את

כל הקוד..
אשמח לעצה. הבעיה היא כאשר אני מקליד enter בודד ללא כלום מה שקורה זה שפשוט במסך השחור יורדים שורה ומחכים להכנסת קלט, אם שוב אני לוחץ enter שוב יורדת שורה עד שמוקלד תו כלשהו.
הבעיה שלי היא שאני צריך לאחר הקלדת האנטר הראשון שהתוכנית תקבל אותו ולא תירד שורה בעצם...
אני אוסיף כאן את החלק של הקוד המדובר...
קוד:
int main()
{

	FILE* fp;
	int n = 0;
	Uni U;
	char C[2];
	int i;
	int flag = 0;


	printf("Enter function <A,B,C,D,E> : ");                           // asking the user to pick a function
	scanf(" %s", &C);

	while (C[1] != '\0')
	{
		fp = fopen("OUTPUT.txt", "w");
		fprintf(fp, "Not Available");                         // writing an error message to the file
		fclose(fp);
		printf("\nEnter function <A,B,C,D,E> : ");                           // asking the user to pick a function
		scanf(" %s", &C);
	}

	while (C[1] != '\0')
	{
		fp = fopen("OUTPUT.txt", "w");
		fprintf(fp, "Not Available");                         // writing an error message to the file
		fclose(fp);
		printf("Enter function <A,B,C,D,E> : ");                           // asking the user to pick a function
		scanf(" %s", &C);
	}

		while (C[0] == '\0')
	{
		fp = fopen("OUTPUT.txt", "w");
		fprintf(fp, "Not Available");                         // writing an error message to the file
		fclose(fp);
		printf("Enter function <A,B,C,D,E> : ");                           // asking the user to pick a function
		scanf(" %s", &C);
	}

	while (C[0] != 'E')                                              // input option 'e' closes the menu loop and ends the program
	{
		while (C[1] != '\0')
		{
			fp = fopen("OUTPUT.txt", "w");
			fprintf(fp, "Not Available");                         // writing an error message to the file
			fclose(fp);
			printf("\nEnter function <A,B,C,D,E> : ");                           // asking the user to pick a function
			scanf(" %s", &C);
		}
		if (C[0] == '\0'){
			//	printf("\nEnter function <A,B,C,D,E> : ");                       // asking the user to pick the next function 
			flushall();
			scanf(" %s", &C);
		}
		if (C[0] == 'A')
		{
			if (!(fp = fopen("INPUT.txt", "r")))                         // checking if the file was opened correctly, else reports it
				error("File Not open !");

			fseek(fp, 0, 2);                                           // checking if there are any student info in the file
			if (!ftell(fp))
				error("Input file is empty !");
			fseek(fp, 0, 0);										     // returns 'fp' to the beginning of the file  

			n = Function_A(fp, &U);									 // reading student info from file	and returns the number of students 									
			flag = 1;
			fclose(fp);												 //close file
		}

		if (C[0] == 'B')
		{

			if (flag == 1)                                              // checking if the functions are being called in the right order
			{
				if (!(fp = fopen("OUTPUT.txt", "w")))				     // checking if the file was opened correctly, else reports it
					error("File Not open !");
				Function_B(fp, U, n);                           // calling function to perform option 'b' after checking that everything is ok to go
				flag = 2;                                              // setting the flag for the next function
				fclose(fp);
			}
			else                                                     // in case the functions weren't called in the right order
			{
				if (!(fp = fopen("OUTPUT.txt", "w")))                     // oprnin the file in writing format
					error("File Not open !");                         // showing an error message on screen
				fprintf(fp, "Not Available");                         // writing an error message to the file
				fclose(fp);
			}
		}

		if (C[0] == 'C')
		{
			if (!(fp = fopen("OUTPUT.txt", "w")))                         // checking if the file was opened correctly, else reports it
				error("File Not open !");                         // showing an error message on screen
			if (flag == 2)                   &nb
 
למעלה