Wheres the glitch?

This is a discussion on Wheres the glitch? within the C Programming forums, part of the General Programming Boards category; Can anyone tell me whats wrong with this program. when i try to run it. After entering the first grade, ...

  1. #1
    Unregistered
    Guest

    Angry Wheres the glitch?

    Can anyone tell me whats wrong with this program. when i try to run it. After entering the first grade, it gives the error message of The instruction at " " referenced memory at " ". The memory could not be written. What exactly does this mean, and what did I do wrong?
    Description: Displays the grade average from four tests

    #include <stdio.h>
    int main()

    {
    unsigned int grade1=0,grade2=0,grade3=0,grade4=0,total=0; /*declare grade1, grade2, grade3,grade4 as a int variable*/
    float average;

    total=grade1 + grade2 + grade3 + grade4;
    average=total/4.0; /*divide the total by 4.0*/
    printf("Enter student1 first grade:\n");
    scanf("%f",grade1);
    printf("Enter student1 second grade:\n");
    scanf("%f",grade2);
    printf("Enter student1 third grade:\n");
    scanf("%f",grade3);
    printf("Enter student1 fourth grade:\n");
    scanf("%f",grade4);
    printf ("\n The grade average of student1 is %f",average);


    return 0;

    }

    SOS!

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    You're using scanf wrong...

    It should be

    scanf("%f",&grade1);

    not

    scanf("%f",grade1);


    Also, change your grades to floats or doubles... Also, since you calculate total and average before getting any input, they'll always come out 0.

  3. #3
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284

    Post Re: Wheres the glitch?

    1)you initialized grade1,grade2,.... to 0 then took their average , which will be zero.

    grade1 is an unsigned integer so no %f use %u ,the second parameter to scanf will be the address of grade1 so scanf("%u",&grade1); , similarly for other grades .

    you can have a look at this page whenever you are in doubt about the functions in the c standard library
    http://www.acm.uiuc.edu/webmonkeys/book/c_guide/

    Originally posted by Unregistered
    Can anyone tell me whats wrong with this program. when i try to run it. After entering the first grade, it gives the error message of The instruction at " " referenced memory at " ". The memory could not be written. What exactly does this mean, and what did I do wrong?
    Description: Displays the grade average from four tests

    #include <stdio.h>
    int main()

    {
    unsigned int grade1=0,grade2=0,grade3=0,grade4=0,total=0; /*declare grade1, grade2, grade3,grade4 as a int variable*/
    float average;

    total=grade1 + grade2 + grade3 + grade4;
    average=total/4.0; /*divide the total by 4.0*/
    printf("Enter student1 first grade:\n");
    scanf("%f",grade1);
    printf("Enter student1 second grade:\n");
    scanf("%f",grade2);
    printf("Enter student1 third grade:\n");
    scanf("%f",grade3);
    printf("Enter student1 fourth grade:\n");
    scanf("%f",grade4);
    printf ("\n The grade average of student1 is %f",average);


    return 0;

    }

    SOS!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie mind wrecking glitch
    By mas0 in forum C Programming
    Replies: 8
    Last Post: 10-18-2006, 08:11 PM
  2. Does goto have a glitch or...?
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 09:40 AM
  3. Replies: 8
    Last Post: 03-27-2004, 10:50 AM
  4. Major glitch losing source files with MSVC++ and WinXP
    By JasonD in forum C++ Programming
    Replies: 10
    Last Post: 08-14-2003, 12:15 PM
  5. Okay its running with a serious glitch
    By Blizzarddog in forum C++ Programming
    Replies: 2
    Last Post: 11-13-2002, 01:33 PM

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