Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,402 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,113 people online right now. Registration is fast and FREE... Join Now!




Boolean Statement Execution

 
Reply to this topicStart new topic

Boolean Statement Execution

eadams20
30 Oct, 2006 - 03:29 PM
Post #1

D.I.C Head
**

Joined: 30 Aug, 2006
Posts: 65


My Contributions
here is the code snippet I am refering too
CODE

do
        {
        // User Input
        cout << "enter the time: (hours and minutes sepreated by space in 24 hour clock style)";
        cin >> hours >> minutes;
        // check for data to be correct using dataareOK
        if((hours < 25) && (hours > 0) && (minutes <= 59) && (minutes > 0))
           dataareOK = true;
        else
            dataareOK = false;
            cout << "Please, enter a hour 1-24 and minute 1-59" <<endl;
          
          
         // While data are not Okay
        } while (dataareOK == false);    
        


This is the only part of my code giving me trouble.. it prints the "please, enter a hour...." regardless of if its false or not. It works great on the false, but when its true it still prints.

Thanks in advance.
Emily
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Boolean Statement Execution
30 Oct, 2006 - 04:39 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
You need braces around the else code block...a lack of braces tells the compiler that only one line of code will follow the statemnt...and you have two. It's just a simple matter of properly structuring the code.
CODE

if((hours < 25) && (hours > 0) && (minutes <= 59) && (minutes > 0))
           dataareOK = true;
        else{
            dataareOK = false;
            cout << "Please, enter a hour 1-24 and minute 1-59" <<endl;
            }

It's not a bad idea to get in the habit of using braces to denote code blocks even when not necessary...helps in the end.
User is offlineProfile CardPM
+Quote Post

eadams20
RE: Boolean Statement Execution
30 Oct, 2006 - 04:54 PM
Post #3

D.I.C Head
**

Joined: 30 Aug, 2006
Posts: 65


My Contributions
thanks so much what a silly mistake!!

Emily


QUOTE(Amadeus @ 30 Oct, 2006 - 05:39 PM) *

You need braces around the else code block...a lack of braces tells the compiler that only one line of code will follow the statemnt...and you have two. It's just a simple matter of properly structuring the code.
CODE

if((hours < 25) && (hours > 0) && (minutes <= 59) && (minutes > 0))
           dataareOK = true;
        else{
            dataareOK = false;
            cout << "Please, enter a hour 1-24 and minute 1-59" <<endl;
            }

It's not a bad idea to get in the habit of using braces to denote code blocks even when not necessary...helps in the end.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 03:12AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month