#include
#include
#include
using namespace std;
int main()
{
ifstream in("question.txt");
string filename;
string line;
if (in)
{
while (getline(in, line))
{
cout << line << endl;
}
}
else
{
cout << "no such file" << endl;
return 0;
}
}
假设question.txt的内容为
问题(1)1+1=?
选项1 1
选项2 2
选项3 3
选项4 4
正确答案 2
如何再做成可以输入答案后判断对错的可做问题。请指教
把从文件读出来的字符串解析出来,分别用两个变量来保存,比如: a= str[0]-’0';
int main()
{
ifstream in(_T("11.txt"));
string filename;
string line;
bool bFlag = false;
char cData;
if (in)
{
while (getline(in, line))
{
if (bFlag)
{
if (line.find(cData) == 9)
{
bFlag = false;
cout <<"OK"<<endl;
}
else
{
cout <<"Error"<<endl;
}
}
cout << line << endl;
if (line.find("选项4") == 0)
{
cout <<"输入正确的选项:";
cin>>cData;
bFlag = true;
}
}
}
else
{
cout << "no such file" << endl;
return 0;
}
return 0;
}
一个简单的思路。。