在简单的文件处理程序中获得异常输出
Getting unusual output in simple file handling program
这段代码有什么问题?我正在尝试写入二进制文件 MCQ 类型问题例如,问题:如果一台计算机向其他计算机提供数据库服务,那么它将被称为?1.网络服务器 2.应用 3.数据库服务器 4.FTP服务器
编号: 1
答案: 3
.
.
.
但我得到了不寻常的输出。请查看我提供注释的代码。程序询问问题并将其写入二进制文件。id 会自动递增,问题和答案由用户提出。如果问题是"x",则程序停止,如果是其他任何东西,它会询问答案并将所有三个id,question,answer设置为临时对象,然后将其写入文件。
该程序对于第一个问题和答案工作正常,然后直接询问第二个答案的答案,而无需询问第二个问题。
#include <iostream>
#include <fstream>
#include<string.h>
#include<conio.h>
using namespace std;
class questions{ //start of class
//private members
int id; //identification number for question ( 1,2,3,....)
char ques[100]; //question
int ans; //answer (1,2,3,4) MCQ
public: //public members
void displayques() //displays question
{
cout<<ques; //the same
}
int getans() //returns answer
{
return ans; //the same
}
int getid() //returns id
{
return id; //the same
}
void setques(char q[]) //set the given string in parameter to the question.
{
strcpy(ques,q);
}
void setid(int i) //set the given int as id
{
id=i;
}
void setans(int a) //set the given int as answer
{
ans=a;
}
}; //end of class questions
int main() //start of main
{
questions quesobj; //create an object of class questions
ofstream quesfile; //creates and object of class ofstream
quesfile.open("questions.dat",ios::out|ios::binary); //opens questions.dar in output mode as binary with the object quesfile of ofstream
char ask='y'; //to stop the process when its changed to n
char tempques[100]; //to temporarily store the question
int tempid=1; //to temporarily store the id, initially at 1, later is incremented
int tempans; //to temporarily store the answer
while(ask!='n') //runs upto the the point when use wants to escape the loop
{
cout<<"Question? or enter x to stop"<<endl; //asks the questions also ask if user want to escape can enter x
gets(tempques); //gets the question in temporary variable
cout<<"Question registered"<<endl; //shows question is ready to be written
if(strcmp(tempques,"x")==0) //if user enter the question as x
{
ask='n'; //sets ask to n which will end the loop
}
else //if user enters something else than x, it is considered the question
{
cout<<"Answer:"<<endl; //ask for the answer
cin>>tempans; //stores answer in temporary variable
cout<<"Answer registered"<<endl; //shows answer is ready to be written
quesobj.setid(tempid); //sets id to the temp object of the class questions
quesobj.setques(tempques); //sets question to the temp object of the class questions
quesobj.setans(tempans); //sets answer to the temp object of the class questions
quesfile.write((char *)&quesobj, sizeof(quesobj)); //write the temp object of class questions to the binary file
tempid++; //tempid is incremented for the next loop so that every question has its own id
}
} //end of while
quesfile.close(); // closes the file using the object of ofstream
}
输出:
Question? or enter x to stop
This is my first question?
Question registered
Answer:
2
Answer registered
Question? or enter x to stop
Question registered
Answer:
_
正如评论中所建议的,
不应混合使用 C 和C++样式的 IO。
由于您的代码未compiling on my system
,我已经清理了您的代码。我已经测试过它,它现在工作得很好。
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
int string_to_int(const string& str)
{
stringstream ss(str);
int str_int;
ss >> str_int;
return str_int;
}
class Question
{
int id;
string question_string;
int answer;
public:
void displayQuestion()
{
cout << question_string;
}
int getAnswer()
{
return answer;
}
int getId()
{
return id;
}
void setQuestion ( const string& question_string_)
{
question_string = question_string_;
}
void setId ( int id_ )
{
id = id_;
}
void setAnswer ( int answer_ )
{
answer = answer_;
}
};
int main()
{
Question question;
ofstream question_file;
question_file.open ( "questions.dat", ios::out | ios::binary );
char option = 'y';
string temp_question;
int temp_id =1;
int temp_answer;
while ( option != 'n' )
{
cout << "Question? or enter 'x' to stop" << endl;
getline(cin, temp_question);
cout << "Question registered" << endl;
if (temp_question == "x")
{
option = 'n';
}
else
{
cout << "Answer? :" << endl;
string temp_answer_string;
getline(cin, temp_answer_string);
temp_answer = string_to_int(temp_answer_string);
cout << "Answer registered" << endl;
question.setId ( temp_id );
question.setQuestion ( temp_question );
question.setAnswer ( temp_answer );
question_file.write ( ( char* ) &question, sizeof ( question ) );
temp_id++;
}
}
question_file.close();
}
相关文章:
- C++ 异常处理错误输出
- std::out_of_range异常奇怪的输出
- 是什么导致异常输出以及如何修复
- basic_string::替换的超出范围异常,而在范围内,正如调试相同参数的输出所证明的那样
- 从QT中的串行端口阅读时输出异常
- c++运算符=工作异常(函数内部输出正常,但返回后错误)
- C++日志异常到文件,不同的输出
- C++ 中窗口线程的异常输出
- 在简单的文件处理程序中获得异常输出
- C/C++:在异常终止前刷新输出
- 正在捕获输入和输出文件的异常
- 格式化的输出运算符标准iostream对象可以抛出什么样的异常
- 为什么这是此异常堆栈的输出
- 将char[]传递给另一个函数时出现异常输出
- 打印值0x89(-119)时出现异常输出
- 为什么这个程序不能输出正确的异常?
- Android OpenCV imread 标志致命异常,imread 在没有标志的情况下工作正常,像素输出与 MATLAB 不匹配
- 双指针输出数组参数异常
- c++程序输出整数时异常退出
- 尝试在打开的总账中创建空白窗口时出现异常输出