這是有問題的程序,很短的和甜:爲什麼我的第一個「if」語句不管輸入如何執行?
// Lab 5 Exercise 1
// Financial Aid Program
//
// Written by: Jared T. Fisher
#include <iostream>
#include <string>
using namespace std;
int main()
{
long int _income; // declaring the variables
char yesNo;
cout << "Are you an undergrad student?" ; // determining if they are an undergrad or not.
cin >> yesNo;
if ((yesNo = 'N') || (yesNo = 'n'))
{
cout << "You do not qualify for financial aid.";
}
else if ((yesNo = 'Y') || (yesNo = 'y'))
{
cout << "What is your current yearly income?";
cin >> _income;
if (_income <= 15000 && _income >= 0) // determining which aid they qualify for.
{
cout << "You qualify for $1000 of financial aid";
}
else if (_income > 15000)
{
cout << "You qualify for $500 of financial aid";
}
else
{
cout << "Your income must be greater than or equal to 0. Please close the program and try again.";
}
}
else
{
cout << "Your inputted value must be either 'y' or 'n'. Please close the program and try again.";
}
cout << endl << endl;
return 0;
} // end function main()
綜上所述,
如果用戶輸入「n」或「N」,他們馬上告訴他們不」沒有資格。
如果用戶輸入'y'或'Y',則會詢問他們的年收入,並且從中計算出資格。
如果用戶輸入除這些輸入以外的任何內容,則會收到錯誤消息。
然而,當我運行這個程序,無論我輸入「N」,「Y」或「棉花糖」,它總是有回報:
"You do not qualify for financial aid."
,好像我什麼輸入' n'或'N'。我的代碼有什麼問題?
,因爲你犯了一個菜鳥錯誤,而不是用'='賦值的'=='。 – 101010
如果你的編譯器沒有產生大量的警告,你應該拋出並獲得一個新的警告。 –