初学者c++帮助:if语句中的if语句

Beginner C++ Help: if statements within if statements

本文关键字:if 语句 c++ 帮助 初学者      更新时间:2023-10-16

所以最近我开始学习我的第一门语言,c++,我正在写我自己的程序来得到一些练习。为了让你知道我已经学到了多少,我刚刚在课堂上介绍了"if"answers"else if"语句。下面是代码:

// 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;
}

这个程序应该是一个分数计算器,它接受老师的分数分类,并计算出你在班上是a、B、C、D还是F。我这样做完全是为了练习,它与我的c++类没有任何关系。

它运行得很好,但我发现了一个问题:在末尾,我放入了以下代码行,它适用于所有条件语句(firstname和lastname是用户输入他的名/姓的字符串)。cutoffa是接收用户输入的A的等级截止):

if (grade >= cutoffa) cout << firstname << " " << lastname << "'s Final Grade is " << grade << "%, which is an A! Congrats!";

问题是,如果某人的名字像Jake Shams,它会打印出"Jake Shams的最终成绩是98%,这是a !"恭喜!"这里的错误是"Shams's",因为应该是"Shams's"。就像我说的,我是初学者这是我第一次使用if语句如果有人能告诉我如何让它,如果用户的姓以字母S结尾程序在S前加一个撇号,像这样:Jake Shams'

使用if语句检查名称是否以s结尾。如果是以s结尾,则在原名称后面加上撇号。如果没有,添加"s"。然后从cout语句中删除"s"。

实际上,Jake Shams的是正确的,所以您不需要更改任何内容。

一些英语变体写成Jake Shams',但是,正式地,人们只在复数形式中省略所有格s(例如"all three cows' feet were digital")。

在任何正确的英语中,名字中的最后一个s都被s所取代(比如Jake Sham的),但是,为了方便讨论,你可以这样做:

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!";
}
但是,我稍微重写了一下,以便lastname_possessive包含整个单词。这种方式更易于重用,并且意味着lastname_possessive变量本身是有意义的。这样的:
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!";
}

现在更容易修改以呈现正确的英语:

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!";
}

实际上,您可能希望在条件之外执行此操作,以便lastname_possessive也可以在其他情况下使用。

我想lastname的类型是std::string。然后可以询问if (lastname.back() == 's')

如果字母"s"是名字的一部分,那么你不应该在名字前面加上"'"。

如果这个人的名字以"s"结尾,比如"Burns",那么为"Burn’s"设置程序代码是不对的。

无论如何-您可以创建一个字符串,存储它,并搜索"S"字符(位置)。

给你一个旁注-被接受的变量名的书写方式是使用一种叫做"CAMEL CASE"的东西,这仅仅意味着你将变量"cutoffa"写成"cutoffa"。除了第一个单词外,每个单词的第一个字母都是大写的。它会让你的代码更容易读懂。如果你需要更多帮助,请告诉我。