[英]C++ Program not Outputting as expected using .get
My prototype program isn't outputting as expected. 我的原型程序未按预期输出。 I want to store 'Y' within the string active , but it's storing NA instead.
我想将'Y'存储在字符串active中 ,但它存储的是NA。
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile;
char testChar;
string active;
inFile.open("test.dat");
inFile.get(testChar);
if (testChar = 'N')
{
inFile.get(testChar);
if (testChar = 'A')
active = "NA";
else
active = "N";
}
else
active = "Y";
cout << endl << active << endl << endl;
system("pause");
return 0;
}
My textfile(text.dat) is just 我的textfile(text.dat)只是
Y
My expected output is Y 我的预期输出是Y
Actual Output NA 实际输出NA
Not sure why this is happening 不知道为什么会这样
It's important to understand the difference between assignment ( =
) and testing for equality ( ==
). 了解分配(
=
)和相等性测试( ==
)之间的区别很重要。
Change: 更改:
if (testChar = 'a')
To 至
if (testChar == 'a')
And similarly for the other cases. 对于其他情况也是如此。
Note that if you had compiled with warnings enabled the compiler would have reported these simple mistakes. 请注意,如果在启用警告的情况下进行编译,则编译器将报告这些简单的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.