473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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!

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3.  
  4.         using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9.         //Declaring variables
  10.         char StudentsName [100]; 
  11.         float ExamValue, Sum, Avg,TotalGrades; //Total grades needs to equal # of grades submitted
  12.  
  13.         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//
  14.         int B = 3;
  15.         int C = 2;
  16.         int D = 1;
  17.         int F = 0;
  18.         string input = ""; 
  19.  
  20.         //declaring students name
  21.         cout << "Enter students name:"; 
  22.         cin >> StudentsName;
  23.         cout << endl;
  24.  
  25.         //declaring grade
  26.         cout << "Enter Grade must be A,B,C,D, or F:";
  27.         cin >> ExamValue;
  28.  
  29.  
  30.         //STILL NEED TO WORK ON THIS EQUATION
  31.         Avg = Sum / TotalGrades;
  32.  
  33.         cout << "Final Grade: " << Avg << endl;
  34.  
  35.         //Calculating grade
  36.         cout << "Grade: ";
  37.         if (Avg == 100){
  38.         cout << "A+" << endl;
  39.         }else if (Avg < 4 && Avg >= 3){
  40.         cout << "A" << endl;
  41.         }else if (Avg < 3 && Avg >= 2){
  42.         cout << "B" << endl;
  43.         }else if (Avg < 2 && Avg >= 1){
  44.         cout << "C" << endl;
  45.         }else if (Avg < 1 && Avg >= 0){
  46.         cout << "D" << endl;
  47.         }else if (Avg < 0){
  48.         cout << "F" << endl;
  49.         }
  50.  
  51.         return 0;
  52.         }
  53.  
  54.  
Jun 17 '15 #1

✓ answered by kiseitai2

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.
Expand|Select|Wrap|Line Numbers
  1. char foo = 'a';
  2. while(a != 'x')//Loop will stop when user types x!
  3. {
  4.    cout << "Type a letter!" << std::endl;
  5.    cin >> foo;
  6.    cout << std::endl;
  7. }
The for loop will repeat a block of code a set number of times:

Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < 5; i++)//Loop will stop when i = 5!
  2. {
  3.    cout << "Loop has ran for: " << i << " times!" << std::endl;
  4. }
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:

Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < 5; i++)//Loop will stop when i = 5!
  2. {
  3.    cout << "Loop has ran for: " << i << " times!" << std::endl;
  4.    i = 0;//This will prevent the loop from reaching 5 because it will always be set to 0!
  5. }
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i > 0; i++)//Loop will stop when i = 5!
  2. {
  3.    /*In a perfect world, the boolean expression will always yield true,
  4.    because i will increment until it reaches infinity, which is pretty     
  5.    hard to do! Unfortunately, each type has an specific size in the 
  6.    machine's registers so the loop will eventually stop working because
  7.    the number will wrap around. I won't get into details as to why i 
  8.    will go from positive to negative and thus become < 0, which yields
  9.    false. */
  10.    cout << "Loop has ran for: " << i << " times!" << std::endl;
  11. }
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; true ; i++)//Loop will stop when i = 5!
  2. {
  3.    /*The loop will always test true because true will always be true.
  4.    Something like this can be used to mimic a while loop, but that's
  5.    sort of silly. :P*/
  6.    cout << "Loop has ran for: " << i << " times!" << std::endl;
  7. }
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!

Expand|Select|Wrap|Line Numbers
  1. char foo = 'x';
  2. do
  3. {
  4.    cout << "Type a letter!" << std::endl;
  5.    cin >> foo;
  6.    cout << std::endl;
  7. }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

11 1841
https://wikis.engrade.com/help/grade_calculation

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.
Jun 17 '15 #2
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?
Jun 17 '15 #3
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.
Jun 17 '15 #4
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.
Jun 18 '15 #5
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.
Jun 18 '15 #6
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.
Expand|Select|Wrap|Line Numbers
  1. char foo = 'a';
  2. while(a != 'x')//Loop will stop when user types x!
  3. {
  4.    cout << "Type a letter!" << std::endl;
  5.    cin >> foo;
  6.    cout << std::endl;
  7. }
The for loop will repeat a block of code a set number of times:

Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < 5; i++)//Loop will stop when i = 5!
  2. {
  3.    cout << "Loop has ran for: " << i << " times!" << std::endl;
  4. }
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:

Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < 5; i++)//Loop will stop when i = 5!
  2. {
  3.    cout << "Loop has ran for: " << i << " times!" << std::endl;
  4.    i = 0;//This will prevent the loop from reaching 5 because it will always be set to 0!
  5. }
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i > 0; i++)//Loop will stop when i = 5!
  2. {
  3.    /*In a perfect world, the boolean expression will always yield true,
  4.    because i will increment until it reaches infinity, which is pretty     
  5.    hard to do! Unfortunately, each type has an specific size in the 
  6.    machine's registers so the loop will eventually stop working because
  7.    the number will wrap around. I won't get into details as to why i 
  8.    will go from positive to negative and thus become < 0, which yields
  9.    false. */
  10.    cout << "Loop has ran for: " << i << " times!" << std::endl;
  11. }
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; true ; i++)//Loop will stop when i = 5!
  2. {
  3.    /*The loop will always test true because true will always be true.
  4.    Something like this can be used to mimic a while loop, but that's
  5.    sort of silly. :P*/
  6.    cout << "Loop has ran for: " << i << " times!" << std::endl;
  7. }
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!

Expand|Select|Wrap|Line Numbers
  1. char foo = 'x';
  2. do
  3. {
  4.    cout << "Type a letter!" << std::endl;
  5.    cin >> foo;
  6.    cout << std::endl;
  7. }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
Jun 18 '15 #7
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!
Jun 18 '15 #8
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.
Jun 18 '15 #9
You are welcomed! Come back if you get stuck again!
Jun 18 '15 #10
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.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int numStudents, numTests;
  8.     double total, Avg;
  9.     char StudentName;
  10.     int A, B, C, D, F;
  11.  
  12.     cout << fixed << showpoint << setprecision(1);
  13.  
  14.     //this will help find number of students.
  15.     cout <<"I will help you Average your student's test scores.\n";
  16.     cout << "For how many students do you have scores?\n";
  17.     cin >> numStudents;
  18.  
  19.  
  20.     //this will help get the grades for students
  21.     cout << "How many test scores does each student have\n";
  22.     cin >> numTests;
  23.  
  24.     //this will get the students name 
  25.     cout << "What is the name of the student\n";
  26.     cin >> StudentName;
  27.  
  28.     //This will help get the average of the student
  29.     for (int student = 1; student <= numStudents; student++)
  30.     {
  31.         total = 0; 
  32.         for (int test = 1; test <= numTests; test++)
  33.         {
  34.             double score;
  35.             cout << "Enter score must be A,B,C,D or F" << test << "for";
  36.             cout << StudentName << ":";
  37.             cin >> score;
  38.             total += score;
  39.             }
  40.             Avg = total /numTests;
  41.  
  42.             if (Avg < 100 && Avg >= 90){
  43.         cout << "A" << endl;
  44.         }else if (Avg < 90 && Avg >= 80){
  45.         cout << "B" << endl;
  46.         }else if (Avg < 80 && Avg >= 70){
  47.         cout << "C" << endl;
  48.         }else if (Avg < 70 && Avg >= 60){
  49.         cout << "D" << endl;
  50.         }else if (Avg < 60 && Avg >= 0){
  51.         cout << "F" << endl;
  52.         }
  53.         }                                      
  54.         return 0;
  55.  
  56.  
  57.  
  58. }
  59.  
Jun 20 '15 #11
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:
Expand|Select|Wrap|Line Numbers
  1. while(exit == 'y' || exit == 'Y')
  2. {
  3.    for(...)
  4.     {
  5.     }
  6.    std::cout << "Do you wish to compute for new student?" << std::endl;
  7. }
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.999999999999(etc) and 200.000000000(etc)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!
Jun 21 '15 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Paul Mendez | last post by:
I really need some help Date Code ConCAT Bal_Fwd NS_Fees Amt_Coll Cur_End_Bal 1/15/2004 KW 11KW2003 $500.00 $250.00 $250.00 2/15/2004 KW 12KW2003 $300.00 $500.00 ...
6
by: Rafael | last post by:
Hi Everyone, I need some help with my calculator program. I need my program to do 2 arguments and a 3rd, but the 3rd with different operators. Any help would be great. Here is my code.... ...
1
by: Matthew Wilson | last post by:
I need to write a function crc(msg, len) that gets a char array of length len and then calculates the crc32 for the code. I don't understand what's going wrong in the code I have. It goes...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
1
by: KiMcHeE | last post by:
Hi! I'm having a real difficult time trying to figure out what is wrong with my program code. I'm trying to make a "calculator" in the C language and I only know basic stuff. Here's my code: ...
7
by: soule | last post by:
Hello, Fellow Users, I need a brave soul (or 5) to peruse this de-sensitized button code for any obvious or unobvious errors. In the VB editor, there are no red “error” lines of text...but I haven’t...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.