A tuple is a comma-separated list enclosed by parens, e.g.
()
(,)
(thing,)
(2,3)
If I have
#define ISTUPLE(x) \\...
I'd like something like ISTUPLE(nope) to resolve to 0 and ISTUPLE((yep)) to resolve to 1.
[FWIW, I have _RTFM_'d a plenty.]
It could likely be done in the Preprocessing library with a little work, but the Variadic Macro Data Library (added to Boost since this question was posted) has a ready-made solution. BOOST_VMD_IS_TUPLE, defined in boost/vmd/is_tuple.hpp, should do what you're looking for:
#include <iostream>
#include <boost/vmd/is_tuple.hpp>
#if BOOST_VMD_IS_TUPLE() != 0
#error BOOST_VMD_IS_TUPLE() != 0
#endif
#if BOOST_VMD_IS_TUPLE(nope) != 0
#error BOOST_VMD_IS_TUPLE(nope) != 0
#endif
#if BOOST_VMD_IS_TUPLE((yep)) != 1
#error BOOST_VMD_IS_TUPLE((yep)) != 1
#endif
#if BOOST_VMD_IS_TUPLE(()) != 1
#error BOOST_VMD_IS_TUPLE(()) != 1
#endif
#if BOOST_VMD_IS_TUPLE((,)) != 1
#error BOOST_VMD_IS_TUPLE((,)) != 1
#endif
#if BOOST_VMD_IS_TUPLE((thing,)) != 1
#error BOOST_VMD_IS_TUPLE((thing,)) != 1
#endif
#if BOOST_VMD_IS_TUPLE((2,3)) != 1
#error BOOST_VMD_IS_TUPLE((2,3)) != 1
#endif
static_assert(BOOST_VMD_IS_TUPLE() == 0,"BOOST_VMD_IS_TUPLE() != 0");
static_assert(BOOST_VMD_IS_TUPLE(nope) == 0,"BOOST_VMD_IS_TUPLE(nope) != 0");
static_assert(BOOST_VMD_IS_TUPLE((yep)) == 1,"BOOST_VMD_IS_TUPLE((yep)) != 1");
static_assert(BOOST_VMD_IS_TUPLE(()) == 1,"BOOST_VMD_IS_TUPLE(()) != 1");
static_assert(BOOST_VMD_IS_TUPLE((,)) == 1,"BOOST_VMD_IS_TUPLE((,)) != 1");
static_assert(BOOST_VMD_IS_TUPLE((thing,)) == 1,"BOOST_VMD_IS_TUPLE((thing,)) != 1");
static_assert(BOOST_VMD_IS_TUPLE((2,3)) == 1,"BOOST_VMD_IS_TUPLE((2,3)) != 1");
int main(void)
{
using std::cout;
using std::endl;
cout << "BOOST_VMD_IS_TUPLE() == " << BOOST_VMD_IS_TUPLE() << endl;
cout << "BOOST_VMD_IS_TUPLE(nope) == " << BOOST_VMD_IS_TUPLE(nope) << endl;
cout << "BOOST_VMD_IS_TUPLE((yep)) == " << BOOST_VMD_IS_TUPLE((yep)) << endl;
cout << "BOOST_VMD_IS_TUPLE(()) == " << BOOST_VMD_IS_TUPLE(()) << endl;
cout << "BOOST_VMD_IS_TUPLE((,)) == " << BOOST_VMD_IS_TUPLE((,)) << endl;
cout << "BOOST_VMD_IS_TUPLE((thing,)) == " << BOOST_VMD_IS_TUPLE((thing,)) << endl;
cout << "BOOST_VMD_IS_TUPLE((2,3)) == " << BOOST_VMD_IS_TUPLE((2,3)) << endl;
return 0;
}
Output:
BOOST_VMD_IS_TUPLE() == 0
BOOST_VMD_IS_TUPLE(nope) == 0
BOOST_VMD_IS_TUPLE((yep)) == 1
BOOST_VMD_IS_TUPLE(()) == 1
BOOST_VMD_IS_TUPLE((,)) == 1
BOOST_VMD_IS_TUPLE((thing,)) == 1
BOOST_VMD_IS_TUPLE((2,3)) == 1
(http://coliru.stacked-crooked.com/a/6e41eaf17437c5d5)
Related
I'm attempting to decode frames, but memory usage grows with every frame (more specifically, with every call to avcodec_send_packet) until finally the code crashes with a bad_alloc. Here's the basic decode loop:
int rfret = 0;
while((rfret = av_read_frame(inctx.get(), &packet)) >= 0){
if (packet.stream_index == vstrm_idx) {
//std::cout << "Sending Packet" << std::endl;
int ret = avcodec_send_packet(ctx.get(), &packet);
if (ret < 0 || ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
std::cout << "avcodec_send_packet: " << ret << std::endl;
break;
}
while (ret >= 0) {
//std::cout << "Receiving Frame" << std::endl;
ret = avcodec_receive_frame(ctx.get(), fr);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
//std::cout << "avcodec_receive_frame: " << ret << std::endl;
av_frame_unref(fr);
// av_frame_free(&fr);
break;
}
std::cout << "frame: " << ctx->frame_number << std::endl;
// eventually do something with the frame here...
av_frame_unref(fr);
// av_frame_free(&fr);
}
}
else {
//std::cout << "Not Video" << std::endl;
}
av_packet_unref(&packet);
}
Memory usage/leakage seems to scale with the resolution of the video I'm decoding. For example, for a 3840x2160 resolution video, the memory usage in windows task manager consistently jumps up by about 8mb (1 byte per pixel??) for each received frame. Do I need to do something besides call av_frame_unref to release the memory?
(more) complete code below
void AVFormatContextDeleter(AVFormatContext* ptr)
{
if (ptr) {
avformat_close_input(&ptr);
}
}
void AVCodecContextDeleter(AVCodecContext* ptr)
{
if (ptr) {
avcodec_free_context(&ptr);
}
}
typedef std::unique_ptr<AVFormatContext, void (*)(AVFormatContext *)> AVFormatContextPtr;
typedef std::unique_ptr<AVCodecContext, void (*)(AVCodecContext *)> AVCodecContextPtr;
AVCodecContextPtr createAvCodecContext(AVCodec *vcodec)
{
AVCodecContextPtr ctx(avcodec_alloc_context3(vcodec), AVCodecContextDeleter);
return ctx;
}
AVFormatContextPtr createFormatContext(const std::string& filename)
{
AVFormatContext* inctxPtr = nullptr;
int ret = avformat_open_input(&inctxPtr, filename.c_str(), nullptr, nullptr);
// int ret = avformat_open_input(&inctx, "D:/Videos/test.mp4", nullptr, nullptr);
if (ret != 0) {
inctxPtr = nullptr;
}
return AVFormatContextPtr(inctxPtr, AVFormatContextDeleter);
}
int testDecode()
{
// open input file context
AVFormatContextPtr inctx = createFormatContext("D:/Videos/Matt Chapman Hi Greg.MOV");
if (!inctx) {
// std::cerr << "fail to avforamt_open_input(\"" << infile << "\"): ret=" << ret;
return 1;
}
// retrieve input stream information
int ret = avformat_find_stream_info(inctx.get(), nullptr);
if (ret < 0) {
//std::cerr << "fail to avformat_find_stream_info: ret=" << ret;
return 2;
}
// find primary video stream
AVCodec* vcodec = nullptr;
const int vstrm_idx = av_find_best_stream(inctx.get(), AVMEDIA_TYPE_VIDEO, -1, -1, &vcodec, 0);
if (vstrm_idx < 0) {
//std::cerr << "fail to av_find_best_stream: vstrm_idx=" << vstrm_idx;
return 3;
}
AVCodecParameters* origin_par = inctx->streams[vstrm_idx]->codecpar;
if (vcodec == nullptr) { // is this even necessary?
vcodec = avcodec_find_decoder(origin_par->codec_id);
if (!vcodec) {
// Can't find decoder
return 4;
}
}
AVCodecContextPtr ctx = createAvCodecContext(vcodec);
if (!ctx) {
return 5;
}
ret = avcodec_parameters_to_context(ctx.get(), origin_par);
if (ret) {
return 6;
}
ret = avcodec_open2(ctx.get(), vcodec, nullptr);
if (ret < 0) {
return 7;
}
//print input video stream informataion
std::cout
//<< "infile: " << infile << "\n"
<< "format: " << inctx->iformat->name << "\n"
<< "vcodec: " << vcodec->name << "\n"
<< "size: " << origin_par->width << 'x' << origin_par->height << "\n"
<< "fps: " << av_q2d(ctx->framerate) << " [fps]\n"
<< "length: " << av_rescale_q(inctx->duration, ctx->time_base, {1,1000}) / 1000. << " [sec]\n"
<< "pixfmt: " << av_get_pix_fmt_name(ctx->pix_fmt) << "\n"
<< "frame: " << inctx->streams[vstrm_idx]->nb_frames << "\n"
<< std::flush;
AVPacket packet;
av_init_packet(&packet);
packet.data = nullptr;
packet.size = 0;
AVFrame *fr = av_frame_alloc();
if (!fr) {
return 8;
}
int rfret = 0;
while((rfret = av_read_frame(inctx.get(), &packet)) >= 0){
if (packet.stream_index == vstrm_idx) {
//std::cout << "Sending Packet" << std::endl;
int ret = avcodec_send_packet(ctx.get(), &packet);
if (ret < 0 || ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
std::cout << "avcodec_send_packet: " << ret << std::endl;
break;
}
while (ret >= 0) {
//std::cout << "Receiving Frame" << std::endl;
ret = avcodec_receive_frame(ctx.get(), fr);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
//std::cout << "avcodec_receive_frame: " << ret << std::endl;
av_frame_unref(fr);
// av_frame_free(&fr);
break;
}
std::cout << "frame: " << ctx->frame_number << std::endl;
// do something with the frame here...
av_frame_unref(fr);
// av_frame_free(&fr);
}
}
else {
//std::cout << "Not Video" << std::endl;
}
av_packet_unref(&packet);
}
std::cout << "RFRET = " << rfret << std::endl;
return 0;
}
Update 1: (1/21/2019) Compiling on a different machine and running with different video files I am not seeing the memory usage growing without bound. I'll try to narrow down where the difference lies (compiler?, ffmpeg version?, or video encoding?)
Update 2: (1/21/2019) Ok, it looks like there is some interaction occurring between ffmpeg and Qt's QCamera. In my application, I'm using Qt to manage the webcam, but decided to use ffmpeg libraries to handle decoding/encoding since Qt doesn't have as comprehensive support for different codecs. If I have the camera turned on (through Qt), ffmpeg decoding memory consumption grows without bound. If the camera is off, ffmpeg behaves fine. I've tried this both with a physical camera (Logitech C920) and with a virtual camera using OBS-Virtualcam, with the same result. So far I'm baffled as to how the two systems are interacting...
I had same problem.
before use the av_frame_unref.
call av_freep(buffer->data[0]).
av_frame_unref was not release raw data in frame
example:
av_freep(&pFrame->data[0]);
av_frame_unref(pFrame);
//av_free(pFrame);
EDIT:
I am sorry that English is immature.
When you decode the video, you have the data for the image in the buffer.
It will remain as a NULL pointer until you release it and reallocate it, which means that you will need to allocate memory again at reallocation.
After you have finished using the image data, you should release the buffer.
Are you using it like that?
while (Framecheck = av_read_frame(pFormatCtx, &packet) == NULL ) {
if (d_end == true)
break;
if (packet.stream_index == VSI) {
if (bool res = avcodec_send_packet(pVideoCodecCtx, &packet)) {
printf("avcodec_send_packet failed %d %d %d\n", res, AVERROR(EINVAL), AVERROR(ENOMEM));
}
if (bool res = avcodec_receive_frame(pVideoCodecCtx, pVFrame) == 0) {
printf("avcodec_receive failed %d %d %d\n", res, AVERROR(EINVAL), AVERROR(ENOMEM));
}
if (pVFrame->data[0] == NULL && pVFrame->data[1] == NULL && pVFrame->data[2] == NULL)
continue;
else {
YUV_frame = Con_yuv_RGB(pVFrame);
QFrame->push(YUV_frame);
PushCount++;
}
}
Sleep(5);
}
if (Framecheck != true){
av_packet_unref(&packet);
d_end = true;
return true;
release:
if (FrameQueue->size()) {
while (FrameQueue->size() > 0) {
av_freep(&FrameQueue->front());
//av_frame_unref(FrameQueue->front());
av_free(FrameQueue->front());
FrameQueue->pop();
}
}
Try calling av_frame_free when you're done with the frame (outside your while loop)
And don't call av_frame_unref
See example here:
https://ffmpeg.org/doxygen/4.0/decode__video_8c_source.html
first of all, forgive my code for being ugly. The tons of ideas I've been given to try to fix this code have jumbled it up after all the potential solutions that haven't worked. Basically, I'm coding a Hearthstone rip-off that reads in two .txt files with card information and battles them to see which player wins. The issue is that when I'm trying to save the player's name (the first line in the files), it saves the whole file instead of just the first line. When I have managed to fix that, the for loop used to save the information for the card objects (format: card name, card power, card health) does not get saved properly for some reason. Any help would be appreciated, I've been trying to fix this for two days and nothing has fully solved the problem. I'll attach the read in files first before the code.
Disclaimer: It's a lot of lines and I'm sorry about that. Also I think the problem could be that my Mac is not saving the .txt in a format that has the right line endings. I'm using XCode as my IDE. Thank you so much to whomever is willing to help!
File1:
The Innkeeper
3
Tunnel Trogg
1
3
Neptulon
7
7
Fire Elemental
6
5
File2:
Malfurion
3
Leper Gnome
2
1
Aviana
5
5
Cenarius
5
8
Main:
#include "Player.h"
using namespace std;
int main()
{
cout << "Please enter file name of the first player: " << endl;
string inFile = "";
getline(cin, inFile);
Player* p1 = new Player(inFile);
cout << "Now enter the file name of the second player: " << endl;
getline(cin, inFile);
Player* p2 = new Player(inFile);
p1->battle(*p2);
delete p1;
delete p2;
return 0;
}
Player Header:
#include "Card.h"
#include <fstream>
#ifndef Player_h
#define Player_h
using namespace std;
class Player
{
private:
string playerName;
int numCards;
Card ** cards;
int wins = 0;
public:
Player(std::string inFile);
void battle(Player p2);
Card* getCard(int counter);
~Player();
};
#endif /* Player_h */
Card Header:
#include <string>
#include <iostream>
#ifndef Card_h
#define Card_h
using namespace std;
class Card
{
public:
Card();
string getName();
int getPower();
int getHealth();
void setName(string newName);
void setPower(int newPower);
void setHealth(int newHealth);
Card* duel(Card&);
friend ostream& operator<<(ostream& o, Card& c);
friend bool operator==(Card& p1Card, Card& p2Card);
private:
string name;
int power;
int health;
};
#endif /* Card_h */
Player Source:
#include "Player.h"
using namespace std;
Player::Player(string inFile)
{
ifstream in(inFile, ios::in);\
if (!in)
{
cerr << "There was a problem opening the file. Sorry, try again!" << endl;
return;
}
getline(in, playerName);
cout << playerName << endl;
in>>numCards;
playerName = "";
numCards = 0;
cards = new Card* [numCards];
string tempName = "";
int tempPower = 0;
int tempHealth = 0;
for (int i = 0; i<numCards; i++)
{
in.ignore();
cards[i] = new Card();
getline(in, tempName);
cout << "in for loop: " << endl;
cout << tempName << ",";
cards[i]->setName(tempName);
in >> tempPower;
in.ignore();
cout << tempPower << ",";
cards[i]->setPower(tempPower);
in >> tempHealth;
cout << tempHealth << " done"<< endl;
cards[i]->setHealth(tempHealth);
}
}
void Player::battle(Player p2)
{
int draws = 0;
cout << "Let the battle begin!" << endl;
cout << numCards << endl;
if (wins > p2.wins)
{
cout << playerName << " wins over " << p2.playerName << ", " << wins << " to " << p2.wins;
if (draws == 0)
{
cout << " and no ties." << endl;
}
else
{
cout << " and " << draws << " ties." << endl;
}
}
else if (p2.wins > wins)
{
cout << p2.playerName << " wins over " << playerName << ", " << p2.wins << " to " << wins;
if (draws == 0)
{
cout << " and no ties." << endl;
}
else
{
cout << " and " << draws << " ties." << endl;
}
}
else if (p2.wins == wins)
{
cout << "It is a draw between " << playerName << " and " << p2.playerName << ", with " << wins << " for each and ";
if (draws == 0)
{
cout << "no ties." << endl;
}
else
{
cout << draws << " ties." << endl;
}
}
cout << "Here are the detailed results:" << endl;
for (int i = 0; i < numCards; i++)
{
cout << *cards[i] << " vs. " << *p2.cards[i] << " - ";
if (*cards[i] == *p2.cards[i])
{
cout << "It is a draw." << endl;
}
else if (cards[i]->duel(*p2.cards[i]) == NULL)
{
cout << "It is a draw." << endl;
}
else if (*cards[i]->duel(*p2.cards[i]) == *p2.cards[i])
{
cout << p2.cards[i]->getName() << "wins for " << p2.playerName << "." << endl;
}
else if (*cards[i]->duel(*p2.cards[i]) == *cards[i])
{
cout << cards[i]->getName() << "wins for " << playerName << "." << endl;
}
}
}
Player::~Player()
{
if (cards != NULL)
{
for (int i = 0; i < numCards; i++)
{
if (cards[i] != nullptr)
{
delete cards[i];
cards[i] = NULL;
}
};
}
}
Card Source:
#include "Card.h"
using namespace std;
Card::Card()
{
name = "";
power = 0;
health = 0;
}
string Card::getName()
{
return name;
}
int Card::getPower()
{
return power;
}
int Card::getHealth()
{
return health;
}
void Card::setName(string newName)
{
name = newName;
}
void Card::setPower(int newPower)
{
power = newPower;
}
void Card::setHealth(int newHealth)
{
health = newHealth;
}
Card* Card::duel(Card& otherCard)
{
if ((otherCard.getHealth() - this->getPower() <=0) && (getHealth() - otherCard.getPower() <= 0))
{
return NULL;
}
else if ((otherCard.getHealth() - this->getPower() >0) && (getHealth() - otherCard.getPower() >0))
{
return NULL;
}
else if (otherCard.getHealth() - this->getPower() <=0)
{
return this;
}
else if (this->getHealth() - otherCard.getPower() <=0)
{
return &otherCard;
}
return NULL;
}
ostream& operator<<(ostream& o, Card& c)
{
o << c.getName() << " (" << c.power << ", " << c.health << ") " << endl;
return o;
}
bool operator==(Card& p1Card, Card& p2Card)
{
if (p1Card.health == p2Card.health &&
p1Card.power == p2Card.power &&
p1Card.name == p2Card.name)
{
return true;
}
else
{
return false;
}
}
Your code is almost right. It can read the Player's name and the card numbers, but your codes showed below:
in>>numCards;
playerName = "";
numCards = 0;
cards = new Card* [numCards];
at first, it read the num of card and store it to numCards, it is right.
next, you clear the value of the numCards, then, you lost the num of the Card, so the codes followed it are executed with numCards == 0
You can just comment the line numCards = 0, and your code is executed right.
I'm not sure why this isn't working, but this is my error message:
Error] no match for 'operator!=' (operand types are 'std::string {aka std::basic_string}' and 'const int')
EDIT: The above issue has been resolved. But, the current issues are redundant ***, and the lack of vowel removal in a sentence rather than just the first word of a sentence.
#include <iostream>
#include <string>
using namespace std;
void removeVowel(string&); // Removes vowels from input string.
string withVowel; // Will be used to read user input.
int main ()
{
const string SENTINEL = "0"; // Sentinel value.
// Request input string unless SENTINEL is entered.
cout << "Enter a word or series of words. " << '\n';
cout << "Or, enter " << SENTINEL << " to quit. " << endl;
cin >> withVowel;
// In case of SENTINEL:
while (withVowel == SENTINEL)
{
cout << "***" << endl;
}
// Run loop.
removeVowel(withVowel);
// Display the string without vowels.
cout << "The word(s) entered reflecting only consonants: " << withVowel << endl;
return 0;
}
void removeVowel(string& withVowel)
{
int i = 0;
int length = int(withVowel.length());
while (i < length)
{
if (withVowel.at(i) == 'a' ||
withVowel.at(i) == 'A' ||
withVowel.at(i) == 'e' ||
withVowel.at(i) == 'E' ||
withVowel.at(i) == 'i' ||
withVowel.at(i) == 'I' ||
withVowel.at(i) == 'o' ||
withVowel.at(i) == 'O' ||
withVowel.at(i) == 'u' ||
withVowel.at(i) == 'U')
{
withVowel.erase(i, 1);
length = int(withVowel.length());
}
else i++;
}
// Display the string without vowels.
cout << removeVowel << endl;
}
Based on your other question, and the error message, I assume withVowel is a std::string. The error message is pretty much telling you what the problem is: you can't compare a std::string with an int.
Since you only need SENTINEL for printing and comparison, just declare it as a std::string as well:
const std::string SENTINEL = "0";
You can't compare const int with strings.
Use ctrl+z and enter to stop the input.
string word;
cout << "ctrl+z and Enter to exit\n";
while (cin >> word){
cout << word << ' ';
// other processing
}
And for your case:
cout << "Enter a word or series of words.\n";
cout << "Ctrl+z and Enter to exit\n";
while (cin >> withVowel){
removeVowel(withVowel);
cout << "The word(s) entered reflecting only consonants :" << withVowel << endl;
}
Program compiles, runs, and works.
Big issue:
It only works for the first word of a sentence.
EXAMPLE:
"Welcome to the jungle" results in "wlcm" rather than "wlcm t th jngl".
Small Issue:
There's a "1" appearing between input and output when it runs. How can I get rid of that? I think it's from this, but I'm not positive:
{
withVowel.erase(i, 1);
length = int(withVowel.length());
}
WHOLE CODE:
#include <iostream>
#include <string>
using namespace std;
void removeVowel(string&); // Removes vowels from input string.
string withVowel; // Will be used to read user input.
int main ()
{
const string SENTINEL = "0"; // Sentinel value.
// Request input string unless SENTINEL is entered.
cout << "Enter a word or series of words." << '\n';
cout << "Or, enter " << SENTINEL << " to quit." << '\n' << endl;
cin >> withVowel;
// In case of SENTINEL:
while (withVowel == SENTINEL)
{
cout << '\n' << "***" << endl;
return 0;
}
// Run loop.
removeVowel(withVowel);
// Display the string without vowels.
cout << "The word(s) entered reflecting only consonants: " << withVowel << endl;
return 0;
}
void removeVowel(string& withVowel)
{
int i = 0;
int length = int(withVowel.length());
while (i < length)
{
if (withVowel.at(i) == 'a' ||
withVowel.at(i) == 'A' ||
withVowel.at(i) == 'e' ||
withVowel.at(i) == 'E' ||
withVowel.at(i) == 'i' ||
withVowel.at(i) == 'I' ||
withVowel.at(i) == 'o' ||
withVowel.at(i) == 'O' ||
withVowel.at(i) == 'u' ||
withVowel.at(i) == 'U')
{
withVowel.erase(i, 1);
length = int(withVowel.length());
}
else i++;
}
// Display the string without vowels.
cout << removeVowel << endl;
}
Use getline(cin, withVowel); instead of cin >> withVowel;
Also replace while with if in main().
And don't forget to upvote and accept answers=)
The problem with only getting the first word is being you're using cin >> withVowel;, which will stop reading input as soon as it encounters white space. Try using std::getine(cin, withVowel); instead.
If at all possible, I would avoid manipulating the string in place, and just copy things to the output if they're not vowels.
std::remove_copy_if(withVowel.begin(), withVowel.end(),
[](char c) { return c == 'a' || c == 'A' ||
c == 'e' || c == 'E' ||
c == 'i' ...;});
I need to know why I am always returning NULL in the following context(for example no matter if I say "Yes", or "No", for any of the questions I am getting null( I assume because the question process repeats when it runs) even though I say no in main. I'm trying to get it to do this: for all invalid answers I return null, if null start over the question process, if answer was valid I return either true or false, if true continue, if false quit program.
bool question1()
{
string answer2;
cout << "Are you 18 or older and have a valid Driver's License? Yes or No: ";
getline( cin, answer2);
transform(answer2.begin(), answer2.end(), answer2.begin(), ::tolower);
cout << endl;
if( answer2 == "yes")
{
cout << "Alright! " << endl << "You are set for registration. Please fill out the registration form. ";
return true;
}
else if( answer2 == "no")
{
cout << "Do you know someone else who is 18 or older that can register? Yes or No ";
getline( cin, answer2);
transform(answer2.begin(), answer2.end(), answer2.begin(), ::tolower);
if( answer2 == "yes")
{
cout << "Good, then please continue the process in their place. Please fill out the registration form";
return true;
}
else if( answer2 == "no")
{
cout << "Please come back later when you have the appropriate personel";
return false;
}
else
{
cout << "The answer given was invalid. Please give a valid answer. " << endl << endl ;
return NULL;
}
}
else
{
cout << "The answer given was invalid. Please give a valid answer. " << endl << endl;
return NULL;
}
void registerPerson( array< string, nameSize > namesOfPeople, array< string, idSize > idlen)
{
string pName;
string dLicense;
static int i = 0;
static int b = 0;
static int c = 0;
unsigned int x = 1;
cout << endl << endl << "REGISTRATION FORM:" << endl << endl << "------------------" << endl;
cout << "Please" << endl << "enter the following: \n \n";
cout << "Name: ";
getline( cin, pName );
for ( int j = i; j<=800; ++ j )
{
namesOfPeople[j] = pName;
cout << namesOfPeople[j];
i = i + 1;
break;
}
cout << endl;
while( x = 1)
{
cout << "Driver\'s Licence Number( Must be 9 characters long, no dashesh ): ";
cin >> dLicense;
if ( dLicense.length() < 9 || dLicense.length()> 9 )
{
cout << "The entered number was invalid. Please try again";
}
else
{
for ( int a = i; c<=800; ++ a )
{
idlen[a] = dLicense;
cout << idlen[a];
c = c + 1;
break;
}
}
}
}
}
{
int main()
array< string, nameSize > names = {};
array< string, idSize > ids = {};
carShare mycarShare1;
carShare mycarShare2;
mycarShare2.welcomeMessage();
mycarShare2.question1();
if( mycarShare1.question1() == NULL)
{
mycarShare1.question1();
}
else if( mycarShare1.question1() == true)
{
mycarShare1.registerPerson(names, ids);
}
else
{
return 0;
}
system( "PAUSE" );
return 0;
}
I'd probably change it to handle invalid input within question1() itself. An easy way to start the question over again is letting question1 call itself again on invalid input until it gets a valid one like here (I put comments at all the changed places.):
bool question1()
{
string answer2;
cout << "Are you 18 or older and have a valid Driver's License? Yes or No: ";
getline( cin, answer2);
transform(answer2.begin(), answer2.end(), answer2.begin(), ::tolower);
cout << endl;
if( answer2 == "yes")
{
cout << "Alright! " << endl << "You are set for registration. Please fill out the registration form. ";
return true;
}
else if( answer2 == "no")
{
cout << "Do you know someone else who is 18 or older that can register? Yes or No ";
getline( cin, answer2);
transform(answer2.begin(), answer2.end(), answer2.begin(), ::tolower);
if( answer2 == "yes")
{
cout << "Good, then please continue the process in their place. Please fill out the registration form";
return true;
}
else if( answer2 == "no")
{
cout << "Please come back later when you have the appropriate personel";
return false;
}
else
{
cout << "The answer given was invalid. Please give a valid answer. " << endl << endl ;
return question1(); // call itself again on invalid input.
}
}
else
{
cout << "The answer given was invalid. Please give a valid answer. " << endl << endl;
return question1(); // call itself again on invalid input.
}
}
int main()
{
array< string, nameSize > names = {};
array< string, idSize > ids = {};
carShare mycarShare1;
carShare mycarShare2;
mycarShare2.welcomeMessage();
bool answer = mycarShare2.question1();
/* if( answer == NULL) we don't need this anymore if we handle errors inside question1()
{
mycarShare1.question1();
}
*/
if( answer == true) // notice the change to if instead of else if here.
{
mycarShare1.registerPerson(names, ids);
}
else
{
return 0;
}
system( "PAUSE" );
return 0;
}
Another way to do the same without recursion would be a do... while loop, here's a version of only question1() with a loop:
bool question1()
{
string answer2; // has to be declared before the loop.
do
{
cout << "Are you 18 or older and have a valid Driver's License? Yes or No: ";
getline( cin, answer2);
transform(answer2.begin(), answer2.end(), answer2.begin(), ::tolower);
cout << endl;
if( answer2 == "yes")
{
cout << "Alright! " << endl << "You are set for registration. Please fill out the registration form. ";
return true;
}
else if( answer2 == "no")
{
cout << "Do you know someone else who is 18 or older that can register? Yes or No ";
getline( cin, answer2);
transform(answer2.begin(), answer2.end(), answer2.begin(), ::tolower);
if( answer2 == "yes")
{
cout << "Good, then please continue the process in their place. Please fill out the registration form";
return true;
}
else if( answer2 == "no")
{
cout << "Please come back later when you have the appropriate personel";
return false;
}
}
}while(answer2 != "yes" && answer2 != "no"); // loop as long as the input is invalid.
}
=====================================
Edit:
As clarification to it printing multiple times, here's the culprit in your original code:
mycarShare2.question1();
if( mycarShare1.question1() == NULL)
{
mycarShare1.question1();
}
else if( mycarShare1.question1() == true)
{
mycarShare1.registerPerson(names, ids);
}
else
{
return 0;
}
mycarShare2.question1() is a function call, you were calling your function 3 times in that part. What you'd want was probably saving it in a bool variable and only test in the if/else statements after that, like this:
bool answer = mycarShare2.question1();
if( answer == NULL)
{
mycarShare1.question1();
}
else if( answer == true)
{
mycarShare1.registerPerson(names, ids);
}
else
{
return 0;
}
Note: this is just to show it the way you probably expected it to be executed in your original code and not a solution. As said bool can only be true or false and the check for NULL isn't needed at all. (See my solutions above.)