Beginning C programmer...

This is a discussion on Beginning C programmer... within the C Programming forums, part of the General Programming Boards category; I'm new, so forgive me if I post something wrong... In the following code: /* * This program reads two ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    3

    Cool Beginning C programmer...

    I'm new, so forgive me if I post something wrong...

    In the following code:

    /*
    * This program reads two SAT verbal scores (between 0
    * and 800) for a student and prints the larger
    */

    #include <stdio.h>

    void instructions(void);
    int ReadScore(void);
    int OutOfRange(int score);
    int max(int x, int y);

    main()
    {

    int
    score1, /* SAT verbal score #1 */
    score2; /* SAT verbal score #2 */

    /* call function instructions to print instructions */
    /* obtain a value for score1 by calling ReadScore */
    /* if score1 is out of range
    print an error message
    else
    obtain a value for score2 by calling ReadScore
    if score2 is out of range
    print an error message
    else
    using function max print larger score */


    instructions();
    score1 = ReadScore();
    if (OutOfRange(score1))
    {
    printf("\nScore1 is out of range\n");
    }
    else
    {
    score2 = ReadScore();

    if (OutOfRange(score2))

    printf("\nScore2 is out of range\n");

    else
    printf("\nYour maximum score is %d\n\n", max(score1, score2));
    }
    }


    /*
    * Function to print instructions on what the program does
    */

    void instructions(void)
    {
    printf("This program interactively reads two SAT\n");
    printf("verbal scores and prints the larger\n\n");
    }

    /*
    * Function to read a score interactively
    */

    int ReadScore(void)
    {
    int score;

    printf("Entering stub ReadScore\n");
    return score;

    }

    /*
    * Function to return TRUE (1) if the SAT score is out of
    * range. Range: 0-800
    */

    int OutOfRange(int score)
    {
    printf("Entering stub OutOfRange\n");
    return 0;
    }

    /*
    * Function to return maximum of two integer parameters
    */

    int max(int x, int y)
    {

    int stubReturn;

    printf("Entering stub max\n");

    if (x < y)
    stubReturn = y;
    else
    stubReturn = x;
    return (x);
    }

    error message is:

    C:\My Documents\C Files\Lab031.c(76) : warning C4700: local variable 'score' used without having been initialized
    Linking...

    Lab031.exe - 0 error(s), 1 warning(s)

    output looks like:

    This program interactively reads two SAT
    verbal scores and prints the larger

    Entering stub ReadScore
    Entering stub OutOfRange
    Entering stub ReadScore
    Entering stub OutOfRange
    Entering stub max

    Your maximum score is -858993460

    Press any key to continue

    I think I'm doing something wrong in:
    int max(int x, int y)
    {

    int stubReturn;

    printf("Entering stub max\n");

    if (x < y)
    stubReturn = y;
    else
    stubReturn = x;
    return (x);
    }

    Maybe I'm returning the wrong variable...
    Anyway, any help or ideas on what I'm doing wrong?

    Thanks...
    - Ed

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    > printf("Entering stub ReadScore\n");
    See this bit?

    It means you need to add some code here to actually read some input from the user. Just saying that you've done it isn't enough.

    > printf("Entering stub OutOfRange\n");
    Same goes for this - you need to fill in the code to make the code do what the comment says.
    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.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    k first the warning...

    int ReadScore(void)
    {
    int score;

    printf("Entering stub ReadScore\n");
    return score;

    }

    you declare an int but dont set that int to anything so its value is random..... You then return that random value.You make no attempt to get input from the user which this function is supposed to do i guess.

    int max(int x, int y)
    {

    int stubReturn;

    printf("Entering stub max\n");

    if (x < y)
    stubReturn = y;
    else
    stubReturn = x;
    return (x);
    }

    here you need to return stubReturn and not x.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    3
    Originally posted by Salem
    > printf("Entering stub ReadScore\n");
    See this bit?

    It means you need to add some code here to actually read some input from the user. Just saying that you've done it isn't enough.

    > printf("Entering stub OutOfRange\n");
    Same goes for this - you need to fill in the code to make the code do what the comment says.
    Thanks for the reply, I'll try what you said...

    - Ed

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    3
    Originally posted by Stoned_Coder
    k first the warning...

    int ReadScore(void)
    {
    int score;

    printf("Entering stub ReadScore\n");
    return score;

    }

    you declare an int but dont set that int to anything so its value is random..... You then return that random value.You make no attempt to get input from the user which this function is supposed to do i guess.

    int max(int x, int y)
    {

    int stubReturn;

    printf("Entering stub max\n");

    if (x < y)
    stubReturn = y;
    else
    stubReturn = x;
    return (x);
    }

    here you need to return stubReturn and not x.
    Thanks for the tips...
    Maybe I should have taken an advanced math course before I enrolled in my C programming course...

    - Ed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginning C Programmer - Need Help Compiling Program
    By fisguts in forum C Programming
    Replies: 2
    Last Post: 07-30-2008, 02:14 PM
  2. QT or others for the beginning linux programmer
    By FillYourBrain in forum Linux Programming
    Replies: 3
    Last Post: 09-12-2003, 01:17 PM
  3. Beginning programmer
    By L_burna225 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2003, 04:55 PM
  4. beginning programmer needs help with a structures program!
    By psychadelic in forum C Programming
    Replies: 1
    Last Post: 12-03-2002, 09:27 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM

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