Help with break;

I get this error when trying to break out of my else statement:

47:9: error: break statement not within loop or switch

Here is my code, what am I doing wrong:

#include<iostream>
#include<ctime>
//#include "Books.h"
//#include "Songs.h"

using namespace std;

void displayTime(bool myAnswer);

int main() {

bool answer;
cout << "Is it time for your child to go to sleep: \n";
cout << "Please enter 1 for yes and 0 for no. \n";
cin >> answer;

displayTime(answer); // This will just display the current date/time

cout << "Let get ready to take a nap" << endl;

return 0;
}

void displayTime(bool myAnswer){
if(myAnswer == 1){
time_t now;
time(&now);
cout << "Current time is: " << ctime(&now);
}
else(myAnswer == 0);
{
cout << "We will check back later " << endl;
break; I would like the program to stop here, go no further
}
}
Last edited on
break is for loops or switch.

Please use code tags - the <> button on the right.

You can also use continue in loops.
Last edited on
The error message is clear enough. For whom do you think compilers generate error messages?!
I would like it to stop the program if answer 0(false). And I'm at a lose as to how to make this happen.

Any suggestion?

Any help would be great
Change break to exit( 0 ) and include header <cstdlib>
Topic archived. No new replies allowed.