Reputation: 3
My program consists of three files. arithcard.hpp (header), assign2.cpp (the main program) and arithcard.cpp(method).
In the array, cards, is store strings read from a text file. For example, cards[0].fileLine has the string 2 + 2 = 4. (fileLine is a private member variable with all the strings), cards[1].fileLine has the string 2 + 4 = 3, etc...
I know cards[i].fileLine contains all these strings because I tested by printing them all out in the readCards method to see if they are actually in there.
Here is my problem, when I try to call cards[i].getQuestion (or .getAnswer()) in the main program. Sometimes, it shows me the question(or answer) or the error
"terminate called after throwing an instance of 'std::length_error' what(): basic_string::_S_create"
#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;
} ;
#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;
}
#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) ;
}
Upvotes: 0
Views: 1972
Reputation: 109089
From §21.4.2/1 [string.require],
If any operation would cause
size()
to exceedmax_size()
, that operation shall throw an exception object of typelength_error
.
So somewhere in your code you're attempting to create a string whose length exceeds string::max_size()
, which is typically a huge number, so you should feel proud of yourself :-)
Jokes aside, it's difficult to tell where the error is because it looks like you've removed relevant sections of code, especially the implementation of ArithCard::readCards
. I don't understand why you chose to make this function static
and then pass it a pointer to an instance of ArithCard
, instead of just making it a non-static member function.
Your code has the pointer you're passing to ArithCard::readCards
initialized to nullptr
, so I assume you're allocating memory for the object within the function. If you're not, then you most likely have undefined behavior going on within that function.
I'd change that function to
class ArithCard {
public:
...
bool readCards(const std::string &fileName, int &cardCnt) ;
};
And change the code within main()
to
ArithCard cards;
result = cards.readCards(dataFile, cardCnt);
Also, in both getQuestion()
and getAnswer()
you're not checking the return value of string::find
, so attempting to extract a substring using a invalid result may be the cause of this error. This is the most likely explanation because string::find
will return string::npos
, a huge number, if the search term is not found.
Upvotes: 1