Thread: Trying to create a basic soccer points projection program and getting stuck..

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    8

    Trying to create a basic soccer points projection program and getting stuck..

    First off, I'm a beginner..
    I'm basically trying to create a program that will project a soccer teams points for the whole season.

    Here are the basics:
    Total games: 38
    Win: 3 pts
    Draw: 1 pt
    Loss: 0 pts

    My program compiles fine, but inevitably crashes right before the if statement.. Can you please help me figure out why? Thank you!

    Code:
    #include<stdio.h>
    
    
    int main(void)
    {
        float ppg, games_played, games_left, points_earned, pts_proj;
        char team_one;
    
    
        printf("What is the name of the first team?");
        scanf("%s", &team_one);
    
    
        printf("How many games have been played?");
        scanf("%f", &games_played);
    
    
        printf("How many points have been earned?");
        scanf("%f", &points_earned);
    
    
        ppg = points_earned / games_played;
    
    
        printf("The points per game is %.2f\n", ppg);
    
    
            if (games_played < 38) {
                games_left = 38 - games_played;
                pts_proj = games_left * ppg + points_earned;
                printf("The predicted finish of %s is %-3.2f\n\n", team_one, pts_proj);
            } else if (games_played == 38) {
                printf("The season has already ended for %s", team_one);
            }
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You cannot store the name of a team as a string in a single character, unless your team is named "".
    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

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    8
    Thank you. So therefor I should change 'char team one;' to something like 'int team one;'?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You probably want an array of char instead. Note that as an array will be converted to a pointer to its first element, you would thus change &team_one to team_one in the scanf call.
    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

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    No. You need to make team_one an array of char. 50 would be a good size for the array, to start with. Then remove the & symbol when passing it to scanf. For reading strings, you really should be using fgets. Using fgets will allow you to specify the size of the buffer that is receiving input, and prevent buffer overflows.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stuck on how to create a very simple TicTacToe game for 2 players
    By DecoratorFawn82 in forum C++ Programming
    Replies: 7
    Last Post: 03-31-2013, 01:55 PM
  2. Editable field in basic C game. I'm stuck.
    By stumped_student in forum C Programming
    Replies: 1
    Last Post: 04-26-2012, 09:29 PM
  3. Program to create a points schedule for a sport
    By hamma in forum C Programming
    Replies: 2
    Last Post: 11-13-2011, 11:43 PM
  4. some basic points about user-defined functions
    By jackson6612 in forum C++ Programming
    Replies: 5
    Last Post: 05-28-2011, 06:16 PM
  5. Help me with my basic program, nothing I create will run
    By Ravens'sWrath in forum C Programming
    Replies: 31
    Last Post: 05-13-2007, 02:35 AM