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

时间:2015-09-17 18:52:06

标签: c++

所以最近我开始学习我的第一语言C ++,并且我正在编写自己的程序来进行练习。为了让你知道我有多远,我刚刚被介绍给"如果"和"否则如果"课堂上的陈述。这是代码:

// 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这样的名字,它会打印出来&#34; Jake Shams的最终成绩为98%,这是A!恭喜!&#34 ;.这里的错误是&#34; Shams&#34;&#34;因为它应该是&#34; Shams&#39;&#34;。就像我说的那样,我是初学者,这是我第一次使用if语句,所以如果有人可以向我解释如何获得它,那么如果用户的姓氏以字母S结尾,程序会在S之前添加一个撇号,像这样:Jake Shams'

4 个答案:

答案 0 :(得分:2)

使用if语句检查名称是否以s结尾。如果是,请将撇号添加到原始名称。如果没有,请添加。然后从你的cout语句中删除&。

答案 1 :(得分:1)

实际上, Jake Shams的是正确的,所以你不需要改变任何东西。

英语的一些变体写了 Jake Shams&#39; ,但是,正式地说,只有一个人为了复数而放弃了所有权 s (例如&#34;所有三头奶牛的脚都是数字的&#34; )。

在没有正确的英语中,名称中的最终 s 完全被&#39> 取代(例如 Jake Sham&#39; )但是,为了论证,你可以这样做:

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

答案 2 :(得分:0)

我认为lastname的类型为std::string。然后,您可以询问if (lastname.back() == 's')

答案 3 :(得分:-1)

你不应该成为一个&#34;&#39;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;在一个名字之前的字母&#34; s&#34;是名字的一部分。

如果此人的姓名以&#34; s&#34;结尾?喜欢&#34;伯恩斯&#34;拥有&#34; Burn&#34;的程序代码是不对的。

无论如何 - 你可以创建一个字符串,存储它,并搜索&#34; S&#34;角色(位置)。

在旁注中 - 接受的编写变量名称的方法是使用名为&#34; CAMEL CASE&#34;这只是意味着你要写你的变量&#34; cutoffa&#34; as&#34; cutOffA&#34;。除了第一个单词之外,单词的每个首字母都大写。它将使您的代码更具可读性。如果您需要更多帮助,请告诉我。