- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
这段代码有什么问题?我正在尝试写入二进制文件 MCQ 类型问题例如,问题:如果一台计算机向其他计算机提供数据库服务,那么它将被称为? 1.Web服务器 2.应用程序 3.数据库服务器 4.FTP服务器
编号:1
答案:3
.
.
.
但是我得到了异常的输出。请查看我提供评论的代码。该程序提出问题并将其写入二进制文件。id 自动递增,问题和答案由用户询问。如果问题是“x”,则程序停止,如果还有其他问题,它会要求答案并将所有三个 id、问题、答案设置为一个临时对象,然后将其写入文件。
程序在第一个问题和答案上运行良好,然后直接询问第二个答案的答案而不询问第二个问题。
#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++ style IO。
由于您的代码未在我的系统上编译
,因此我稍微清理了您的代码。我已经对其进行了测试,它现在运行良好。
#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++ - 在简单的文件处理程序中获得异常输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35119323/
我正在努力实现以下目标, 假设我有字符串: ( z ) ( A ( z ) ( A ( z ) ( A ( z ) ( A ( z ) ( A ) ) ) ) ) 我想编写一个正则
给定: 1 2 3 4 5 6
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
大家好,我卡颂。 Svelte问世很久了,一直想写一篇好懂的原理分析文章,拖了这么久终于写了。 本文会围绕一张流程图和两个Demo讲解,正确的食用方式是用电脑打开本文,跟着流程图、Demo一
身份证为15位或者18位,15位的全为数字,18位的前17位为数字,最后一位为数字或者大写字母”X“。 与之匹配的正则表达式: ?
我们先来最简单的,网页的登录窗口; 不过开始之前,大家先下载jquery的插件 本人习惯用了vs2008来做网页了,先添加一个空白页 这是最简单的的做法。。。先在body里面插入 <
1、MySQL自带的压力测试工具 Mysqlslap mysqlslap是mysql自带的基准测试工具,该工具查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出
前言 今天大姚给大家分享一款.NET开源(MIT License)、免费、简单、实用的数据库文档(字典)生成工具,该工具支持CHM、Word、Excel、PDF、Html、XML、Markdown等
Go语言语法类似于C语言,因此熟悉C语言及其派生语言( C++、 C#、Objective-C 等)的人都会迅速熟悉这门语言。 C语言的有些语法会让代码可读性降低甚至发生歧义。Go语言在C语言的
我正在使用快速将 mkv 转换为 mp4 ffmpeg 命令 ffmpeg -i test.mkv -vcodec copy -acodec copy new.mp4 但不适用于任何 mkv 文件,当
我想计算我的工作簿中的工作表数量,然后从总数中减去特定的工作表。我错过了什么?这给了我一个对象错误: wsCount = ThisWorkbook.Sheets.Count - ThisWorkboo
我有一个 perl 文件,用于查看文件夹中是否存在 ini。如果是,它会从中读取,如果不是,它会根据我为它制作的模板创建一个。 我在 ini 部分使用 Config::Simple。 我的问题是,如果
尝试让一个 ViewController 通过标准 Cocoa 通知与另一个 ViewController 进行通信。 编写了一个简单的测试用例。在我最初的 VC 中,我将以下内容添加到 viewDi
我正在绘制高程剖面图,显示沿路径的高程增益/损失,类似于下面的: Sample Elevation Profile with hand-placed labels http://img38.image
嗨,所以我需要做的是最终让 regStart 和 regPage 根据点击事件交替可见性,我不太担心编写 JavaScript 函数,但我根本无法让我的 regPage 首先隐藏。这是我的代码。请简单
我有一个非常简单的程序来测量一个函数花费了多少时间。 #include #include #include struct Foo { void addSample(uint64_t s)
我需要为 JavaScript 制作简单的 C# BitConverter。我做了一个简单的BitConverter class BitConverter{ constructor(){} GetBy
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我是 Simple.Data 的新手。但我很难找到如何进行“分组依据”。 我想要的是非常基本的。 表格看起来像: +________+ | cards | +________+ | id |
我现在正在开发一个 JS UDF,它看起来遵循编码。 通常情况下,由于循环计数为 2,Alert Msg 会出现两次。我想要的是即使循环计数为 3,Alert Msg 也只会出现一次。任何想法都
我是一名优秀的程序员,十分优秀!