CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2015
    Posts
    13

    [RESOLVED] I am trying to understand why I am getting wrong output for my program.

    For my average output I am getting 008914D8 and for my letter grade output I am getting 0089121C. Any help will be great. Thank You.





    Code:
    // This program will input an undetermined number of student names
    // and a number of grades for each student. The number of grades is 
    // given by the user. The grades are stored in an array.
    // Two functions are called for each student. 
    // One function will give the numeric average of their grades.
    // The other function will give a letter grade to that average. 
    // Grades are assigned on a 10 point spread.  
    // 90-100 A   80- 89 B  70-79 C   60-69 D   Below 60 F
    
    // Jose Velazquez
    
    
    #include <iostream>
    
    
    using namespace std;
    
    
    const  int MAXGRADE = 25;              // maximum number of grades per student
    const  int MAXCHAR = 30;              // maximum characters used in a name
    
    
    typedef char StringType30[MAXCHAR + 1];// character array data type used for names 
                                           // having 30 characters or less.
    typedef  float GradeType[MAXGRADE];    // one dimensional integer array data type
    
    float findGradeAvg(GradeType, int);    // finds grade average by taking array of 
                                           // grades and number of grades as parameters
    
    char  findLetterGrade(float);          // finds letter grade from average given 
                                           // to it as a parameter
    
    
    int main()
    
    {
    	StringType30 firstname, lastname;  // two arrays of characters defined
        int numOfGrades;                   // holds the number of grades
    	GradeType  grades;                 // grades is defined as a one dimensional array 
    	float average;                     // holds the average of a student's grade
        char moreinput;                    // determines if there is more input
    
        // Input the number of grades for each student
    
    	cout << "Please input the number of grades each student will receive." << endl
    		 << "This number must be a number between 1 and " << MAXGRADE << " inclusive" << endl;
    
    	cin >> numOfGrades;
    
    	while (numOfGrades > MAXGRADE || numOfGrades < 1)
    	{
    		cout << "Please input the number of grades for each student." << endl
    		     << "This number must be a number between 1 and " << MAXGRADE << " inclusive" << endl;
    
    		cin >> numOfGrades;
    
    	}
    
        // Input names and grades for each student
    
    	cout << "Please input a y if you want to input more students"
    		 << " any other character will stop the input" << endl;
    	cin >> moreinput;
    
    	while ( moreinput == 'y' || moreinput == 'Y')
    	
    	{
            cout << "Please input the first name of the student" << endl;
    		cin >> firstname;
    		cout << endl << "Please input the last name of the student" << endl;
    		cin >> lastname;
    
    		for (int count = 0; count < numOfGrades; count++)
    
    		{
    
    			cout << endl << "Please input a grade" << endl;
    		
    			cin >> grades[count];
    				// Fill in the input statement to place grade in the array
    
    		}
    
            cout << firstname << ' ' << lastname << " has an average of "; 
    	
    		cout << findGradeAvg; // Fill in code to get and print average of student to screen
    		cout << " which gives the letter grade of ";
    		cout << findLetterGrade;// Fill in call to get and print letter grade of student to screen
    	    
    		cout << endl << endl << endl;
    		cout << "Please input a y if you want to input more students"
    		     << " any other character will stop the input" << endl;
    	    cin >> moreinput;
    
    	}
    
    	return 0;
    }
    
    //***********************************************************************
    //                              findGradeAvg
    //
    //  task:           This function finds the average of the 
    //                  numbers stored in an array.
    //            
    //  data in:        an array of integer numbers
    //  data returned:  the average of all numbers in the array
    //
    //***********************************************************************
    
    float findGradeAvg(GradeType array, int numgrades)
    
    {
    
    	float sum = 0;
    	for (int i = 0; i < numgrades; i++)
    	{
    		sum = sum + array[i];
    	}
    	return (sum / numgrades); // Fill in the code for this function
    
    
    }
    
    //***********************************************************************
    //                              findLetterGrade
    //
    //  task:           This function finds the letter grade for the number
    //				    passed to it by the calling function
    //            
    //  data in:        a floating point number
    //  data returned:  the grade (based on a 10 point spread) of the number 
    //                  passed to the function
    //
    //***********************************************************************
    
    char  findLetterGrade(float numgrade)
    
    {
    	
    	if (numgrade >= 90 && numgrade <= 100)
    	{
    		return 'A';
    	}
    	if (numgrade >= 80 && numgrade <= 89)
    	{
    		return 'B';
    	}
    	if (numgrade >= 70 && numgrade <= 79)// Fill in the code for this function
    	{
    		return 'C';
    	}
    	if (numgrade >= 60 && numgrade <= 69)
    	{
    		return 'D';
    	}
    	else
    	{
    		return 'F';
    	}
    
    }

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: I am trying to understand why I am getting wrong output for my program.

    Code:
    cout << findGradeAvg;
    You are not calling the function findGradeAvg with the required parameters.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured