Studi Kasus C++ 04 - Predikat Nilai Menggunakan Bahasa Pemrograman C++
Table of Contents
Introduction
In this tutorial, we will explore how to create a program in C++ that determines student grades based on their scores. This guide will take you through the logical framework needed for the program and its implementation in C++. By the end, you should have a solid understanding of how to build a grading system using C++.
Step 1: Understand the Logic for Grading
Before writing any code, you need to establish the grading criteria. Here are the common grading ranges:
- A: 85-100
- B: 70-84
- C: 55-69
- D: 40-54
- E: 0-39
Practical Tips
- Make sure the ranges do not overlap.
- Consider edge cases, such as what should happen if a score is exactly at the boundary.
Step 2: Set Up Your C++ Environment
To write and run your C++ code, you need a suitable development environment. Follow these steps:
- Install a C++ compiler. Popular options include:
- Dev-C++
- Code::Blocks
- Visual Studio Code
- Create a new C++ file (e.g.,
grading_system.cpp
).
Common Pitfalls
- Ensure your compiler is correctly set up to avoid any runtime errors.
Step 3: Write the Basic Program Structure
Begin by including the necessary headers and defining the main
function. Here’s a simple skeleton for your program:
#include <iostream>
using namespace std;
int main() {
// Your code will go here
return 0;
}
Step 4: Declare Variables
You need variables to store the student's score and their corresponding grade. Declare them at the beginning of your main
function:
int score;
char grade;
Step 5: Get User Input
Prompt the user to enter their score. Use cin
to read the input:
cout << "Enter your score: ";
cin >> score;
Step 6: Determine the Grade
Using conditional statements, assign a grade based on the score input. Here’s how to implement this logic:
if (score >= 85) {
grade = 'A';
} else if (score >= 70) {
grade = 'B';
} else if (score >= 55) {
grade = 'C';
} else if (score >= 40) {
grade = 'D';
} else {
grade = 'E';
}
Step 7: Output the Result
Finally, display the grade to the user. Use cout
for output:
cout << "Your grade is: " << grade << endl;
Step 8: Compile and Run Your Program
- Save your file and compile your code using your C++ compiler.
- Run the program and test it with different score inputs to ensure it works correctly.
Real-World Application
This basic grading system can be expanded to include features such as:
- Handling multiple students' scores.
- Storing scores in arrays.
- Calculating average scores.
Conclusion
You have now created a simple C++ program that determines a student's grade based on their score. This foundational knowledge can be built upon for more complex applications in C++. Next steps may include adding more features to your program, exploring data structures, or learning about file handling in C++. Happy coding!