Thread: Help with highscores in guessing game?

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    3

    Help with highscores in guessing game?

    My line count in the text file is 8 but somewhere my program is reading outside the array for both username and score. Im getting the huge spazzy errors with the weird characters. Any help?

    Code:
    void high_scores()
    {
        FILE *my_file1, *my_file2, *my_file3, *my_file4;
        int i = 0, scorearray[20], temp_scores, x=0, linecount, charcount, wordcount, flag = 1, counter =0;
        char usernamearray[20][20], temp_names[20], ch;
    
    
        my_file3 = fopen("username1.txt", "r");
        my_file4 = fopen("username2.txt", "r");
        
                // Initialize counter variables
             linecount = 0;
             charcount = 0;
             wordcount = 0;
           
             system("mode con: cols=60 lines=80");
    
    
           // If file opened successfully, then write the string to file
           if (my_file3)
           {
                //Repeat until End Of File character is reached.    
                   while ((ch=getc(my_file3)) != EOF) {
                         // Increment character count if NOT new line or space
                        if (ch != ' ' && ch != '\n') { ++charcount; }
               
                      // Increment line count if new line character
                       if (ch == '\n') { ++linecount; }
    
    
                       if (ch == ' '|| ch == '\n') {++wordcount;}
                   }
    
    
                   if (charcount > 0) {
                    ++linecount;
                    ++wordcount;
                   }
              
                }
            else
            {
                 printf("Failed to open the file\n");
            }
    
    
           printf("%d", linecount);
    
    
           counter = linecount/2;
    
    
            getch();
    
    
            for(i = 0; i <= counter; i++)
            {
                fscanf(my_file3, "%s\n", usernamearray[i]);
                fscanf(my_file3, "%d\n", &scorearray[i]);
            }
    
    
            
    
    
            //Initalized the sorting process
    
    
            while (flag==1)
            {
                flag=0;
                for (i=1; i == counter ; i++)
                {
                    if (scorearray[i]>scorearray[i+1])
                    {
                        flag=1;
                        temp_scores=scorearray[i];
                        scorearray[i]=scorearray[i+1];
                        scorearray[i+1]=temp_scores;
    
    
                        strcpy(temp_names, usernamearray[i]);
                        strcpy(usernamearray[i], usernamearray[i+1]);
                        strcpy(usernamearray[i+1], temp_names);
    
    
                        flag = 1;
                    }
                    
                }
                system("cls");
            }
    
    
            fclose(my_file3);
    
    
            my_file3 = fopen("username1.txt", "a");
    
    
            system("pause");
    
    
            fflush(stdin);
    
    
            for(i=1; i<=counter; i++)
            {
                printf("\t%s - %d\n\n", usernamearray[i], scorearray[i]);
    
    
            }
    
    
    
    
        system("pause");
    
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Posting the requirements for the assignment and contents of your username files would make it easier for us to help you.

    General tips, and bug I can see just by reading the code:

    • Formatting. Keep your code neatly formatted and indented. You are pretty good in this regard, but could use a little cleanup.
    • Don't use magic numbers. Define constants with descriptive names. Even if 2 things have the same value, declare 2 constants. For example, MAX_LINES and MAX_NAMES may both be 20 now. But if you wanted to change max name length to 40, then this way you make a small one-line change instead of trying to locate which
    • What are my_file1 and my_file2 for? Also you open username2.txt, but never do anything with it.
    • Your while ((ch=getc(...))) loop reads until the end of the file. So when that's done, what is left to read with fscanf? (hint: nothing).
    • fflush(stdin) is wrong. Read why.
    • Your calculation of counter (as linecount/2) seems wrong. Either that, or you're counting lines incorrectly.
    • I suspect your for loop on line 72 is wrong. The middle part i == counter is the condition for the loop to "keep going". So that loop only runs if counter is 1, and then will only run once.
    • It looks like you might end up with an off-by-one error in your sorting loop. Depending on how you fix the i == counter bit, you may end up swapping the last element with an element past the end of the array.
    • Line 76 and 87 are redundant. Remove one of them.


    And lastly: don't copy random code that you don't understand from the internet. Especially crappy code like this. It's not well written and doesn't actually do what you probably need it to do. This is the source of several of your issues.

  3. #3
    Registered User
    Join Date
    Jan 2018
    Posts
    3
    i tried removing the sorting bit altogether, no difference still reading something outside of the array.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If your code still isn't working, you need to post the latest version. We don't know which bit of the [good] advice you chose to apply, and whether you've applied it correctly.

    To make it easier for us to help you, you should post a complete but simplified version of the program that exhibits the problem.

  5. #5
    Registered User
    Join Date
    Jan 2018
    Posts
    3
    I figured it out lol, i re wrote it 100% more simply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help me with this guessing game.
    By NFlores2 in forum C++ Programming
    Replies: 4
    Last Post: 05-22-2014, 03:41 AM
  2. guessing game
    By GaitBait in forum C Programming
    Replies: 2
    Last Post: 10-03-2013, 12:29 AM
  3. Guessing game: how to quit the game?
    By hzr in forum C Programming
    Replies: 5
    Last Post: 12-18-2008, 10:53 AM
  4. guessing game
    By Inferno in forum C++ Programming
    Replies: 2
    Last Post: 09-22-2004, 05:37 PM
  5. guessing game
    By wayko in forum C++ Programming
    Replies: 11
    Last Post: 09-19-2001, 06:10 PM

Tags for this Thread