Thread: Why won't my program terminate or output txt?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    Why won't my program terminate or output txt?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    int avgcalc(int score1, int score2, int score3);
    int starassign(int avg);
    void output(int avg, int numbowlers, int highscore, int numstars, char *name);
    void numberstars(int numstars);
     
    int main(void)
    {
            char name[40], highname[40];
            int score1, score2, score3, avg, highscore, numstars, numbowlers = 1, totalscore = 0, totalavg, choice;
     
            do
            {
            printf("Bowler's Name:");                                                                       //User Input
            scanf("%s", &name);
            reinput:
    //20
            printf("Input three(3) scores between 0 and 300 with spaces in between:");
            scanf("%d %d %d", &score1, &score2, &score3);
     
            if((score1 < 0) || (score1 > 300))                                                              //Check score ranges
                    goto reinput;
            else if((score2 < 0) || (score2 > 300))
                    goto reinput;
            else if((score3 < 0) || (score3 > 300))
                    goto reinput;
    //30
            avg = avgcalc(score1, score2, score3);                                                          //Calculate Average
            numstars = starassign(avg);                                                                     //Assign Number of Stars
     
            totalscore +=avg;                                                                               //Calculate Total Average
            totalavg = totalscore/numbowlers;
     
            if(avg > highscore)                                                                             //Determine if a high score is reached
            {
                    highscore = avg;
                    strcpy(highname, name);
                    printf("HIGH SCORE!!!");
                    }
    output(avg, numbowlers, highscore, numstars, &name[40]);                                                //Output
     
            printf("\nCumulative Average: %d\nBowler with the Highest Average %c\nBowlers Average: %d", totalavg, highname, highscore);
     
            printf("\n\nEnter Another Bowler? (1 for Y or 2 for N)");                                       //More bowlers??
            scanf("%d", &choice);
                    numbowlers++;
            }while(choice = 1);
     
     
            exit(0);
    }
    //58
    int avgcalc(int score1, int score2, int score3)
    {
            int avg = (score1 + score2 + score3)/3;
            return avg;
    }
    //64
    int starassign(int avg)
    {
            int numstars;
     
            if(avg >= 200){
                    numstars = 4;
            }
            else if(avg >= 170){
                    numstars = 3;
            }
            else if(avg >= 125){
                    numstars = 2;
            }
            else if(avg >= 100){
                    numstars = 1;
            }
            else{
                    numstars = 0;
            }
    //83
            return numstars;
    }
     
    void output(int avg, int numbowlers, int highscore, int numstars, char *name)
    {
            int i;
            printf("\nBowler's name:");
            for(i = 0; name[i]; i++)
                    putchar(name[i]);
            putchar('\n');
            printf("\nBowler's Average: %d\nNumber of Stars Earned:", avg);
            numberstars(numstars);
    }
    //92
    void numberstars(int numstars)
    {
            switch(numstars)
            {
            case 0:
                    printf("No Stars");
                    break;
            case 1:
                    printf("*");
                    break;
            case 2:
                    printf("**");
                    break;
            case 3:
                    printf("***");
                    break;
            case 4:
                    printf("****");
                    break;
            }
    }
    When I enter 2 to terminate the program, it jumps back up to Enter Bowler Name, and the char array wont print out. Been working on it for a while to no success.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should indent your code a little more consistently, and replace the use of goto with a loop construct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to terminate this program?
    By xianjianwuhen in forum C Programming
    Replies: 9
    Last Post: 07-17-2009, 09:38 AM
  2. terminate the program....
    By mancode1009 in forum C Programming
    Replies: 1
    Last Post: 07-11-2008, 03:47 AM
  3. Why doesnt this program terminate?
    By ojve in forum C Programming
    Replies: 4
    Last Post: 11-16-2007, 07:58 AM
  4. help: how do i terminate a program
    By Quartzzz in forum C++ Programming
    Replies: 8
    Last Post: 01-13-2007, 12:24 AM
  5. Why did the program terminate?
    By Mathsniper in forum C Programming
    Replies: 2
    Last Post: 06-10-2005, 04:05 AM