עזרה בשפת c (משהו קטן)
היי. התבקשתי בתרגיל מסויים לקלוט תו ולהדפיס הודעות בהתאם בקובץ מסויים.
כמו כן נדרשתי להתייחס לכל אפשרות של שגיאה בקלט(התייחסתי למקרה שישר מוקלד enter ולמקרה שמוקלד תו שהוא לא חלק מהתפריט), מה שנותר לי זה להתייחס למקרה בו הקלט הוא יותר מתו בודד. ואני דיי מסתבך עם זה בגלל הקליטה ב scanf. מצד שני אני יכול לקלוט ע״י gets אבל אז זה גם יכול לגרור לבעיות...
אשמח אם תוכלו קצת לכוון אותי לפיתרון האידיאלי מבחינתכם...כמו כן אוסיף חלק מהקוד המדובר שתבינו מה הולך ואת השאלה...
תודה לעוזרים.
היי. התבקשתי בתרגיל מסויים לקלוט תו ולהדפיס הודעות בהתאם בקובץ מסויים.
כמו כן נדרשתי להתייחס לכל אפשרות של שגיאה בקלט(התייחסתי למקרה שישר מוקלד 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