Program accepts input but does not return result as intended

This is a discussion on Program accepts input but does not return result as intended within the C Programming forums, part of the General Programming Boards category; I'm working on a simple program that accepts scores from 20 games. It then is supposed to determine which one ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    3

    Program accepts input but does not return result as intended

    I'm working on a simple program that accepts scores from 20 games. It then is supposed to determine which one is the high score and return that to the screen. The program compiles without errors (using Code::Blocks), and it accepts all 20 inputs, however when it comes to returning the top score, it always returns 0. This is regardless of what numbers are entered. I'm thinking that either a number isn't being stored in the points2 array, highscore integer, or isn't being returned to the variable number. Any help would be much appreciated.

    Thanks,

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define RANGE 21
    int top_score(int[],int);
    int main(void)
    {
        int points[RANGE];
        int game = 0;
        int number;
        char instring [20];
        for (game = 1; game <= 20; game++)
        {
            printf ("Enter points scored in game number %d", game);
            gets (instring);
            points [game] = atoi (instring);
        }
        number = top_score(points,game);
        printf ("Highest points scored in a game is %f", number);
    }
    
    int top_score (int points2[], int game2)
    {
        int index;
        int highscore;
        highscore = points2[0];
        for (index = 1; index < game2; index++)
        {
            if (highscore < points2[index])
            {
                highscore = points2[index];
             }
        }
            return (highscore);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why %f?

    SIDE HINT: gets is bad. Really really really bad. SourceForge.net: Gets - cpwiki

  3. #3
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Array indices start at 0.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    28
    You should use
    Code:
    scanf("%d", &points[game]);
    Also, as msh said, you should use the index starting from 0.

    However, one quick thing to do is to print all the numbers on the screen to verify that your array is filling up correctly:
    Code:
     
    for (game = 0; game < 20; game++)
    printf("Score %d = %d\n", game+1, points[game]);

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    3

    Gives output value now...

    However, the value is always 2,293,580. This is strange to me because I'm usually only inputting between 1 and 3 for my individual score values. I'm not sure where this large number is coming from. I used the example from v333k to show what was being stored in my array. It shows as going in correctly. So, I'm thinking that something is wrong in my top_score function.

    Thanks msh and tabstop for the help. I have the game index starting at 1 because my array is length 21. Starting it at one prompts to user to input the score for Game 1 instead of Game 0. I figured I'm still only taking 20 entries.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define RANGE 21
    int top_score(int[],int);
    int main(void)
    {
        int points[RANGE];
        int game = 0;
        int number;
        char instring [20];
        for (game = 1; game <= 20; game++)
        {
            printf ("Enter points scored in game number %d", game);
            scanf("%d", &points[game]);
            printf("Score %d = %d\n", game, points[game]); /*shows what's in my array*/
            points [game] = atoi (instring);
            number = top_score(points,game);
        }
    
        printf ("Highest points scored in a game is %d", number);
    }
    
    int top_score (int points2[], int game2)
    {
        int index;
        int highscore;
        highscore = points2[0];
        for (index = 0; index < game2; index++)
        {
            if (highscore < points2[index])
            {
                highscore = points2[index];
             }
        }
            return (highscore);
    }

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    wouldn't adding a 'int maxScore = 0;' before your loop and insert in the middle of the for loop 'if (points[game] > maxScore) maxScore = points[game];' and finally a printf("Highest score was %d\n", maxScore); at the end be easier?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  7. #7
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Replace %f in the second printf statement with %d.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    3
    The reason why this isn't working is so simple that you're gonna kick yourself. I'll point out the offending line, try to see why it's the problem. There's another line that will cause your program to fall short on checking scores as well; i'll point that out as problem two.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define RANGE 21
    int top_score(int[],int);
    int main(void)
    {
        int points[RANGE];
        int game = 0;
        int number;
        char instring [20];
        for (game = 1; game <= 20; game++)
        {
            printf ("Enter points scored in game number %d", game);
            scanf("%d", &points[game]);
            printf("Score %d = %d\n", game, points[game]); /*shows what's in my array*/
            points [game] = atoi (instring);  <--------------- PROBLEM ONE
            number = top_score(points,game);
        }
    
        printf ("Highest points scored in a game is %d", number);
    }
    
    int top_score (int points2[], int game2)
    {
        int index;
        int highscore;
        highscore = points2[0];
        for (index = 0; index < game2; index++) <-------------- PROBLEM TWO
        {
            if (highscore < points2[index])
            {
                highscore = points2[index];
             }
        }
            return (highscore);
    }
    Hope this helps!

    (You can thank google alerts, by the way. It's the only reason why I saw this post, LoL)

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    It kind of looks like you're trying to do a bubble sort.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    3

    Smile

    Ok, I got it to work correctly filling the array correctly and outputting a correct high score.

    I still didn't figure out what I had wrong with this line of code, which fatezero pointed out for me.
    Code:
      for (index = 0; index < game2; index++) <-------------- PROBLEM TWO
    Thank you everyone who helped point me in the right direction.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    3
    Quote Originally Posted by Anarchristo View Post
    I still didn't figure out what I had wrong with this line of code, which fatezero pointed out for me.
    Code:
      for (index = 0; index < game2; index++) <-------------- PROBLEM TWO
    This for loop is only going to run from index 0, to index 20; your score values are stored at indicies 1 through 21, meaning that the last score is never going to be tested.

    If you're going for the bubblesort approach, this is definitely a good start. But a smaller, faster, approach would be something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXSCORES	20
    
    int main(void)
    {
        int points[MAXSCORES];
        int highscore=0;
    
        for (int game=0; game < 20; game++)
        {
            printf ("Enter points scored in game number %d: ", game+1);
            scanf("%d", &points[game]);
    
            if (points[game] > highscore) highscore = points[game];
        }
    
        printf ("Highest points scored in a game is %d\n", highscore);
    	return 0;
    }
    Last edited by fatezero; 05-12-2010 at 05:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 01:34 PM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21