Thread: Nasty Char-Input-into-Int Bug

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    Question Nasty Char-Input-into-Int Bug

    In this array program I have due on Tuesday in my C programming course at school (which includes a menu function), I have tested this compiled program for any bugs. One major problem I found is the endless cycling of program code that occurs when I enter a non-integer character into the program (the input code is programmed for an integer). This only occurs when I enter a letter or a symbol, but not a number (I've programmed an error message for any numbers other than 1, 2, 3, or 4 in the main function, or 1, 2, or 3 in the third function). Does anyone know code I should insert so that any non-integer char characters that are entered will, instead of causing the program to crash, return an error message and recall the menu?

    Here is my code (first my own header file, and then the C file)

    Code:
    int pointsNFL2010 [2][2][2] =
    {
    {
    {518, 375},
    {435, 366}
    },
    {
    {414, 334},
    {439, 310}
    }
    };
    Code:
    #include <stdio.h>
    #include "football.h"
    
    int sel, score, score1, score2, afcScore, nfcScore, i, j, k;
    int *value, *value1, *value2, *afc1, *nfc1;
    int pointsTotal();
    int pointsConference();
    int pointsTeam();
    
    int main(void)
    {
                    printf("MENU\n");
                    printf("------------\n");
                    printf("Welcome! This program lets you view statistics for total team points scored in the NFL during the 2010 regular teams. Points are given for the four division winners in each conference (AFC and NFC) in total 'Points For' (PF), and sorted out in order of seed in playoffs (in AFC: New England Patriots, Pittsburgh Steelers, Indianapolis Colts, Kansas City Chiefs; in NFC: Atlanta Falcons, Chicago Bears, Philadelphia Eagles, Seattle Seahawks). Points can be viewed by league totals, conference totals, or by individual team.\n");
                    printf("------------\n");
                    printf("Which statistic would you like to view?\n");
                    printf("------------\n");
                    printf("1) Total points scored by all teams\n");
                    printf("2) Total points scored by conference\n");
                    printf("3) Total points scored by individual team\n");
                    printf("4) EXIT\n");
                    printf("------------\n");
                    printf("Enter a selection above: ");
                    scanf("%d", &sel);
    while (sel != 4)
                    {
                            {
                            if (sel == 1)
                                    pointsTotal();
                            else if (sel == 2)
                                    pointsConference();
                            else if (sel == 3)
                                    pointsTeam();
                            else
                                    printf("\nError! Please try again!\n\n");
                            }
                            printf("\n------------\n");
                            printf("Which statistic would you like to view?\n");
                            printf("------------\n");
                            printf("1) Total points scored by all teams\n");
                            printf("2) Total points scored by conference\n");
                            printf("3) Total points scored by individual team\n");
                            printf("4) EXIT\n");
                            printf("------------\n");
                            printf("Enter a selection above: ");
                            scanf("%d", &sel);
                    }
            printf("\nPeace out.\n");
    
            return 0;
    }
    
    int pointsTotal()
    {
            printf("\nThis function gives you the combined score for all 8 division winners.\n");
            for (i = 0; i < 2; i++){
                    for (j = 0; j < 2; j++){
                            for (k = 0; k < 2; k++){
                                    value = &pointsNFL2010[i][j][k];
                                    score += *value;
            }
            }
            }
            printf("\nThe total points scored by all 8 division winners in 2010 is %d.\n\n", score);
    
            return 1;
    }
    
    int pointsConference()
    {
            printf("\nThis function breaks down the combined score of all 8 division winners into their respective conferences.\n");
            for (i = 0; i < 1; i++){
                    for (j = 0; j < 2; j++){
                            for (k = 0; k < 2; k++){
                                    afc1 = &pointsNFL2010[0][j][k];
                                    afcScore += *afc1;}
    
            }}
            for (i = 1; i < 2; i++){
                    for (j = 0; j < 2; j++){
                            for (k = 0; k < 2; k++){
                                    nfc1= &pointsNFL2010[1][j][k];
                                    nfcScore += *nfc1;}
            }}
            printf("\nThe total points scored is %d by the AFC and %d by the NFC.\n\n", afcScore, nfcScore);
    
            return 1;
    
    }
    
    int pointsTeam()
    {
            int t;
    
            printf("\nThis function displays total points scored for two teams (one in AFC, one in NFC). For example, if you pick 1, the total points scored for the number 1 seeded teams in both the AFC and NFC will each be displayed separately.\n");
            printf("Which ranking would you like to see? Enter 1, 2, 3, or 4 :\n");
            scanf("%d", &t);
            if (t == 1)
            {
                    value1 = &pointsNFL2010[0][0][0];
                    value2 = &pointsNFL2010[0][0][1];
                    score1 = *value1;
                    score2 = *value2;
                    printf("\nThe scores for the teams ranked at the number specified are %d and %d (AFC and NFC, respectively).\n\n", score1, score2);
            }
            else if (t == 2)
            {
                    value1 = &pointsNFL2010[0][1][0];
                    value2 = &pointsNFL2010[0][1][1];
                    score1 = *value1;
                    score2 = *value2;
                    printf("\nThe scores for the teams ranked at the number specified are %d and %d (AFC and NFC, respectively).\n\n", score1, score2);
            }
            else if (t == 3)
            {
                    value1 = &pointsNFL2010[1][0][0];
                    value2 = &pointsNFL2010[1][0][1];
                    score1 = *value1;
                    score2 = *value2;
                    printf("\nThe scores for the teams ranked at the number specified are %d and %d (AFC and NFC, respectively).\n\n", score1, score2);}
            else if (t == 4)
            {
                    value1 = &pointsNFL2010[1][1][0];
                    value2 = &pointsNFL2010[1][1][1];
                    score1 = *value1;
                    score2 = *value2;
                    printf("\nThe scores for the teams ranked at the number specified are %d and %d (AFC and NFC, respectively).\n", score1, score2);
            }
            else
                    printf("\nError! Back to the main menu to try again!\n\n");
    
            return 1;
    }
    For the record, I should say that I attempted to change the input type to straight-up char (it wouldn't compile because it still regarded 1, 2, 3, 4, as ints), so when I changed them to letters a, b, c, and d (thinking I could then put code in to call the error message for any entry other than a, b, c, or d), the menu function would not run properly. This is the only way for me to get the program to run properly (with int input type).
    Last edited by kowit010; 09-17-2011 at 08:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Something Nasty
    By string in forum C Programming
    Replies: 4
    Last Post: 06-01-2008, 01:28 PM
  2. How can terminate input when input specifed char?
    By Mathsniper in forum C Programming
    Replies: 5
    Last Post: 12-11-2006, 09:59 AM
  3. Function input may char or char[] ..?
    By GSalah in forum C++ Programming
    Replies: 2
    Last Post: 11-15-2006, 04:07 AM
  4. Nasty looking thing
    By richard_hooper in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 09:40 AM

Tags for this Thread