C programming
#1

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;
}
I did read about going back to the top of the function on the internet, and it says, that I should call function name again. But I'm almost sure about break, so.. anybody?

edit://
Lol, I made a mistake with while at the end, here:
Код:
while(answer != 'N');
should be 'Y'. Nevermind! My mistake and stupidity.
Reply
#2

Quote:
Originally Posted by Riddick94
Посмотреть сообщение
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;
}
I did read about going back to the top of the function on the internet, and it says, that I should call function name again. But I'm almost sure about break, so.. anybody?

edit://
Lol, I made a mistake with while at the end, here:
Код:
while(answer != 'N');
should be 'Y'. Nevermind! My mistake and stupidity.
you ant stupid when it comes to scripting everybody is the same make mistakes hope it worked and good work.
Reply
#3

Quote:
Originally Posted by ******
Посмотреть сообщение
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 Код:
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;
                }
            }

That is equivalent to:


pawn Код:
switch(answer)
            {
                case 'Y':
                    team_file = fopen(file_path, "wb");
                    printf("Creating file. . .\nFile created successfully! Press any key to continue. . .");
                    getch();
                    system("PAUSE");
                    break;
               
                case 'N':
                    system("PAUSE");
                    break;
            }
Yep, I know.. but I made something like this now:
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;
}
And it's working fine. Do you have any advice here for me?

But.. you said: "switch doesn't use braces", but I'm using Dev-cPP 4.9.9.2 and it doesn't work without braces for switch. I tried.
Reply
#4

just one thing, why is that
pawn Код:
else
        {
            // Close file from reading.
            fclose(team_file);
            // ....
Why do u close the file when it fails to open ?

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
Reply
#5

Well.. lets make it step by step:

Quote:
Originally Posted by OrMisicL
Посмотреть сообщение
just one thing, why is that
pawn Код:
else
        {
            // Close file from reading.
            fclose(team_file);
            // ....
Why do u close the file when it fails to open ?
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 Код:
// 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);
But maybe, there's more efficient way, if so, could you tell me please? Because I noticed that C uses standard file handling, but I stopped using it a while ago (I'm more familar with y_ini, which is a little bit different, but lets don't complain PAWN with C)

Quote:
Originally Posted by OrMisicL
Посмотреть сообщение
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
Tutor told me to use integer to return 0 always, because It's gonna mean:
Quote:
Originally Posted by Tutor
0 run time errors
And in other point, I can use return 1 if I need. That's what he said.
Reply
#6

Quote:
Originally Posted by Riddick94
Посмотреть сообщение
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 Код:
// 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);
But maybe, there's more efficient way, if so, could you tell me please? Because I noticed that C uses standard file handling, but I stopped using it a while ago (I'm more familar with y_ini, which is a little bit different, but lets don't complain PAWN with C)
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

Quote:
Originally Posted by Riddick94
Посмотреть сообщение
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.
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
Reply
#7

Quote:
Originally Posted by OrMisicL
Посмотреть сообщение
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
Right, I removed fclose, works same, you're right.

Quote:
Originally Posted by OrMisicL
Посмотреть сообщение
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
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)
Reply
#8

Quote:
Originally Posted by Riddick94
Посмотреть сообщение
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)
AFAIK comments are filterd out by the preprocessor, so it has nothing to do with the programming language itself (but with the compiler).

And if a teacher is trying to force his way down your throat he's teaching wrong. It's better to give someone the tools nessercary and let them learn it themselves. (from my experience a lot of teachers don't know what they're talking about*)

*What I mean is that there are usally multiple ways to a goal and you should definitely experiment with other methods and not follow your teacher blindly only then will you learn to think outside the box.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)