C programming
#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


Messages In This Thread
C programming - by Riddick94 - 26.04.2013, 09:10
Re: C programming - by Fredrick - 26.04.2013, 09:13
Re: C programming - by Riddick94 - 26.04.2013, 10:28
Re: C programming - by OrMisicL - 26.04.2013, 11:15
Re: C programming - by Riddick94 - 26.04.2013, 11:26
Re: C programming - by OrMisicL - 26.04.2013, 11:32
Re: C programming - by Riddick94 - 26.04.2013, 12:01
Re: C programming - by playbox12 - 26.04.2013, 15:30

Forum Jump:


Users browsing this thread: 1 Guest(s)