这是有问题的程序,很短的和甜:为什么我的第一个“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
如果你的编译器没有产生大量的警告,你应该抛出并获得一个新的警告。 –