Reputation: 141
So recently I started learning my first language, C++ and I'm writing my own program to get some practice. To let you know how far along I am, I have just been introduced to "if" and "else if" statements in class. Here is the code:
// Grade Calculator
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;
int main()
{
float cutoffa, cutoffb, cutoffc, cutoffd, grade;
string firstname, lastname;
cout << setprecision(3) << fixed;
cout << "This program was made to test the \"if\" and \"else if\" statements.\n";
cout << "For this program you will be pretending to be a professor who is entering a student's grade.\n";
cout << "The first thing you will need to do is provide us with some basic information. Press enter when you are ready. \n";
cin.get();
cout << "What is the percent grade cut off (the lowest grade possible) for an A ? " ;
cin >> cutoffa;
cout << "What is the percent grade cut off for an B? " ;
cin >> cutoffb;
cout << "What is the percent grade cut off for an C? " ;
cin >> cutoffc;
cout << "What is the percent grade cut off for an D? " ;
cin >> cutoffd;
cout << "Enter the student's first name: ";
cin >> firstname;
cout << "Enter the student's last name: ";
cin >> lastname;
cout << "Enter the student's overall grade percentage: ";
cin >> grade;
if (grade >= cutoffa)
cout << firstname << " " << lastname << "'s Final Grade is " << grade << "%, which is an A! Congrats!";
else if (grade < cutoffa && grade >= cutoffb)
cout << firstname << " " << lastname << "'s Final Grade is " << grade << "%, which is a B! Pretty good, could be better though.";
else if (grade < cutoffb && grade >= cutoffc)
cout << firstname << " " << lastname << "'s Final Grade is " << grade << "%, which is a C! You're average - nothing more and nothing less.";
else if (grade < cutoffc && grade >= cutoffd)
cout << firstname << " " << lastname << "'s Final Grade is " << grade << "% which is D! Damn, you're stupid";
else if (grade < cutoffd)
cout << firstname << " " << lastname << "'s Final Grade is " << grade << "% which is an F! Have you considered dropping out?";
cin.get();
cin.get();
return 0;
}
The program is supposed to be a grade calculator which takes the teacher's grade breakdown and calculates whether you have an A, B, C, D, or F in the class. I'm making this solely for the reason of practicing and it doesn't have anything to do with my C++ class.
It runs nice and all, but I've figured out a problem with it: Towards the end I put the following line of code, which applies to all the conditional statements (firstname and lastname are strings where the user enters his first/last name. cutoffa is the grade cut off for receiving an A that the user inputs):
if (grade >= cutoffa)
cout << firstname << " " << lastname << "'s Final Grade is " << grade << "%, which is an A! Congrats!";
The problem is that if someone has a name like Jake Shams, it will print out "Jake Shams's final grade is 98%, which is an A! Congrats!". The mistake here is the "Shams's" because it should be "Shams'". Like I said, I'm a beginner and this is my first time using if statements so if anyone can explain to me how to get it so that if the users last name ends with the letter S the program adds an apostrophe before the S, like so: Jake Shams'
Upvotes: 0
Views: 153
Reputation: 385104
Actually, Jake Shams's is correct, so you do not need to change anything.
Some variants of English write Jake Shams' instead but, formally, one only drops the possessive s for a plural (e.g. "all three cows' feet were digital").
In no correct English is the final s in a name altogether replaced by 's (like Jake Sham's) but, for argument's sake, you could do it like this:
if (grade >= cutoffa) {
// Copy "lastname" and strip any terminating "s",
// in advance of appending "'s" in a moment
string lastname_possessive = lastname;
if (lastname_possessive.back() == 's')
lastname_possessive.pop_back();
cout << firstname << " " << lastname_possessive << "'s Final Grade is "
<< grade << "%, which is an A! Congrats!";
}
However, I'd rewrite it slightly so that lastname_possessive
contains the whole word. It's more reusable that way and means lastname_possessive
the variable makes sense on its own. Like this:
if (grade >= cutoffa) {
string lastname_possessive = lastname;
if (lastname_possessive.back() == 's')
lastname_possessive.pop_back();
// Now do that appending
lastname_possessive += "'s";
cout << firstname << " " << lastname_possessive << " Final Grade is "
<< grade << "%, which is an A! Congrats!";
}
Now it's easier to change this to render proper English:
if (grade >= cutoffa) {
string lastname_possessive = lastname;
if (lastname_possessive.back() == 's')
lastname_possessive += "'"; // technically wrong but sometimes used
else
lastname_possessive += "'s";
cout << firstname << " " << lastname_possessive << " Final Grade is "
<< grade << "%, which is an A! Congrats!";
}
You'd probably want to do this outside of the conditional, actually, so that lastname_possessive
can be used in other cases too.
Upvotes: 1
Reputation: 31
Have an if statement checking if the name ends with an s. If it does, add an apostrophe to the original name. If not, add 's. Then remove the 's from your cout statement.
Upvotes: 2
Reputation: 116
There shouldn't be a reason for you to place an "'" before a name where the letter "s" is apart of the name.
If the person has a name that ends with "s" like "Burns" it wouldn't be right to have a program code for "Burn's".
Regardless- you could create a string, store it, and search for the "S" character (position).
On a side note for you- the accepted way of writing variable names is by using something called "CAMEL CASE" which simply means you'd write your variable "cutoffa" as "cutOffA". Every first letter of a word is capitalized except for the first initial word. It will make your code way more readable. Let me know if you need more help.
Upvotes: -1
Reputation: 5209
I suppose lastname
is of type std::string
. You can then ask if (lastname.back() == 's')
.
Upvotes: 0