2015-09-17 139 views
0

因此,最近我開始學習第一語言C++,並且正在編寫自己的程序以獲得一些練習。爲了讓你知道我有多遠,我剛剛介紹了課堂上的「if」和「else if」陳述。下面是代碼:Beginner C++ Help:if語句中的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是用於接收將切斷的檔次,用戶輸入):

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

的問題是,如果有人有像傑克沙姆斯的名稱,它會打印出「傑克沙姆斯的最終成績是98%,這是A!恭喜!「。這裏的錯誤是「Shams's」,因爲它應該是「Shams」。就像我說的,我是初學者,這是我第一次使用if語句,所以如果有人能向我解釋如何得到它,那麼如果用戶姓氏以字母S結尾,程序會在S之前添加一個撇號,像這樣:Jake Shams'

+1

體面的代碼,做得很好。我們更喜歡將[最小但完整的]代碼發佈到_here_上,因爲鏈接會死機,Stack Overflow上的所有問題都可以幫助未來的讀者,而不僅僅是您現在! –

+0

謝謝我現在編輯帖子! – przm

+1

你不必寫'else if(grade = cutoffb)',你可以寫'else if(grade> = cutoffb)',因爲當你到達else時,'grade alain

回答

0

我想lastnamestd::string類型。然後您可以詢問if (lastname.back() == 's')

+0

謝謝,是的,這是一個字符串變量。因此'.back()=='s''是檢查給定變量(在本例中是lastname)中最後一個空格的命令嗎? – przm

+0

'std :: string :: back()'將返回給定'std :: string'的最後一個成員的引用,在你的情況下,是lastname的最後一個字母。你想要它,然後比較''s''來檢查是否以's'結尾。 – Zereges

1

其實,傑克沙姆斯的是正確的,所以你不需要改變任何東西。

英語的一些變體寫傑克沙姆斯代替,但形式上,一個只下降了複數所有格小號(例如「所有三頭牛的腳是數字」)。

在沒有正確的英語是最後小號的名稱由完全取代(如傑克深水的),但是,爲了便於討論,你可以做這樣的:

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可以用在其他情況也一樣。

-1

您應該沒有理由在名稱前面加上「'」,而名稱中的字母「s」與名稱分開。

如果此人的名字以「s」結尾,如「Burns」,那麼爲「Burn's」生成程序代碼將是不對的。

無論 - 你可以創建一個字符串,存儲它,並搜索「S」字符(位置)。

在附註中 - 可接受的寫變量名稱的方法是使用一種叫做「CAMEL CASE」的方法,這意味着您將變量「cutoffa」寫爲「cutOffA」。除了第一個單詞外,單詞的每個首字母大寫。它會讓你的代碼更可讀。讓我知道你是否需要更多的幫助。

+3

完全廢話。變量名稱沒有「接受」的案例約定。每個人都有自己的喜好。 –

+0

下劃線怎麼樣?可接受的變量命名方式是「cut_off_a」嗎? –

+0

當然。 –

2

有if語句檢查名稱是否以s結尾。如果是這樣,請將撇號添加到原始名稱。如果不是,請添加's。然後從您的cout聲明中刪除。

+0

謝謝!我不熟悉代碼是這樣做的。在閱讀完這裏的評論之後,我假設'lastname.back()=='s''將檢查存儲在lastname中的最後一個字母是否是's'。 – przm

+0

@przm:是的。 –

+0

@LightnessRacesinOrbit謝謝,我能夠得到它的工作! cpp.sh/2ziz – przm