int PromptUserForFilePaths(void) { // Local variables. int count = 0; char answer; do { while(count != 1) { switch(count) { // Prompt for BATTING team file path. case 0: { printf("Please enter BATTING team file location below:\n(format: Disk:\\Folder\\FileName)\n"); scanf("%s", file_path); strcpy(batt_path, file_path); break; } // Prompt for FIELDING team file path. case 1: { printf("Please enter FIELDING team file location below:\n(format: Disk:\\Folder\\FileName)\n"); scanf("%s", file_path); strcpy(batt_path, file_path); break; } } count += 1; } // Open each file for reading binary. team_file = fopen(file_path, "rb"); // If file doesn't exists in the given location. if(!team_file) { // Close file from reading. fclose(team_file); // Prompt user to create file in the given location. printf("That file doesn't exists!\nCreate a new file in that location? (Y/N)"); answer = toupper(getch()); // Check, which option user chosen from prompt above. switch(answer) { case 'Y': { team_file = fopen(file_path, "wb"); printf("Creating file. . .\nFile created successfully! Press any key to continue. . ."); getch(); } case 'N': { system("PAUSE"); break; } } } } while(answer != 'N'); return 0; }
while(answer != 'N');
Haya!
I'm doing (re-writing) code for my tutor in College in UK, and I'm stuck about going back to the top of function if user press 'N' key, which means 'NO'. Well, I know I should start a loop (using do & while iteration) and check in while, if my answer is 'N', use a break; statement and it should go back to the top. But it's not working. Probably I'm doing something wrong (not probably), but I need your help with it. Here's the code: Код:
int PromptUserForFilePaths(void) { // Local variables. int count = 0; char answer; do { while(count != 1) { switch(count) { // Prompt for BATTING team file path. case 0: { printf("Please enter BATTING team file location below:\n(format: Disk:\\Folder\\FileName)\n"); scanf("%s", file_path); strcpy(batt_path, file_path); break; } // Prompt for FIELDING team file path. case 1: { printf("Please enter FIELDING team file location below:\n(format: Disk:\\Folder\\FileName)\n"); scanf("%s", file_path); strcpy(batt_path, file_path); break; } } count += 1; } // Open each file for reading binary. team_file = fopen(file_path, "rb"); // If file doesn't exists in the given location. if(!team_file) { // Close file from reading. fclose(team_file); // Prompt user to create file in the given location. printf("That file doesn't exists!\nCreate a new file in that location? (Y/N)"); answer = toupper(getch()); // Check, which option user chosen from prompt above. switch(answer) { case 'Y': { team_file = fopen(file_path, "wb"); printf("Creating file. . .\nFile created successfully! Press any key to continue. . ."); getch(); } case 'N': { system("PAUSE"); break; } } } } while(answer != 'N'); return 0; } edit:// Lol, I made a mistake with while at the end, here: Код:
while(answer != 'N'); |
That looks like PAWN code, and unfortunately that's going to cause you problems. C doesn't use braces in "switch" and has fallthrough, so this is wrong:
pawn Код:
That is equivalent to: pawn Код:
|
int PromptUserForFilePaths(void)
{
// Local variables.
int count = 0;
char answer;
while(count != 2)
{
clrscr();
switch(count)
{
// Prompt for BATTING team file path.
case 0:
{
printf("[Format: Disk:\\Folder\\FileName]\nPlease enter BATTING team file location below: ");
scanf("%s", file_path);
strcpy(batt_path, file_path);
break;
}
// Prompt for FIELDING team file path.
case 1:
{
printf("[Format: Disk:\\Folder\\FileName]\nPlease enter FIELDING team file location below: ");
scanf("%s", file_path);
strcpy(fiel_path, file_path);
break;
}
}
// Open each file for reading binary.
team_file = fopen(file_path, "rb");
// If file doesn't exists in the given location.
if(team_file)
{
count += 1;
printf("\nFile is already created there!\nPress any key to continue. . .");
getch();
}
else
{
// Close file from reading.
fclose(team_file);
// Prompt user to create file in the given location.
printf("\nERROR: That file doesn't exists!\nCreate a new file in that location (Y/N)?");
answer = toupper(getch());
// Check, which option user chosen from prompt above.
switch(answer)
{
case 'Y':
{
team_file = fopen(file_path, "wb");
printf("\n\nCreating file. . .\nFile created successfully! Press any key to continue. . .");
fclose(team_file);
count += 1;
getch();
break;
}
case 'N':
{
char team_name[8];
if(!count)strcpy(team_name, "BATTING");
else strcpy(team_name, "FIELDING");
printf("\n\nPress any key, to type %s team file location again. . .", team_name);
getch();
break;
}
}
}
}
return 0;
}
else
{
// Close file from reading.
fclose(team_file);
// ....
just one thing, why is that
pawn Код:
|
// Open each file for reading binary.
team_file = fopen(file_path, "rb");
// If file exists in the given location.
if(team_file)
{
count += 1;
printf("\nFile is already created there!\nPress any key to continue. . .");
getch();
}
else
{
// Close file from reading.
fclose(team_file);
also, this is not a bug but, why the function return type is integer ? the func always returns 0 so u could just use "void" as a type and remove the return, but as i said its not a big deal, just to point it out
|
Originally Posted by Tutor
0 run time errors
|
Well.. lets make it step by step:
When it fails to open, i'm closing file handler to open it again for writing binary, because earlier I used it for read binary, to let me check if its created or not. pawn Код:
|
Tutor told me to use integer to return 0 always, because It's gonna mean:
And in other point, I can use return 1 if I need. That's what he said. |
I dont think u're supposed to close the file handle when it fails to open, simply cuz the file handle is "NULL" and the file is not even open
|
it really depends on the function, some functions returns 0 when succeeded, some returns 1, some returns something totally differente depending on its role, so to resume returning "0" in an own created function dosent always mean "runtime error" (unless u decide to make it so)
also, i dont see u returning 1 when the function succeeds, u're always returning 0 which is in ur case an error |
Right, I removed fclose, works same, you're right.
Well, that's true, I'm not returning 1 anywhere, but anyway that's his words.. "always return 0", alright, so I will change it to void then. Thanks for advice! edit:// Anyway, one of my tutors (he left) told me to do not use double slash (//) to comment one line. Because it's working only in C++ language. And we must use slash - star (/* .. */). He shout on me when I used double slash, to comment one line, he was really annoying, why we can't use it, if we have it? let's make life easier.. Thankfully he's gone. But as we see.. my tutors (all) doesn't have any clue about programming (because I trust you) |