【发布时间】:2014-03-19 06:38:28
【问题描述】:
我的程序由三个文件组成。 arithcard.hpp(标题)、assign2.cpp(主程序)和 arithcard.cpp(方法)。
在数组中,卡片是存储从文本文件中读取的字符串。例如,cards[0].fileLine 的字符串为 2 + 2 = 4。(fileLine 是包含所有字符串的私有成员变量),cards[1].fileLine 的字符串为 2 + 4 = 3,等等...
我知道 card[i].fileLine 包含所有这些字符串,因为我通过在 readCards 方法中将它们全部打印出来来测试它们是否真的在其中。
这是我的问题,当我尝试在主程序中调用cards[i].getQuestion(或.getAnswer())时。有时,它会向我显示问题(或答案)或错误
"在抛出 'std::length_error' 的实例后调用终止 what(): basic_string::_S_create"
arithcard.hpp
#include <iostream>
#include <string>
/*
Defines the class ArithCard, for handling arithmetic flash cards.
You may not modify the public method declarations provided below. The main
program used to test your solution will assume that these methods exist.
You can add private attributes and methods as you need them.
You may find it convenient to add additional public methods for testing.
Keep in mind that your code must work with the unmodified main program
or it will fail automated testing.
*/
class ArithCard {
public:
// Default constructor
ArithCard() ;
static bool readCards(const std::string &fileName,
int &cardCnt,ArithCard *&cards) ;
void displayQuestion(std::ostream &out) ;
bool checkAnswer(std::istream &in) ;
// Return the question as a string.
std::string getQuestion() ;
// Return the answer as a string.
std::string getAnswer() ;
// Return the answer as an integer.
int getAnswerValue() ;
private:
// Add your private methods and attributes here.
std::string fileLine;
std::string question;
std::string answer;
} ;
arithcard.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "arithcard.hpp"
using namespace std;
ArithCard::ArithCard()
{
//body intentionally empty
}
bool ArithCard::readCards(const string &fileName, int &cardCnt, ArithCard *&cards)
{
//read the file line by line and store it in cards
return (true);
}
void displayQuestion(std::ostream &out)
{
//display output
}
bool checkAnswer(std::istream &in)
{
return (true);
}
// Return the question as a string.
string ArithCard::getQuestion()
{
size_t pos = 0;
pos = fileLine.find("=");
question = fileLine.substr(0, pos);
return question;
}
// Return the answer as a string.
string ArithCard::getAnswer()
{
size_t pos = 0;
pos = fileLine.find("=") + 1;
answer = fileLine.substr(pos);
return answer;
}
// Return the answer as an integer.
int getAnswerValue()
{
return answer;
}
assign2.cpp
#include <iostream>
#include <sstream>
#include <string>
#include <limits>
#include <random>
#include "arithcard.hpp"
// File-local anonymous namespace
namespace {
int verbosity = 1 ;
}
/*
Read in the files of cards, then loop: query the user to specify a
question, print the question, check the answer. Terminate when the user
types an empty line instead of specifying a question.
*/
int main (int argc, const char *argv[])
{
if (argc != 2) {
std::cout << "Usage: " << argv[0] << " <datafile>" << std::endl ;
return (1) ;
}
std::string dataFile(argv[1]) ;
bool result = false ;
int cardCnt = -1 ;
ArithCard *cards = nullptr ;
/*
Parse the file of questions & answers. A false return value indicates an
absent file or other i/o error. It's also possible the file is present but
contains no valid questions.
*/
result = ArithCard::readCards(dataFile,cardCnt,cards) ;
std::cout << cards[0].getQuestion() << std:: endl;
return (0) ;
}
【问题讨论】:
标签: c++ arrays string class c++11