Thread: What is wrong program not calculating correctly.

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    1

    Unhappy What is wrong program not calculating correctly.

    I cannot figure what I have done wrong but it does not go through the calculations. It just displays 0.

    Code:
    
    
    Code:
    #include <iostream>
    #include <cstdlib>         
    #include <stdio.h>
    #include <cmath>
    #include<fstream>
    
    
    double Homework ( double homework); // fuction for homework weight
    double Midterm ( double midterm); // function for midterm weight
    double Final ( double final);     // function for final test weight
    //function to calculate final grade
    double CalcGrade (double homework, double midterm, double final, double totalScore, char grade);
    //function to print 
    int Print( double homework, double midterm, double final, double totalScore, char grade);
    
    
    int main()
    {
        //local delcarations
        double homework;
        double midterm;
        double final;
        double totalScore;
        char grade;
        
        //informs the user what this program does
        printf("This program will calculate the final grade after homework\n");
        printf("midterm, and final test grades are inputed in.\n");
        system("cls"); //clears screen
        
        //function calls
        Homework(homework);
        Midterm(midterm);
        Final(final);
        CalcGrade ( homework,  midterm,  final,totalScore, grade);
        Print(homework, midterm, final, totalScore, grade);
    
    
    
    
    system("pause"); //holds screen for dev C++
    
    
    return 0;
    }  //end of main
    
    
    /////////////////////////////HOMEWORK////////////
    double Homework (double homework)
    {
    system("cls");
    
    
    //function delcarations
    int x ;
    int i ;
    double sum;
    double homeworkMaxPts ;
    // homework ;
    
    
    //prompts users for input, and how to terminate
    printf("Enter the homework assignment grades: \nEnter < 999 > to stop.\n");
    
    
    do //loop to collect input for homework grades
    {
          scanf("%d", &x);
          if ( x <900) //termination of loop
          sum += x; // calculated total amount of points
    }while ( x < 900);
    
    
    //prompts user to input maxmum score for each assignment
    printf("Enter the max points for each assignemnt: \n");
    printf("Enter < 999 > to stop\n ");
    
    
    //loop to collect imput for maximun points possible for homework
    do
    {
                scanf("%d", &i);
                if ( i < 900) //loop termination
                homeworkMaxPts += i; // calculates total amount of points
    }while ( i < 900);
    
    
    //calculates the total weight of homework
    homework =(sum*50/homeworkMaxPts); 
    
    
    
    
    system("pause"); 
     return homework; // returns weighted homeword
    
    
    }      //end of homework
    
    
    // Function to determine weight of midterm tests
    double Midterm ( double midterm)
    {
    
    
    system("cls"); // clear screen
    
    
    //declarations
    int a=0;
    int i=1;
    double sum;
    int x =1;
    double midMaxPts = 0;
    int b=0;
     //midterm =0;
    
    
    // prompts user to input midterm grades
    printf("Enter the midterm grades: \n");
    
    
    //loop to calculate total midterm points
    while(i<=3)
    {
               scanf("%d", &a);
               sum +=a;
               i++;
    }//end of while
    //prompts user to imput maximun amount of points possible
    printf("\nEnter the max points for midterm grades: \n");
    
    
    //loop to calculate total possible points
    while ( x <=3)
    {
          scanf("%d", &b);
          midMaxPts += b;
          x++;
    }//end of while
    
    
    system("cls"); //clear screen
    midterm = (sum*20/midMaxPts); //calculate midterm weights
    
    
    
    
    
    
    
    
        
     system("pause");
     return midterm; //return midterm
    
    
    } //end of midterm
    
    
    //Final test function
    double Final ( double final)
    {
    
    
    system("cls"); //clear screen
    //declarations
    int final1, final2; 
    int finalSum = 0;
    int points1, points2;
    double finalMaxpts = 0;
    final = 0;
    //prompts user to input the two final grades
    printf("Enter the final grades: \n" );
    scanf("%d %d", &final1, &final2);
    //prompts user to input the maximum points for finals
    printf("Enter the max points possible for each grade: \n");
    scanf("%d %d", &points1, &points2);
    
    
    finalSum = final1 + final2; //calculates total points made
    
    
    finalMaxpts = points1 + points2; //calculates total points possible
    
    
    final = (finalSum*30/finalMaxpts); //calculates weight of final
    
    
     system("cls");
     system("pause");
     return final; // returns final weight
    } //end final function
    
    
    // grade calculation function
    double CalcGrade (double homework, double midterm, double final, double totalScore, char grade)
    {
        //calculate final overall grade
        totalScore = homework + midterm + final;
        
        //to calculate the letter grade
        if (totalScore >= 90)
           grade = 'A';
        else if ( totalScore >= 80)
             grade = 'B';
        else if ( totalScore >= 70)
             grade = 'C';
        else if ( totalScore >= 60)
             grade = 'D';
        else 
             grade = 'F';
             
    system("pause");
    return grade; //return grade
    } 
    
    
      // print function                                                       
    int Print( double homework, double midterm, double final, double totalScore, char grade)
    {
    // prints out all grades
    printf("\n\nFinal exam score was %2.2f\n", final);
    printf("Midterm exam score was %2.2f\n", midterm);
    printf("Homework assignments score were %2.2f\n", final);
    printf("------------------------------------------\n\n");
    printf("Total score is %2.2f\n", totalScore);
    printf("Grade for the class is %c\n\n\n", grade);
    
    
    system("pause");
    
    
    return 0;
    }        //

  2. #2
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    You should probably decide first if you want to write C++ or C code, pick one then go with that. Then post in the correct forum and maybe someone will help you out, if it isn't homework. maybe.

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    In the Homework function, at least, you've prototyped it to take a double as an argument and return a double. When you call the function in main, you're passing in an uninitialized copy and not using the returned value.

    Code:
    double Homework(double);
    ...
    double homework;
    ...
    Homework(homework);
    the other value returning functions are similar. that is, you're passing in uninitialized copies and you don't do anything with the value they return.

    When you call Print, changes that were made to your locals (homework, midterm, final, etc) were left behind in the functions because you passed them by value; the value returned by the functions was probably suspect anyway.

    I'm not sure why you're passing in the variables. Unless it's required, you'd probably be better off making the functions take no arguments and use the locals to get the returns. Or make the functions return no value and pass in the locals by reference.

    Code:
    int x;
    double sum;
    
    do //loop to collect input for homework grades
    {
          scanf("%d", &x);
          if ( x <900) //termination of loop
          sum += x; // calculated total amount of points
    }while ( x < 900);
    have you disabled compiler warnings??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program not running correctly
    By computermajor12 in forum C Programming
    Replies: 3
    Last Post: 11-14-2012, 12:08 AM
  2. Why's my program not compiling correctly?
    By mgracecar in forum C Programming
    Replies: 1
    Last Post: 02-15-2012, 01:45 AM
  3. have i written this program correctly ?
    By broli86 in forum C Programming
    Replies: 6
    Last Post: 07-20-2008, 03:20 PM
  4. can't get this program to run correctly
    By Amyaayaa in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2008, 04:16 PM
  5. Program Not Functioning Correctly
    By TWIXMIX in forum C++ Programming
    Replies: 4
    Last Post: 09-04-2005, 06:04 PM

Tags for this Thread