Thread: whats wrong with this code

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    7

    whats wrong with this code

    i am trying to get this program to ask the user to answer whether or not the class is pass fail or for a grade. prior to asking what the weight of the grade is


    #include <stdio.h>
    char grade (int exam1, int exam2, float exam1_weight);
    char type (int P, int G);
    main ()
    {
    int ans, mid_term, final, P, G, pass, letter;
    float weight;
    char letter_grade,c_type;
    do {
    printf("\n\nCompute another grade (1 = Yes, 0 =No)? ");
    scanf("%d", &ans);
    printf("\n Is this class Pass or Fail or For a Grade?");
    scanf("%c\n", &pass,&letter);
    if (ans == 1) {
    printf( "\nEnter mid_term, final, weight: ");
    scanf("%d%d%f", &mid_term, &final, &weight );
    letter_grade = grade(mid_term, final, weight);
    printf("Grade is: %c\n", letter_grade);
    }
    if (pass == P, letter == G) {
    c_type = type(P, G);
    printf("Class Type is: %c\n", c_type);
    }
    }
    while (ans == 1);

    }

    char grade (int exam1, int exam2, float exam1_weight)
    {
    float average;

    average = exam1_weight * exam1 + (1.0 - exam1_weight) * exam2;
    if (average > 7.0)
    return 'P';
    else
    return 'F';
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > scanf("%c\n", &pass,&letter);

    Why are you scanning for ONE value (%c) and passing two arguments to scan into?

    Furthermore, why are you checking the value of "ans" after you do the second scanf? Shouldn't you actually check to see if they want to do a test and then if they do, ask for the second prompt?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered
    Guest
    1/ scanf("%c\n", &pass,&letter);

    not only are you scanning for one value and passing two arguments, but the arguments are of type int and you are scanning for a char.

    2/ if (pass == P, letter == G)

    Haven't seen this before should it be?

    if (pass == P && letter == G)

    3/ char type (int P, int G);

    Where is the function definition and what does it do?

    4/ average = exam1_weight * exam1 + (1.0 - exam1_weight) * exam2;

    You might have to cast int to float, ie

    average = exam1_weight * (float)exam1 + (1.0 -
    exam1_weight) * (float)exam2;

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    using code tags would be nice

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM