I'm writing a source and header file that are implemented by a program with some other files, and I need to make the code work by only editing the one source/header files (game.h and game.cpp). I'm trying to call another function from another source/header file, but it's not working right. Here's my code
game.cpp
#include "game.h"
#include "guesser.h"
#include "provider.h"
using namespace std;
const char FILL_CHARACTER = '.';
string wordSoFar;
int numMissedGuesses;
const int MAX_MISTAKE_LIMIT = 10;
void Game::setUpGame (int wordLength)
{
wordSoFar = string(wordLength, FILL_CHARACTER);
numMissedGuesses = 0;
}
bool Game::guesserHasWon()
{
return wordSoFar.find(FILL_CHARACTER) == string::npos;
}
bool Game::guesserHasLost()
{
return numMissedGuesses >= MAX_MISTAKE_LIMIT;
}
void Game::guessHasBeenMade (char guess)
{
bool isInWord;
Provider::getResponseToGuess(guess, isInWord, wordSoFar);
if (isInWord)
{
characterIsInWord (guess, wordSoFar);
}
else
{
++numMissedGuesses;
characterIsNotInWord (guess);
}
}
game.h
#ifndef GAME_H
#define GAME_H
#include <string>
class Provider;
class Guesser;
class Game{
const char FILL_CHARACTER;
std::string wordSoFar;
int numMissedGuesses;
const int MAX_MISTAKE_LIMIT;
void setUpGame (int wordLength);
bool guesserHasWon();
bool guesserHasLost();
void guessHasBeenMade (char guess);
};
#endif
provider.h
#ifndef PROVIDER_H
#define PROVIDER_H
#include <string>
class Provider {
public:
int initialPrompt ();
void getResponseToGuess (char guess, bool& isInWord,
std::string& wordSoFar,
int numMissedGuesses);
std::string providerHasWon ();
void providerHasLost (std::string wordSoFar);
private:
};
#endif
provider.cpp
#include "provider.h"
#include "game.h"
#include <iostream>
using namespace std;
int Provider::initialPrompt ()
{
cout << "Let's play Hangman!\n\n"
<< "Please think of a word from 4-9 characters long. The word\n"
<< "should not be a proper name (something that you would normally\n"
<< "capitalize, nor should it contain any punctuation characters.\n"
<< "\n\nOK, got a word?\n" << endl;
int len = 1;
while (len < 4 || len > 9)
{
cout << "How long is your word? " << flush;
cin >> len;
if (len < 4 || len > 9)
{
cout << "Please choose a word between 4 and 9 characters long."
<< endl;
}
}
return len;
}
bool getYesNoResponse()
{
string response;
getline (cin, response);
while (response.size() == 0 ||
(response[0] != 'y' && response[0] != 'Y'
&& response[0] != 'n' && response[0] != 'N'))
{
if (response.size() > 0)
cout << "Please respond 'yes' or 'no'. " << flush;
getline (cin, response);
}
return response[0] == 'y' || response[0] == 'Y';
}
void Provider::getResponseToGuess (char guess, bool& isInWord,
std::string& wordSoFar,
int numMissedGuesses)
{
cout << "I have missed " << numMissedGuesses << " guesses ("
<< Game::MAX_MISTAKE_LIMIT - numMissedGuesses << " misses left)"
<< endl;
cout << "\n" << wordSoFar << "\n";
for (int i = 1; i <= wordSoFar.size(); ++i)
cout << i;
cout << "\n\nDoes your word contain the letter '"
<< guess << "'? (y/n) " << flush;
isInWord = getYesNoResponse();
if (isInWord) {
string response;
bool done = false;
string newWord;
while (!done)
{
cout << "Enter all of the character positions (1-"
<< wordSoFar.size() << ") in which the letter '"
<< guess << "' appears: " << flush;
getline (cin, response);
bool digitsFound = false;
newWord = wordSoFar;
for (int i = 0; i < response.size(); ++i)
{
char d = response[i];
if (d >= '1' && d <= '0' + wordSoFar.size())
{
int k = d - '1';
if (wordSoFar[k] == Game::FILL_CHARACTER)
{
newWord[k] = guess;
digitsFound = true;
}
}
}
if (digitsFound)
{
cout << "Like this: " << newWord << "? (y/n) " << flush;
bool yn = getYesNoResponse();
if (yn)
{
wordSoFar = newWord;
done = true;
}
}
}
}
}
/**
* Announce that the provider has won the game, and get the
* provider's actual word.
*/
std::string Provider::providerHasWon ()
{
cout << "Congratulations, you have won." << endl;
cout << "\nOut of curiosity, what was your word? " << flush;
string answer;
getline (cin, answer);
return answer;
}
/**
* Announce that the guesser has won the game, and get the
* provider's actual word.
*/
void Provider::providerHasLost (string wordSoFar)
{
cout << wordSoFar
<< "\n\nI have won!\nThanks for playing" << endl;
}
My problem is the line
Provider::getResponseToGuess(guess, isInWord, wordSoFar);
It was originally just
getResponseToGuess(guess, isInWord, wordSoFar);
but I've tried several variations. I've tried Provider::Provider and Provider.getReponseToGuess, and a bunch of others, but nothing seems to work. Can anyone help me with this? I'm just trying to call the function from the provider files.
EDIT: I have the same problem calling a function from the guesser files, I tried doing it the same way I did with provider, but it gives me the error "no matching function for call to Guesser::Guesser();
Provider providerobj;
Guesser guesserobj;
providerobj.getResponseToGuess(guess, isInWord, wordSoFar, numMissedGuesses);
if (isInWord)
{
guesserobj.characterIsInWord(guess, wordSoFar);
}
I tried doing it the same way as I did with providerobj, but it won't work for some reason.
Here's the code for guesser.
guesser.cpp
#include "guesser.h"
#include "game.h"
#include <iostream>
#include <fstream>
using namespace std;
const std::string Guesser::alphabet = "abcdefghijklmnopqrstuvwxyz";
// Initialize the guesser for a game wit hthe indicated wordlength,
// using words from an indicated file.
Guesser::Guesser (int wordLength, const char* wordListFilename)
{
for (int i = 0; i < 26; ++i)
charactersTried[i] = false;
string word;
ifstream in (wordListFilename);
while (in >> word)
{
if (word.size() == wordLength)
{
// word is of desired length
if (word.find_first_not_of(alphabet) == string::npos) {
// word contains only lowercse alphabetics
possibleSolutions.push_back (word);
}
}
}
in.close();
}
/**
* Scan the words that are possible solutions so far, counting, for
* each letter not already tried, the number of words with that letter.
* Guess the letter that occurs in the most words.
*/
char Guesser::guessACharacter()
{
int counts[26];
for (int i = 0; i < 26; ++i)
counts[i] = 0;
// Count the number of words in which each letter can be found
for (int i = 0; i < possibleSolutions.size(); ++i)
{
string word = possibleSolutions[i];
for (char c = 'a'; c <= 'z'; ++c)
{
if (!charactersTried[c- 'a'])
{
// Character c has not been tried yet
if (word.find(c) != string::npos)
// c is in this word
++counts[c - 'a'];
}
}
}
// Find the character that occurs in the most words
char guess = ' ';
int maxSoFar = -1;
for (char c = 'a'; c <= 'z'; ++c)
{
if (counts[c - 'a'] > maxSoFar)
{
guess = c;
maxSoFar = counts[c - 'a'];
}
}
if (maxSoFar <= 0)
{
guess = 'a';
while (charactersTried[guess-'a'])
++guess;
}
charactersTried[guess-'a'] = true;
return guess;
}
/**
* Following a successful guess of a letter in the word, make a pass through
* the possibleSolutions, removing all words that do not contain the
* guess character in the positions indicated in wordSoFar.
*/
void Guesser::characterIsInWord (char guess, const string& wordSoFar)
{
vector<string> remainingSolutions;
for (int i = 0; i < possibleSolutions.size(); ++i)
{
string wd = possibleSolutions[i];
bool OK = true;
for (int k = 0; OK && k < wordSoFar.size(); ++k)
{
if (wordSoFar[k] == guess)
{
if (wd[k] != guess)
{
OK = false;
}
}
}
if (OK)
{
//cerr << "Keeping " << wd << endl;
remainingSolutions.push_back (wd);
}
}
possibleSolutions = remainingSolutions;
}
/**
* Following a mistaken guess of a letter in the word, make a pass through
* the possibleSolutions, removing all words that contain the
* guess character.
*/
void Guesser::characterIsNotInWord (char guess)
{
vector<string> remainingSolutions;
for (int i = 0; i < possibleSolutions.size(); ++i)
{
string wd = possibleSolutions[i];
if (wd.find(guess) == string::npos)
{
remainingSolutions.push_back (wd);
}
}
possibleSolutions = remainingSolutions;
}
/**
* Guesser has lost the game. Look at the provider's actual word
* and gripe a bit about losing.
*/
void Guesser::admitToLoss (std::string actualWord, const string& wordSoFar)
{
bool match = actualWord.size() == wordSoFar.size();
for (int i = 0; match && i < actualWord.size(); ++i)
{
match = wordSoFar[i] == Game::FILL_CHARACTER ||
wordSoFar[i] == actualWord[i];
}
if (!match)
{
cout << "Ummm...your word '" << actualWord
<< "' does not match the patterh '"
<< wordSoFar <<"'.\nDid you make a mistake somewhere?"
<< endl;
}
else
{
for (int i = 0; match && i < actualWord.size(); ++i)
{
if (wordSoFar[i] == Game::FILL_CHARACTER
&& charactersTried[actualWord[i]-'a'])
{
cout << "Did you forget to mention the '"
<< actualWord[i]
<< "' in position " << i+1 << "?"
<< endl;
return;
}
}
for (int i = 0; (!match) && i < possibleSolutions.size(); ++i)
match = (actualWord == possibleSolutions[i]);
match = match && (possibleSolutions.size() > 0);
if (match)
{
cout << "OK, I might have guessed that eventually." << endl;
}
else
{
cout << "Interesting, I don't know that word. Are you sure you\n"
<< "spelled it correctly?." << endl;
}
}
}
guesser.h
#ifndef GUESSER_H
#define GUESSER_H
#include <string>
#include <vector>
class Game;
class Guesser {
public:
// Initialize the guesser for a game with the indicated wordlength,
// using words from an indicated file.
Guesser (int wordLength, const char* wordListFilename);
/**
* Scan the words that are possible solutions so far, counting, for
* each letter not already tried, the number of words with that letter.
* Guess the letter that occurs in the most words.
*/
char guessACharacter();
/**
* Following a successful guess of a letter in the word, make a pass through
* the possibleSolutions, removing all words that do not contain the
* guess character in the positions indicated in wordSoFar.
*/
void characterIsInWord (char guess, const std::string& wordSoFar);
/**
* Following a mistaken guess of a letter in the word, make a pass through
* the possibleSolutions, removing all words that contain the
* guess character.
*/
void characterIsNotInWord (char guess);
/**
* Guesser has lost the game. Look at the provider's actual word
* and gripe a bit about losing.
*/
void admitToLoss (std::string actualWord, const std::string& wordSoFar);
private:
// A collection of words that match all guesses made so far
std::vector<std::string> possibleSolutions;
// Tracks characters already guessed.
// charactersTried[c-'a'] is true if the character c
// has been guessed previously
bool charactersTried[26];
static const std::string alphabet;
};
#endif
Provider::getResponseToGuess(guess, isInWord, wordSoFar); will not work because, getResponseToGuess is not a static function.
Provider.getReponseToGuess also wrong because Provider is a class, not an object, you can't call a member function of a class with class name.
you have to create a object of the class first, then call the member function with the object.
Provider provider_obj;
proveder_obj.getResponseToGuess(guess, isInWord, wordSoFar);
if you are new to C++, I would suggest first try some basic programs.
Related
I am creating a program that uses recursion functions to count the vowels in a sentence and to determine if it is a palindrome. The problem I am getting is that it says the sentence entered is not palindrome even if it is.. Any help with this will be greatly appreciated. Thank you.
#include<iostream>
#include <cmath>
using namespace std;
struct Sentence
{
int CountVowels(string , int);
public:
Sentence (string);
bool isPal(string , int);
void Print();
string s;
int numVowel;
int length;
//~Sentence();
};
Sentence :: Sentence (string b)
{
s = b;
length = 0;
numVowel = CountVowels(s, 0);
}
int Sentence :: CountVowels(string myWord, int startindex)
{
length ++;
int pandi;
if(myWord[startindex])
{
if (myWord[startindex] != 'a' && myWord[startindex] != 'e' && myWord[startindex] != 'i' && myWord[startindex] != 'o' && myWord[startindex] != 'u')
{
pandi = 0;
}
else pandi = 1;
return pandi + CountVowels(myWord, startindex + 1);
}
return 0;
}
bool Sentence :: isPal(string myWord, int size)
{
int r = myWord.size() - size;
int t = size - 1;
if (size == r || r == t)
return true;
if ((myWord[r]) != (myWord[t]))
return false;
return isPal(myWord, -- size);
}
void Sentence :: Print()
{
cout << s [-- length];
if (length == 0)
{
cout << endl;
return;
}
Print ();
}
/*Sentence :: ~Sentence()
{
cout << "\ntilde delete\n\n";
}*/
int main ()
{
string userW;
cout << "Enter a sentence: \n";
getline(cin, userW);
userW.erase(remove_if(userW.begin(), userW.end(), [](char c) {return !isalpha(c); }), userW.end());
Sentence userSent(userW);
cout << "The number of vowels in the sentence is " << userSent.numVowel << endl;
cout << "" << endl;
cout << "The sentence " << userSent.s << " is" <<
(userSent.isPal(userSent.s, userSent.s.size()) ? " Palindrome\n" : " Not Palindrome\n");
return 0;
}
UPDATE:
I am now trying to remove special characters. So it looks like this
string userW;
cout << "Enter a sentence: \n";
getline(cin, userW);
userW.erase(remove_if(userW.begin(), userW.end(), [](char c) {return !isalpha(c); }), userW.end());
But I am getting this error:
In function 'int main()':
88:85: error: 'remove_if' was not declared in this scope
I have reviewed your program. You are trying to out the string in the function pirnt(). Here is the problem ,when you use
cout << "The sentence backwards is: " << userSent.Print();
but funtion Print() does not have any return type.(because this is void type). Here you should use
cout << "The sentence backwards is: " ;
userSent.Print();
and now it works.
Your code is fine except that it does not remove commas, spaces and anything that is not alphabetic from the sentence. Also, you need to do a case-insensitive comparison on the characters. This is required, otherwise the examples
A man, a plan, a canal, Panama
Desserts, I stressed
would not be palidromes.
To remove special characters from the user input, you can use lambda
string userW; cout << "Enter a sentence: \n"; getline(cin, userW);
userW.erase(remove_if(userW.begin(), userW.end(), [](char c) {return !isalpha(c); }), userW.end());
EDIT you can also try the following to avoid the need for lambda:
userW.erase(std::copy_if(userW.begin(), userW.end(), userW.begin(), isalpha), userW.end());
to do a case-insensitive comparison, in the function isPal, you can change this:
if ((myWord[r]) != (myWord[t]))
into this:
if (tolower(myWord[r]) != tolower(myWord[t]))
So I'm working on this homework assignment and I'm really having trouble. I'm supposed to count the number of words more than two characters(have to contain one letter), unique words, and the number of times each unique word appears in the Programming Execution Environment. I'm also supposed to get input to search for in the PEE and output the number of times it appears and the line where it appears. I have some of it working, but I'm really struggling with counting how many times each word appears. I know my code is really bad right now, but that's why I'm here. I'm really struggling with these string functions for some reason. Any help is really appreciated!
#include <iostream>
#include <cstring>
#include <string>
#include <cctype>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
//PEE string
string envstr("");
bool checkChar(unsigned c)
{
return (ispunct(c) || isspace(c) || isblank(c) || isdigit(c) || c == '\n');
}
void searchWord(unsigned c, size_t length)
{
multiset<string> words;
vector<string> vwrds; //this was something i was trying out
string tempword;
while (!checkChar(envstr[c]) && c < length)
{
tempword = tempword + envstr[c]; //problem here
c++;
}
tempword = tempword + " ";
vwrds.push_back(tempword);
words.insert(tempword); //this is just a bunch of random letters
tempword.clear();
//for (multiset<string>::const_iterator i(words.begin()), end(words.end()); i != end; i++)
//cout << *i;
}
bool checkIfWord(char c)
{
bool valid = false;
int i;
for (i = c; i > c - 2; i--)
{
if (!checkChar(envstr[i]))
valid = true;
}
if (valid)
searchWord(i, envstr.length());
return valid;
}
int main()
{
//this code given by my instructor
extern char **environ; // needed to access your execution environment
int k = 0;
size_t wordCount = 0;
while (environ[k] != NULL)
{
cout << environ[k] << endl;
string str(environ[k]);
envstr = envstr + str;
k++;
}
//iterator to count words
wordCount = count_if(envstr.begin(), envstr.end(), checkIfWord);
cout << "\nThe PEE contains " << wordCount << " words. \n";
//transform environment string to lowercase
transform(envstr.begin(), envstr.end(), envstr.begin(), tolower);
string input;
do
{
cout << "Enter your search item: \n";
cin >> input;
//string can only be forty characters
if (input.length() > 40 || input == "\n")
{
cout << "That search query is too long. \n";
continue;
}
//change the search string to lowercase, like the envstr
transform(input.begin(), input.end(), input.begin(), tolower);
int j = 0;
int searchCount = 0;
vector<size_t> positions;
size_t pos = envstr.find(input, 0);
//search for that string
while (pos != string::npos)
{
positions.push_back(pos);
pos = envstr.find(input, pos + 1);
searchCount++;
}
cout << "\nThat phrase occurs a total of " << searchCount << " times.\n";
cout << "It occurs in the following lines: \n";
//output where that string occurs
for (vector<size_t>::iterator it = positions.begin(); it != positions.end(); ++it)
{
for (int i = *it; i < envstr.length() - 1 && checkChar(envstr[i]); i++)
{
cout << envstr[i];
}
cout << endl;
}
positions.clear();
} while (input != "END");
cin.get();
return 0;
}
First, your function checkChar() returns false when the parameter is a char, so if you want to print where that string occurs, it should be:
for (int i = *it; (i < envstr.length() - 1) && !checkChar(envstr[i]); i++)
{
cout << envstr[i];
}
Second, the code for counting words makes no sense and there is a potential out-of-bounds here: if (!checkChar(envstr[i])), I would suggest you to split the string using delimter '\', then do something.
My program prompts a user for a phrase to check if its a palindrome, then it's supposed to print out the phrase without capitalization or special characters like " ' , ? etc. My problem is erasing those characters. I've gotten my program to ignore them I'm asking how should I erase them? I made a comment where I think the statement should go. Example output should be: "Madam I'm Adam" to "madamimadam"
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
//Variables and arrays
int const index = 80;
char Phrase[index];
char NewPhrase[index];
int i, j, k, l;
bool test = true;
//Prompt user for the phrase/word
cout << "Please enter a sentence to be tested as a palindrome: ";
cin.getline(Phrase, 80);
//Make everything lowercase, delete spaces, and copy that to a new array 'NewPhrase'
for(k = 0, l = 0; k <= strlen(Phrase); k++)
{
if(Phrase[k] != ' ')
{
NewPhrase[l] = tolower(Phrase[k]);
l++;
}
}
//cout << "The Phrase without punctuation/extra characters: " << newPhrase[l];
int length = strlen(NewPhrase); //Get the length of the phrase
for(i = 0, j = length-1; i < j; i++, j--)
{
if(test) //Test to see if the phrase is a palindrome
{
if(NewPhrase[i] == NewPhrase[j])
{;}
else
{
test = false;
}
}
else
break;
}
if(test)
{
cout << endl << "Phrase/Word is a Palindrome." << endl << endl;
cout << "The Palindrome is: " << NewPhrase << endl << endl;
}
else
cout << endl << "Phrase/Word is not a Palindrome." << endl << endl;
system("Pause");
return 0;
}
Modify this line:
if(Phrase[k] != ' ')
To be:
if((phrase[k] != ' ') && (ispunct(phrase[k]) == false))
This means that we check for spaces and punctuation at the same time.
Also, consider rewriting this:
if(NewPhrase[i] == NewPhrase[j])
{;}
else
{
test = false;
}
As this:
if(NewPhrase[i] != NewPhrase[j])
test = false;
Here's suggestion:
Use an std::string
Use std::ispunct to determine whether a character in the string is a punctuation mark
Use the erase-remove idiom to remove punctuation
That is one line of code (plus one extra line for a convenience lambda):
std::string phrase = .....;
auto isPunct = [](char c) { return std::ispunct(static_cast<unsigned char>(c)); }
phrase.erase(std::remove_if(phrase.begin(), phrase.end(), isPunct),
phrase.end());
Next, for turning into lower case, from my answer to this recent question, another one-liner:
std::transform(phrase.begin(), phrase.end(), phrase.begin(),
[](char c)
{ return std::tolower(static_cast<unsigned char>(c));});
I am working on a project where I will be able to read in a file that contains any text, like the sample text below. Then, character by character, it will be able to output n-character long sequences (represented below as a read-in value given by the user along the lines of 1, 2, 3, 4...) along the whole length of the text. So, for example:
As Gregor Samsa awoke one morning from uneasy dreams he found himself transformed in his bed into a gigantic insect.
If the user provided 2 as the sequence length, the program should spit out: "As" "s " " G" "Gr" "re" "eg" "go" "or" "r " and so on...
I have written this code but don't know why it won't work. Right now, it doesn't spit out every possible variation of the sequence. Any suggestions would be very helpful. Thanks.
#include "genlib.h"
#include <iostream>
#include <fstream>
#include "simpio.h"
#include "random.h"
#include "vector.h"
#include "map.h"
/* Private Instance Variables */
int seed_length;
string line;
string seed_string;
string next_string;
char ch;
/* Function Prototypes */
string promptUserForFile(ifstream & infile);
int main() {
ifstream infile;
promptUserForFile(infile);
// Ask what order of Markov model to use.
cout << "What order of Markov model should we use? ";
cin >> seed_length;
while (infile.eof() == false) {
ch = infile.get();
for (int i = 0; i < seed_length - 1; i++) {
cout << "ch up here is " << ch << endl;
if (isspace(ch) && i == 0) {
seed_string += ch;
} else {
seed_string += ch;
ch = infile.get();
}
}
next_string = ch;
if (isspace(ch)) {
next_string = " ";
} else {
char trythis = infile.get();
next_string += trythis;
}
cout << seed_string << endl;
cout << next_string << endl;
seed_string = "";
next_string = "";
}
cout << "TEST" << endl;
// Close the file when you're done storing all of the scores.
infile.close();
return 0;
}
string promptUserForFile(ifstream & infile) {
string prompt = "Please input your filename: ";
while(true) {
cout << prompt;
string filename;
getline (cin, filename);
infile.open(filename.c_str());
if(!infile.fail()) return filename;
infile.clear();
cout << "Unable to open that file. Try again." << endl;
if (prompt == "") prompt == "Input file: ";
}
return 0;
}
The code has two problems.
The special handling for isspace is broken:
if (isspace(ch) && i == 0) {
seed_string += ch;
} else {
seed_string += ch;
ch = infile.get();
}
This essentially means that if the first character in this loop is a space, it will be added twice.
Every character received from infile.get() is only added to seed_string once (with the exception of isspace characters).
A better way to code this is to recognize that:
You have to ignore consecutive isspace characters.
Every sequence can be obtained by removing the first character of the preceding sequnce and appending the next character from the file.
Here is a better implementation; it takes the order of the Markov model in the first command line parameter and takes the text from standard input. By encapsulating the skipping of duplicate spaces in a separate function, you don't have to deal with it in the main body of the algorithm.
#include <iostream>
#include <cstdlib>
char next_character() {
static bool was_space = false;
char ret = 0;
do {
ret = std::cin.get();
} while (was_space && std::isspace(ret));
if (std::isspace(ret)) {
was_space = true;
ret = ' ';
} else {
was_space = false;
}
return ret;
}
int main(int argc, char **argv) {
if (argc != 2) return 0;
int mlen = std::atoi(argv[1]);
std::string seq;
for (unsigned i = 0; i < mlen; ++i) {
seq += next_character();
}
std::cout << seq << '\n';
while (true) {
seq.erase(0, 1);
char c = next_character();
if (std::cin.eof()) break;
seq += c;
std::cout << seq << '\n';
}
return 0;
}
Example input:
This is a test
Example output:
This
his i
is is
s is
is a
is a
s a t
a te
a tes
test
malloc: *** error for object 0x10ee008c0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
Or I get this when I try and print everything
Segmentation fault: 11
I'm doing some homework for an OOP class and I've been stuck for a good hour now. I'm getting this error once I've used keyboard input enough. I am not someone who gets frustrated at all, and here I am getting very frustrated with this. Here are the files:
This is the book class. I'm pretty sure this is very solid. But just for reference:
//--------------- BOOK.CPP ---------------
// The class definition for Book.
//
#include <iostream>
#include "book.h"
using namespace std;
Book::Book()
//
{
strcpy(title, " ");
strcpy(author, " ");
type = FICTION;
price = 0;
}
void Book::Set(const char* t, const char* a, Genre g, double p)
{
strcpy(title, t);
strcpy(author, a);
type = g;
price = p;
}
const char* Book::GetTitle() const
{
return title;
}
const char* Book::GetAuthor() const
{
return author;
}
double Book::GetPrice() const
{
return price;
}
Genre Book::GetGenre() const
{
return type;
}
void Book::Display() const
{
int i;
cout << GetTitle();
for (i = strlen(title) + 1; i < 32; i++)
cout << (' ');
cout << GetAuthor();
for (i = strlen(author) + 1; i < 22; i++)
cout << (' ');
switch (GetGenre())
{
case FICTION:
cout << "Fiction ";
break;
case MYSTERY:
cout << "Mystery ";
break;
case SCIFI:
cout << "SciFi ";
break;
case COMPUTER:
cout << "Computer ";
break;
}
cout << "$";
if (GetPrice() < 1000)
cout << " ";
if (GetPrice() < 100)
cout << " ";
if (GetPrice() < 10)
cout << " ";
/* printf("%.2f", GetPrice());*/
cout << '\n';
}
This is the store class that deals with the array and dynamic allocation. This was working well without input commands, but just using its functions it was working like a champ.
//--------------- STORE.CPP ---------------
// The class definition for Store.
//
#include <iostream>
#include <cstring> // for strcmp
#include "store.h"
using namespace std;
Store::Store()
{
maxSize = 5;
currentSize = 0;
bookList = new Book[maxSize];
}
Store::~Store()
// This destructor function for class Store
// deallocates the Store's list of Books
{
delete [] bookList;
}
void Store::Insert(const char* t, const char* a, Genre g, double p)
// Insert a new entry into the direrctory.
{
if (currentSize == maxSize)// If the directory is full, grow it.
Grow();
bookList[currentSize++].Set(t, a, g, p);
}
void Store::Sell(const char* t)
// Sell a book from the store.
{
char name[31];
strcpy(name, t);
int thisEntry = FindBook(name);// Locate the name in the directory.
if (thisEntry == -1)
cout << *name << " not found in directory";
else
{
cashRegister = cashRegister + bookList[thisEntry].GetPrice();
// Shift each succeding element "down" one position in the
// Entry array, thereby deleting the desired entry.
for (int j = thisEntry + 1; j < currentSize; j++)
bookList[j - 1] = bookList[j];
currentSize--;// Decrement the current number of entries.
cout << "Entry removed.\n";
if (currentSize < maxSize - 5)// If the directory is too big, shrink it.
Shrink();
}
}
void Store::Find(const char* x) const
// Display the Store's matches for a title or author.
{
// Prompt the user for a name to be looked up
char name[31];
strcpy(name, x);
int thisBook = FindBook(name);
if (thisBook != -1)
bookList[thisBook].Display();
int thisAuthor = FindAuthor(name, true);
if ((thisBook == -1) && (thisAuthor == -1))
cout << name << " not found in current directory\n";
}
void Store::DisplayGenre(const Genre g) const
{
double genrePrice = 0;
int genreCount = 0;
for (int i = 0; i < currentSize; i++)// Look at each entry.
{
if (bookList[i].GetGenre() == g)
{
bookList[i].Display();
genrePrice = genrePrice + bookList[i].GetPrice();
genreCount++;
}
}
cout << "Number of books in this genre: " << genreCount
<< " " << "Total: $";
if (genrePrice < 1000)
cout << " ";
if (genrePrice < 100)
cout << " ";
if (genrePrice < 10)
cout << " ";
printf("%.2f", genrePrice);
}
void Store::DisplayStore() const
{
if (currentSize >= 1)
{
cout << "**Title**\t\t"
<< "**Author**\t"
<< "**Genre**\t"
<< "**Price**\n\n";
for (int i = 0; i < currentSize; i++)
bookList[i].Display();
}
else
cout << "No books currently in inventory\n\n";
cout << "Total Books = " << currentSize
<< "\nMoney in the register = $";
if (cashRegister < 1000)
cout << " ";
if (cashRegister < 100)
cout << " ";
if (cashRegister < 10)
cout << " ";
printf("%.2f", cashRegister);
cout << '\n';
}
void Store::Sort(char type)
{
Book temp;
for(int i = 0; i <= currentSize; i++)
{
for (int j = i+1; j < currentSize; j++)
{
if (type == 'A')
{
if (strcmp(bookList[i].GetTitle(), bookList[j].GetTitle()) > 0)
{
temp = bookList[i];
bookList[i] = bookList[j];
bookList[j] = temp;
}
}
if (type == 'T')
{
if (strcmp(bookList[i].GetAuthor(), bookList[j].GetAuthor()) > 0)
{
temp = bookList[i];
bookList[i] = bookList[j];
bookList[j] = temp;
}
}
}
}
}
void Store::SetCashRegister(double x)
// Set value of cash register
{
cashRegister = x;
}
void Store::Grow()
// Double the size of the Store's bookList
// by creating a new, larger array of books
// and changing the store's pointer to refer to
// this new array.
{
maxSize = currentSize + 5;// Determine a new size.
cout << "** Array being resized to " << maxSize
<< " allocated slots" << '\n';
Book* newList = new Book[maxSize];// Allocate a new array.
for (int i = 0; i < currentSize; i++)// Copy each entry into
newList[i] = bookList[i];// the new array.
delete [] bookList;// Remove the old array
bookList = newList;// Point old name to new array.
}
void Store::Shrink()
// Divide the size of the Store's bookList in
// half by creating a new, smaller array of books
// and changing the store's pointer to refer to
// this new array.
{
maxSize = maxSize - 5;// Determine a new size.
cout << "** Array being resized to " << maxSize
<< " allocated slots" << '\n';
Book* newList = new Book[maxSize];// Allocate a new array.
for (int i = 0; i < currentSize; i++)// Copy each entry into
newList[i] = bookList[i];// the new array.
delete [] bookList;// Remove the old array
bookList = newList;// Point old name to new array.
}
int Store::FindBook(char* name) const
// Locate a name in the directory. Returns the
// position of the entry list as an integer if found.
// and returns -1 if the entry is not found in the directory.
{
for (int i = 0; i < currentSize; i++)// Look at each entry.
if (strcmp(bookList[i].GetTitle(), name) == 0)
return i;// If found, return position and exit.
return -1;// Return -1 if never found.
}
int Store::FindAuthor(char* name, const bool print) const
// Locate a name in the directory. Returns the
// position of the entry list as an integer if found.
// and returns -1 if the entry is not found in the directory.
{
int returnValue;
for (int i = 0; i < currentSize; i++)// Look at each entry.
if (strcmp(bookList[i].GetAuthor(), name) == 0)
{
if (print == true)
bookList[i].Display();
returnValue = i;// If found, return position and exit.
}
else
returnValue = -1;// Return -1 if never found.
return returnValue;
}
Now this is the guy who needs some work. There may be some stuff blank so ignore that. This one controls all the input, which is the problem I believe.
#include <iostream>
#include "store.h"
using namespace std;
void ShowMenu()
// Display the main program menu.
{
cout << "\n\t\t*** BOOKSTORE MENU ***";
cout << "\n\tA \tAdd a Book to Inventory";
cout << "\n\tF \tFind a book from Inventory";
cout << "\n\tS \tSell a book";
cout << "\n\tD \tDisplay the inventory list";
cout << "\n\tG \tGenre summary";
cout << "\n\tO \tSort inventory list";
cout << "\n\tM \tShow this Menu";
cout << "\n\tX \teXit Program";
}
char GetAChar(const char* promptString)
// Prompt the user and get a single character,
// discarding the Return character.
// Used in GetCommand.
{
char response;// the char to be returned
cout << promptString;// Prompt the user
cin >> response;// Get a char,
response = toupper(response);// and convert it to uppercase
cin.get();// Discard newline char from input.
return response;
}
char Legal(char c)
// Determine if a particular character, c, corresponds
// to a legal menu command. Returns 1 if legal, 0 if not.
// Used in GetCommand.
{
return((c == 'A') || (c == 'F') || (c == 'S') ||
(c == 'D') || (c == 'G') || (c == 'O') ||
(c == 'M') || (c == 'X'));
}
char GetCommand()
// Prompts the user for a menu command until a legal
// command character is entered. Return the command character.
// Calls GetAChar, Legal, ShowMenu.
{
char cmd = GetAChar("\n\n>");// Get a command character.
while (!Legal(cmd))// As long as it's not a legal command,
{// display menu and try again.
cout << "\nIllegal command, please try again . . .";
ShowMenu();
cmd = GetAChar("\n\n>");
}
return cmd;
}
void Add(Store s)
{
char aTitle[31];
char aAuthor[21];
Genre aGenre = FICTION;
double aPrice = 10.00;
cout << "Enter title: ";
cin.getline(aTitle, 30);
cout << "Enter author: ";
cin.getline(aAuthor, 20);
/*
cout << aTitle << " " << aAuthor << "\n";
cout << aGenre << " " << aPrice << '\n';
*/
s.Insert(aTitle, aAuthor, aGenre, aPrice);
}
void Find()
{
}
void Sell()
{
}
void ViewGenre(Store s)
{
char c;
Genre result;
do
c = GetAChar("Enter Genre - (F)iction, (M)ystery, (S)ci-Fi, or (C)omputer: ");
while ((c != 'F') && (c != 'M') && (c != 'S') && (c != 'C'));
switch (result)
{
case 'F': s.DisplayGenre(FICTION); break;
case 'M': s.DisplayGenre(MYSTERY); break;
case 'S': s.DisplayGenre(SCIFI); break;
case 'C': s.DisplayGenre(COMPUTER); break;
}
}
void Sort(Store s)
{
char c;
Genre result;
do
c = GetAChar("Enter Genre - (F)iction, (M)ystery, (S)ci-Fi, or (C)omputer: ");
while ((c != 'A') && (c != 'T'));
s.Sort(c);
}
void Intro(Store s)
{
double amount;
cout << "*** Welcome to Bookstore Inventory Manager ***\n"
<< "Please input the starting money in the cash register: ";
/* cin >> amount;
s.SetCashRegister(amount);*/
}
int main()
{
Store mainStore;// Create and initialize a Store.
Intro(mainStore);//Display intro & set Cash Regsiter
ShowMenu();// Display the menu.
/*mainStore.Insert("A Clockwork Orange", "Anthony Burgess", SCIFI, 30.25);
mainStore.Insert("X-Factor", "Anthony Burgess", SCIFI, 30.25);*/
char command;// menu command entered by user
do
{
command = GetCommand();// Retrieve a command.
switch (command)
{
case 'A': Add(mainStore); break;
case 'F': Find(); break;
case 'S': Sell(); break;
case 'D': mainStore.DisplayStore(); break;
case 'G': ViewGenre(mainStore); break;
case 'O': Sort(mainStore); break;
case 'M': ShowMenu(); break;
case 'X': break;
}
} while ((command != 'X'));
return 0;
}
Please, any and all help you can offer is amazing.
Thank you.
Welcome to the exciting world of C++!
Short answer: you're passing Store as a value. All your menu functions should take a Store& or Store* instead.
When you're passing Store as a value then an implicit copy is done (so the mainStore variable is never actually modified). When you return from the function the Store::~Store is called to clean up the copied data. This frees mainStore.bookList without changing the actual pointer value.
Further menu manipulation will corrupt memory and do many double frees.
HINT: If you're on linux you can run your program under valgrind and it will point out most simple memory errors.
Your Store contains dynamically-allocated data, but does not have an assignment operator. You have violated the Rule of Three.
I don't see the Store class being instantiated anywhere by a call to new Store() which means the booklist array has not been created but when the program exits and calls the destructor, it tries to remove the array that was never allocated and hence that's why i think you are getting this error. Either, modify the destructor to have a null check or instantiate the class by a call to the constructor. Your code shouldn't still be working anywhere you are trying to use a Store object.
Hope this helps