I'm stuck and I dont know how to go any farther

This is a discussion on I'm stuck and I dont know how to go any farther within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I just stated programming a week ago and am having some problems with this program i am making. I wrote ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    11

    Question I'm stuck and I dont know how to go any farther

    I just stated programming a week ago and am having some problems with this program i am making. I wrote it and then compiled it and executed it and that all worked, but then when i tried to run it it wasnt what i wanted. I want it to ask some questions then calculate some answeres. Here is my program can you tell me how to fix it?

    Thanks

    Code:
    #include <stdio.h>
    int main()
    {
    float hundredthirteen = 113.0;
    float decimal = 0.096;
    float five= 5.0;
    float scoreone, rating, slope;
    float scoretwo, ratingtwo, slopetwo;
    float scorethree, ratingthree, slopethree;
    float scorefour, ratingfour, slopefour;
    float scorefive, ratingfive, slopefive;
    float partone, parttwo, partthree, handicapone;
    float partonetwo, parttwotwo, partthreetwo, handicaptwo;
    float partonethree, parttwothree, partthreethree, handicapthree;
    float partonefour, parttwofour, partthreefour, handicapfour;
    float partonefive, parttwofive, partthreefive, handicapfive;
    float partfour, handicapindex;
    
    printf("You will enter your last five golf scores and this will find your handicap index");
    printf("Score 1: What did you shoot? ");
    scanf("%*scoreone%.0f");
    printf("What is the course rating? ");
    scanf("%*rating%.0f");
    printf("What is the course slope? ");
    scanf("%*slope%.0f");
    partone = scoreone - rating;
    parttwo = partone * hundredthirteen;
    partthree = parttwo / slope;
    handicapone = partthree * decimal;
    
    printf("\nScore 2: What did you shoot? ");
    scanf(" %.0f", &scoretwo);
    printf("\nWhat is the course rating? ");
    scanf(" %.1f", &ratingtwo);
    printf("\nWhat is the course slope? ");
    scanf(" %.0f", &slopetwo);
    partonetwo = scoretwo - ratingtwo;
    parttwotwo = partonetwo * hundredthirteen;
    partthreetwo = parttwotwo / slopetwo;
    handicaptwo = partthreetwo * decimal;
    
    printf("\nScore 3: What did you shoot? ");
    scanf(" %.0f", &scorethree);
    printf("\nWhat is the course rating? ");
    scanf(" %.1f", &ratingthree);
    printf("\nWhat is the course slope? ");
    scanf(" %.0f", &slopethree);
    partonethree = scorethree - ratingthree;
    parttwothree = partonethree * hundredthirteen;
    partthreethree = parttwothree / slopethree;
    handicapthree = partthreethree * decimal;
    
    printf("\nScore 4: What did you shoot? ");
    scanf(" %.0f", &scorefour);
    printf("\nWhat is the course rating? ");
    scanf(" %.1f", &ratingfour);
    printf("\nWhat is the course slope? ");
    scanf(" %.0f", &slopefour);
    partonefour = scorefour - ratingfour;
    parttwofour = partonefour * hundredthirteen;
    partthreefour = parttwofour / slopefour;
    handicapfour = partthreefour * decimal;
    
    printf("\nScore 5: What did you shoot? ");
    scanf(" %.0f", &scorefive);
    printf("\nWhat is the course rating? ");
    scanf(" %.1f", &ratingfive);
    printf("\nWhat is the course slope? ");
    scanf(" %.0f", &slopefive);
    partonefive = scorefive - ratingfive;
    parttwofive = partonefive * hundredthirteen;
    partthreefive = parttwofive / slope;
    handicapfive = partthreefive * decimal;
    
    partfour = handicapone + handicaptwo + handicapthree + handicapfour + handicapfive;
    handicapindex = partfour / five;
    printf(" Your handicap index is %.2f", handicapindex);
    return 0;
    }
    Last edited by Bobit; 11-07-2003 at 11:04 PM.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    After every call to scanf(), flush the input buffer. Search the board for flushing the input buffer, you'll find many threads and you'll also find out why not to use fflush(stdin).

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    11
    I tried to do several things but sill when i run the program it prints score 1: What did you shoot. Then i type in the score and press enter and it skips the second question its suppose to ask and goes to the third and i type in a number and it repeats the 1-3 questions and lets me type in a number for the 3rd question again. can ya tell me how to fix it.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    > scanf("%*scoreone%.0f");
    This isn't even close
    The ones which follow are better

    > scanf(" %.0f", &scoretwo);
    A simple
    scanf("%f", &scoretwo);
    should suffice
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

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