Need help with Grade calculator code.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • djselect24
    New Member
    • Jun 2015
    • 5

    Need help with Grade calculator code.

    I am a beginner and needing help with my coding for my grade calculator. I know that most examples show grade averages for a set number of grades. The thing that I am trying to learn is how would I write it if the number of grades was different for each student. I am having trouble being able to figure out how to put in a function where the user can stop putting in grades for the student whenever they want. I also am trying to put it to where the user enters A,B,C,D,F for the grade and have my program automatically convert it to A=4,B=3, C=2, D=1, F=0, then average up all the grades entered. I am trying to learn this stuff not just looking for a handout, I am just stumped as to where to find help.. THANKS!

    Code:
    #include <iostream>
    #include <string>
     
            using namespace std;
    
    int main()
    {
           
            //Declaring variables
            char StudentsName [100]; 
            float ExamValue, Sum, Avg,TotalGrades; //Total grades needs to equal # of grades submitted
            
            int A = 4;//Need to find a way for the user to input A,B,C,D,F and it turn it to the corr. number//
            int B = 3;
            int C = 2;
            int D = 1;
            int F = 0;
            string input = ""; 
     
            //declaring students name
            cout << "Enter students name:"; 
            cin >> StudentsName;
            cout << endl;
     
            //declaring grade
            cout << "Enter Grade must be A,B,C,D, or F:";
            cin >> ExamValue;
             
     
            //STILL NEED TO WORK ON THIS EQUATION
            Avg = Sum / TotalGrades;
            
            cout << "Final Grade: " << Avg << endl;
     
            //Calculating grade
            cout << "Grade: ";
            if (Avg == 100){
            cout << "A+" << endl;
            }else if (Avg < 4 && Avg >= 3){
            cout << "A" << endl;
            }else if (Avg < 3 && Avg >= 2){
            cout << "B" << endl;
            }else if (Avg < 2 && Avg >= 1){
            cout << "C" << endl;
            }else if (Avg < 1 && Avg >= 0){
            cout << "D" << endl;
            }else if (Avg < 0){
            cout << "F" << endl;
            }
     
            return 0;
            }
  • inspectelement
    New Member
    • Nov 2014
    • 21

    #2


    This is from engrade, and is slightly helpful. If you are a teacher, or can someway make an account on engrade, you can use this grading system.This is what all our teachers use, and uses averaging to calculate grades. Please tell me why you need 1 for A, 2 for B. Is it for a specific reason? Because there are way easier ways to calculate grades.

    Comment

    • inspectelement
      New Member
      • Nov 2014
      • 21

      #3
      I reread what your post and realized you do not want averaging. What do you mean by "stop" putting in grades for a student? And still, why do you need 1a b2 etc?

      Comment

      • djselect24
        New Member
        • Jun 2015
        • 5

        #4
        by the stop putting in grades, I meant say you have student A with 5 grades, student B with 7 grades, and student C with 10 grades. How could you tell the program to keep asking to input grades in for a different range of grade scores. Most examples have a set number of 3 grade inputs so it makes it easy. But in my example it is unknown how many grades each student has. Also the grade needs to be broken down in College Scoring ie. A=4.0, B=3.0, C=2.0, D=1.0, F=0.0 This is the assignment for my class and how the instructor wants the grading.

        Comment

        • kiseitai2
          New Member
          • Jul 2007
          • 93

          #5
          As per forum rules I can't give you a coded solution, but I will attempt to help you see one possible solution. First, how much of C++ have you learned? Have you tried asking the user how many grades s/he wishes to input? If you ask the user for the number of input events prior to computing the average you will have a value for TotalGrades. Furthermore, you can use the value to know how many times you have to ask the user for grades. This brings us to, have you covered any types of loops in class? Let's deal with those issues before we move on.

          Comment

          • djselect24
            New Member
            • Jun 2015
            • 5

            #6
            I am just in my first class of C++, it is a long story but basically the curriculum that the teacher has is not very good. He asks us to code these projects but the reading material that he gives us is no where near what the projects asks. We were told that we didn't need textbooks for this class so I have been researching a lot online. The project that he asks us to code does not have a set number of grades, he told us to code it as if we did not know how many grades the student has, therefore it makes it a little difficult because if it was just a set number then I would be able to figure that out a little bit easier. I have used loops briefly but I am barely in my 4th week of class. Sorry if I am making this confusing, and I understand I don't want anybody to code it for me I just wanted to be steered in the right direction so i can read the lessons on my own.

            Comment

            • kiseitai2
              New Member
              • Jul 2007
              • 93

              #7
              Ok, I have an idea of what tools you have to solve the problem. Your instructor most likely wanted you to use a loop to solve this problem. Why? A loop will allow you to repeat a set of instructions (example, cout << "What color is the next circle?: ";) a set number of times or even indefinitely. For this, I will briefly explain what a couple of loops do, so you can make a decision on which to use!
              The while loop will execute a block of code as long as the test expression yields true.
              Code:
              char foo = 'a';
              while(a != 'x')//Loop will stop when user types x!
              {
                 cout << "Type a letter!" << std::endl;
                 cin >> foo;
                 cout << std::endl;
              }
              The for loop will repeat a block of code a set number of times:

              Code:
              for(int i = 0; i < 5; i++)//Loop will stop when i = 5!
              {
                 cout << "Loop has ran for: " << i << " times!" << std::endl;
              }
              Notice that the contents of a for loop is divided into a variable for holding results, a Boolean or test expression, and an instruction to execute AFTER the block of code has executed. Furthermore, the playful eye will notice that you can make the for loop indefinite by making the test expression always true (either because it is impossible to reach or always true). For example:

              Code:
              for(int i = 0; i < 5; i++)//Loop will stop when i = 5!
              {
                 cout << "Loop has ran for: " << i << " times!" << std::endl;
                 i = 0;//This will prevent the loop from reaching 5 because it will always be set to 0!
              }
              Code:
              for(int i = 0; i > 0; i++)//Loop will stop when i = 5!
              {
                 /*In a perfect world, the boolean expression will always yield true,
                 because i will increment until it reaches infinity, which is pretty     
                 hard to do! Unfortunately, each type has an specific size in the 
                 machine's registers so the loop will eventually stop working because
                 the number will wrap around. I won't get into details as to why i 
                 will go from positive to negative and thus become < 0, which yields
                 false. */
                 cout << "Loop has ran for: " << i << " times!" << std::endl;
              }
              Code:
              for(int i = 0; true ; i++)//Loop will stop when i = 5!
              {
                 /*The loop will always test true because true will always be true.
                 Something like this can be used to mimic a while loop, but that's
                 sort of silly. :P*/
                 cout << "Loop has ran for: " << i << " times!" << std::endl;
              }
              The final loop I want to introduce is the do loop. This loop behaves like the while loop, except it executes a block of code at least once!

              Code:
              char foo = 'x';
              do
              {
                 cout << "Type a letter!" << std::endl;
                 cin >> foo;
                 cout << std::endl;
              }while(foo != 'x'); //The loop will stop when foo = x, but only if the user typed x!
              Now, how would you use this information to ask the user for grade? Don't worry about being right or wrong. I just want you to start playing around with the language and the code. Post a code snippet of whatever solution comes to your head! :D
              Last edited by kiseitai2; Jun 18 '15, 03:32 PM. Reason: Forgot one set of code tags! :(

              Comment

              • kiseitai2
                New Member
                • Jul 2007
                • 93

                #8
                P.S. A loop is like having a parrot, it won't stop repeating the same thing until it dies or you tell it to shut up! :P
                I hope it helps!

                Comment

                • djselect24
                  New Member
                  • Jun 2015
                  • 5

                  #9
                  awesome thanks for the help Kiseitai2!!!
                  I am gonna try the while loop, I think that will work for what I am trying to do.

                  Comment

                  • kiseitai2
                    New Member
                    • Jul 2007
                    • 93

                    #10
                    You are welcomed! Come back if you get stuck again!

                    Comment

                    • djselect24
                      New Member
                      • Jun 2015
                      • 5

                      #11
                      sorry kiseitai2 but I have another question. So here is the code that I have now come up with and my question is. How do I get the name to change for the student because right now, once I enter the number of students and then the name. The name of the student stays the same for all the students.

                      Code:
                      #include <iostream>
                      #include <iomanip>
                      using namespace std;
                      
                      int main()
                      {
                      	int numStudents, numTests;
                      	double total, Avg;
                      	char StudentName;
                      	int A, B, C, D, F;
                      
                      	cout << fixed << showpoint << setprecision(1);
                      
                      	//this will help find number of students.
                      	cout <<"I will help you Average your student's test scores.\n";
                      	cout << "For how many students do you have scores?\n";
                      	cin >> numStudents;
                      
                      
                      	//this will help get the grades for students
                      	cout << "How many test scores does each student have\n";
                      	cin >> numTests;
                      
                      	//this will get the students name 
                      	cout << "What is the name of the student\n";
                      	cin >> StudentName;
                      
                      	//This will help get the average of the student
                      	for (int student = 1; student <= numStudents; student++)
                      	{
                      		total = 0; 
                      		for (int test = 1; test <= numTests; test++)
                      		{
                      			double score;
                      			cout << "Enter score must be A,B,C,D or F" << test << "for";
                      			cout << StudentName << ":";
                      			cin >> score;
                      			total += score;
                      			}
                      			Avg = total /numTests;
                                              		
                      			if (Avg < 100 && Avg >= 90){
                              cout << "A" << endl;
                              }else if (Avg < 90 && Avg >= 80){
                              cout << "B" << endl;
                              }else if (Avg < 80 && Avg >= 70){
                              cout << "C" << endl;
                              }else if (Avg < 70 && Avg >= 60){
                              cout << "D" << endl;
                              }else if (Avg < 60 && Avg >= 0){
                              cout << "F" << endl;
                              }
                              }                              		
                      		return 0;
                      
                      
                      
                      }
                      Last edited by Rabbit; Jun 20 '15, 06:15 PM. Reason: Fixed code tags

                      Comment

                      • kiseitai2
                        New Member
                        • Jul 2007
                        • 93

                        #12
                        Normally, I would solve the new problem by creating and/or using a data structure. However, from what I gather from earlier posts is that this approach would be beyond the scope of the tools you have at your reach. My original responses were geared towards building the average calculator for a single student. Now, for multiple students you can either run the program every time you need to compute the average grade or you could use a while loop to ask the user if s/he wants to compute the average for another student. The flow of the program would go as follows:

                        1)Intro -> 2) Ask for name -> 3) Ask for number of grades -> 4)Ask for grade in loop and compute average -> 5) Output name of student and the average grade -> 6) Ask user if s/he wants to compute the average for another student (example, if user types 'y' the while loops again from #2 to #6, otherwise, exit the loop) -> 7) Output final message (if any) -> 8) Exit program

                        In code, it would look something like you tried with the nested for loops, but like this:
                        Code:
                        while(exit == 'y' || exit == 'Y')
                        {
                           for(...)
                            {
                            }
                           std::cout << "Do you wish to compute for new student?" << std::endl;
                        }
                        That is how I would solve the problem when I can't use data structures of any kind. Also, do not worry about the term data structures at this stage in the game! :D

                        ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~
                        Based on the code you posted, I am gonna point out a couple of errors so you can fix them as you modify the program.

                        First, each type has its own size and characteristics from the point of view of the machine. For example, in int vs. unsigned int, both are (or used to be) around 32 bits. The difference is that int is signed so half of the type "space" is used to represent numbers that are negative (-1 to -big number), so for counting in the positive direction, you get half the numbers that this type could potentially represent. Unsigned integral (int) types tell the machine that you don't care about counting in the negative direction and would rather appreciate to use this space to double how many numbers you can represent in the positive axis until you reach the limit of the type.
                        Char is a special integer that is guaranteed to be 8 bits in size (goes from 0 to 255 if unsigned and 0 to 127 if signed). Char has another meaning, it is the integer used to represent characters in the ASCII table. Thus, the letter Y is represented internally by a number. Also, when dealing with single characters, you refer to them in code with single quotes. A set of double quotes implies that the contents ought to be treated as a string, which is actually an array (for a good explanation of arrays refer to this article by weaknessforcats ). Hence, 'Y' != "Y" ("Y" is actually more dangerous than 'Y').
                        Float and double deal with floating point numbers, which have their own internal notation (scientific-like notation, #e# -> 2e32).

                        Why do I bring this up? Well, you can't input a letter like 'A' into the variable score because their types differ ('A' = an int that may or may not become converted into its floating point alternative). Let's assume 'A' == 200, when I type 'A' in the cin prompt, the best that will happen is that score will equals something between 199.99999999999 9(etc) and 200.000000000(e tc)1. Of course, I expect the compiler to complain about this before you even get a program to run. What you need to do is to save this input into a char variable and do a conversion using if statements.

                        The variable StudentName is of type char, but char can oly hold 1 character, so when you as the user for the student name, only the first character should get stored in the variable (the rest of the input buffer may linger around or get discarded, I do not remember how cout will deal with it). As a result, you need an array of characters to store the name (for example, char StudentName[50];). I recommend you read the article about arrays! :D

                        I think these are all the things I want you to work on. I hope it helps!

                        Comment

                        Working...