Skip to content

Instantly share code, notes, and snippets.

@sudomann
Created September 19, 2020 22:53
Show Gist options
  • Save sudomann/2bf8ba43b4c26d4094de932f3b02efb1 to your computer and use it in GitHub Desktop.
Save sudomann/2bf8ba43b4c26d4094de932f3b02efb1 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
int main() {
// I'll first create all the variables that are needed to store user inputs
// Student Name
string name;
// Participation Score
double participation;
// Test Score
double test;
// Assignment Score
double assignment;
// Exam Score
double exam;
// Practice Score
double practice;
/* Take Note:
- None of those variables have anything assigned to them,
i.e. the assignment operator `=` was not used to assign them a value
- Those variables' data type is `double`
- I could've also used `int`, but I just chose double because in my opinion
a non-integer score such as 85.5 points/percent should be valid input
Now you have to assign values to the variables you created
- You print out text by 'passing a string to the `cout` object`
- These values will come from keyboard input
- To read keyboard input in C++, one easy way is with the `cin` object
*/
// Print out a prompt for input
cout << "Enter student name: ";
/* The string, "Enter student name: "
was just passed to the `cout` object using the operator `<<`.
The `cout` object then outputs/prints that string.
*/
// Then read input
cin >> name;
/* When the program reaches this point, it pauses, because of `cin`.
It is awaiting keyboard input, and the the `>>` operator will the passed
this input, which will get stored in the variable on the right side of the `>>` operator.
In this case, that variable is `name`.
So whatever you type, once you hit Enter, it will attempt to store it, and your input
needs to match the data type of the variable it is getting stored into.
If the variable was of type, `int` such as `participation` is, your program would
only continue to work if you entered nothing but digits. No spaces, no symbols, just digits.
*/
// At this point we've entered and stored the name of the student, so we'll
// and store scores
cout << "Enter Participation Score: ";
cin >> participation;
cout << "Enter Test Score: ";
cin >> test;
cout << "Enter Assignment Score: ";
cin >> assignment;
cout << "Enter Exam Score: ";
cin >> exam;
cout << "Enter Practice Score: ";
cin >> practice;
// Now we can figure out the sum of the scores and their average
double sum = participation + test + assignment + exam + practice;
double average = sum / 5;
// And finally output the results
cout << name << ": Final Score: " << sum << " Average Score: " << average;
// I'm assuming here you don't need an explanation for printing out the values/contents
// of variables :D
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment