Connect with us

nesting a program with a bool statement

Discussion in 'Microcontrollers, Programming and IoT' started by foTONICS, Sep 29, 2012.

  1. foTONICS

    foTONICS

    332
    9
    Sep 30, 2011
    so I have this program that asks a user to enter a time(24 hour clock) and spit out the next available flight. the program works except for one thing: I've tried to get it to loop until a user chooses to quit (giving the variable <int action> a value of 2) but no matter the value of <action> the program terminates. Am I not looping this correctly? Here's the code:


    // lab3q8.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <process.h>

    //variables
    int user_h,user_m,air_h,air_m,action;

    //boolean operator (checks the user input for validity)
    bool _valid;

    //main program
    int main(void)
    {
    //printf("TP1");
    while (_valid==true);
    {
    //printf("TP2");
    //system("clear");//clears screen upon each program loop
    printf("Please enter a time (24 hour clock) in the following format (hh:mm)");
    scanf_s("%d:%d", &user_h, &user_m);
    if ((user_h<=24&&user_h>=0)&&(user_m>=0||user_m<=60))//this if statement checks if the user input is valid
    {
    if (user_h<=8)//this if statement determines the output
    printf("The next available flight:\nDeparture\tArrival\n8:00a.m.\t10:16a.m.");
    else if (user_h<=9&&user_m<43)
    printf("The next available flight:\nDeparture\tArrival\n9:43a.m.\t11:52a.m.");
    else if (user_h<=11&&user_m<19)
    printf("The next available flight:\nDeparture\tArrival\n11:19a.m.\t1:31a.m.");
    else if (user_h<=12&&user_m<47)
    printf("The next available flight:\nDeparture\tArrival\n12:47p.m.\t3:00p.m.");
    else if (user_h<2)
    printf("The next available flight:\nDeparture\tArrival\n2:00p.m.\t4:08p.m.");
    else if (user_h<=3&&user_m<45)
    printf("The next available flight:\nDeparture\tArrival\n3:45p.m.\t5:55p.m.");
    else if (user_h<7)
    printf("The next available flight:\nDeparture\tArrival\n7:00p.m.\t9:20p.m.");
    else if (user_h<=9&&user_m<45)
    printf("The next available flight:\nDeparture\tArrival\n9:45p.m.\t11:58p.m.");
    else
    printf("I'm sorry, but you have missed all available flight times for today; please try again tomorrow morning");
    printf("Would you like to run this program again?, key \"1\" for yes and \"2\" for no");
    scanf_s("%d", &action);
    if (action=2)
    _valid = false;
    else
    _valid = true;
    }
    else
    {
    printf("Please enter a valid format/time, please execute this program again");
    _valid==false;
    system("PAUSE");
    }
    }


    return 0;
    }
     
  2. Harald Kapp

    Harald Kapp Moderator Moderator

    9,129
    1,842
    Nov 17, 2011
    I see a few issues:

    1)
    Code:
    bool _valid;
    
    //main program
    int main(void)
    {
    //printf("TP1");
    while (_valid==true);
    
    The variable _valid is not initialized before you first use it. Add _valid = true before the while statement.

    2)
    Code:
    if (action=2)
    This is not a comparison but an assignment.
    use
    Code:
    if (action==2)
    3)
    Code:
    scanf_s("%d", &action);
    if (action=2)
    
    check the value of action. Is it really 2? Use a debugger or add a debug-output between these two code lines.

    4)
    a tip: shorten the code to get the loop running as required:
    Code:
    // lab3q8.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <process.h>
    
    //variables
    int user_h,user_m,air_h,air_m,action;
    
    //boolean operator (checks the user input for validity)
    bool _valid;
    
    //main program
    int main(void)
    {
    _valid = true; // my change, see above
    //printf("TP1");
    while (_valid==true);
       {
       printf("Would you like to run this program again?, key \"1\" for yes and \"2\" for no");
       scanf_s("%d", &action);
       printf("%d\n", action); // debug output added by me
       if (action==2) // my change, see above
           _valid = false;
       else
          _valid = true;
       }
    return 0;
    } 
     
  3. foTONICS

    foTONICS

    332
    9
    Sep 30, 2011
    these are very useful hints, ill make those changes and let you know how it turns out


    -thanks again!
     
  4. Harald Kapp

    Harald Kapp Moderator Moderator

    9,129
    1,842
    Nov 17, 2011
    Code:
    if (action=2)
    _valid = false
    Not only is this an assignment of the value 2 to the variable action, moreover the result of this assignment is generally true so _valid becomes necessaryily false and the loop is terminated after the first run. This is the real problem, apart from the other points I mentioned.
     
  5. foTONICS

    foTONICS

    332
    9
    Sep 30, 2011
    I made the changes and it works now. Thanks for the help!
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-