Thread: Help with this small C program

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    2

    Help with this small C program

    Hi, I am new to C, but I want to get ahead of the game before I go to school and out into the workplace. I tried coming up with a general program that calculates an average grade. I am a beginner at this and any help would be greatly appriceated!

    Code:
    #include
    int main ()
    
    {
    int hw1, hw2, hw3, hwtotal;
    int proj1, proj2, proj3, proj4, projtotal;
    int exam1, exam2, exam3, examtotal;
    float grade;
    
    /* Get user input scores */
    
    printf("Enter the homework 1 score: \n");
    scanf("%d, &hw1);
    printf("Enter the homework 2 score: \n");
    scanf("%d, &hw2);
    printf("Enter the homework 3 score: \n");
    scanf("%d, &hw3);
    printf("Enter the project 1 score: \n");
    scanf("%d, &proj1);
    printf("Enter the project 2 score: \n");
    scanf("%d, &proj2);
    printf("Enter the project 3 score: \n");
    scanf("%d, &proj3);
    printf("Enter the project 4 score: \n");
    scanf("%d, &proj4);
    printf("Enter the exam 1 score: \n");
    scanf("%d, &exam1);
    printf("Enter the exam 2 score: \n");
    scanf("%d, &exam2);
    printf("Enter the exam 3 score: \n");
    scanf("%d, &exam3);
    
    /* Calculate the grades */
    
    hwtotal=hw1+hw2+hw3;
    projtotal=proj1+proj2+proj3+proj4;
    examtotal=exam1+exam2+exam3;
    grade=++]*100;
    
    /* Shows the user their grade */
    
    if (grade=90)
    {
    printf("With an average of %f, your course grade is an A.\n", grade);
    }
    
    return 0;
    }
    proj1.c: In function `main':
    proj1.c:13: error: missing terminating " character
    proj1.c:14: error: parse error before ';' token
    proj1.c:15: error: missing terminating " character
    proj1.c:17: error: missing terminating " character
    proj1.c:18: error: parse error before ';' token
    proj1.c:19: error: missing terminating " character
    proj1.c:21: error: missing terminating " character
    proj1.c:22: error: parse error before ';' token
    proj1.c:23: error: missing terminating " character
    proj1.c:25: error: missing terminating " character
    proj1.c:26: error: parse error before ';' token
    proj1.c:27: error: missing terminating " character
    proj1.c:29: error: missing terminating " character
    proj1.c:30: error: parse error before ';' token
    proj1.c:31: error: missing terminating " character
    proj1.c:38: error: parse error before '[' token
    proj1.c:5: warning: unused variable `hw1'
    proj1.c:5: warning: unused variable `hw2'
    proj1.c:5: warning: unused variable `hw3'
    proj1.c:5: warning: unused variable `hwtotal'
    proj1.c: At top level:

    Are my logistics wrong? Please help me out with the various fixes.
    Last edited by Havax; 10-30-2005 at 07:07 PM.

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Your #include is missing the file:
    Code:
    #include <stdio.h>
    All your scanf functions are missing the second quote:
    Code:
    scanf("%d", &hw1);
    What do you mean here? It doesn't make sense:
    Code:
    grade=++]*100;
    In C, you use = for assignment, and == for comparison:
    Code:
    if (grade=90)
    Last edited by cwr; 10-30-2005 at 07:14 PM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    2
    I ended up fixing the errors:
    Code:
    #include <stdio.h>
    int main ()
    
    {
       int hw1, hw2, hw3, hwtotal;
       int proj1, proj2, proj3, proj4, projtotal;
       int exam1, exam2, exam3, examtotal;
       float grade;
    
       /* Get user input scores */
    
       printf("Enter the homework 1 score: \n");
       scanf("%d", &hw1);
       printf("Enter the homework 2 score: \n");
       scanf("%d", &hw2);
       printf("Enter the homework 3 score: \n");
       scanf("%d", &hw3);
       printf("Enter the project 1 score: \n");
       scanf("%d", &proj1);
       printf("Enter the project 2 score: \n");
       scanf("%d", &proj2);
       printf("Enter the project 3 score: \n");
       scanf("%d", &proj3);
       printf("Enter the project 4 score: \n");
       scanf("%d", &proj4);
       printf("Enter the exam 1 score: \n");
       scanf("%d", &exam1);
       printf("Enter the exam 2 score: \n");
       scanf("%d", &exam2);
       printf("Enter the exam 3 score: \n");
       scanf("%d", &exam3);
    
       /* Calculate the grades */
    
       hwtotal=hw1+hw2+hw3;
       projtotal=proj1+proj2+proj3+proj4;
       examtotal=exam1+exam2+exam3;
       grade=(hwtotal/300)*.12+(projtotal/400)*.28+(examtotal/300)*.6*100;
    
    /* Shows the user their grade */
    
          if (grade<=60)
          {
             printf("With an average of %f, your course grade is an F.\n", grade);
          }
          else if (grade<70)
            {
               printf("With an average of %f, your course grade is a D.\n", grade);
            }
          else if (grade<80)
          {
             printf("With an average of %f, your course grade is a C.\n", grade);
          }
          else if (grade<90)
          {
             printf("With an average of %f, your course grade is a B.\n", grade);
          }
          else if (grade>=90)
          {
             printf("With an average of %f, your course grade is an A.\n", grade);
          }
    
    return 0;
    }
    But when I execute the program no matter what grades I enter, it always comes out 0.00000 and F. Why is this? What can I do to fix that?

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Code:
    grade=(hwtotal/300)*.12+(projtotal/400)*.28+(examtotal/300)*.6*100;
    Because hwtotal, projtotal and examtotal are integers, and because 300, 400, and 300 are integers, the compiler is treating the expression as integer. Change them to 300.0, 400.0, 300.0 so it knows to use floating point.

    Also, watch out for operator precedence. The *100 will only affect the (examtotal/300)*.6, but you want to to affect the whole expression.
    Last edited by cwr; 10-30-2005 at 07:24 PM.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    8
    Havax, do you live in MD? I have a similar assignment in school and that'd be interesting if we had the same class..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help with small program
    By cjohnman in forum C Programming
    Replies: 11
    Last Post: 04-14-2008, 09:59 AM
  2. Program to reverse a number. small error with it..
    By comproghelp in forum C Programming
    Replies: 8
    Last Post: 11-22-2004, 10:52 AM
  3. Help writing small program
    By guitarhead2000 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2004, 12:42 PM
  4. A little Help with a small program
    By whtpirate in forum C Programming
    Replies: 7
    Last Post: 06-05-2003, 06:15 PM
  5. Very small program...Whats wrong???
    By SmokingMonkey in forum C++ Programming
    Replies: 4
    Last Post: 05-30-2003, 09:09 PM