Thread: return

  1. #1
    Student kahli_mist's Avatar
    Join Date
    Nov 2005
    Location
    Preston, UK
    Posts
    10

    return

    hi, i'm trying to offer the user a chance to make more than one booking on a booking system. at the moment my code is

    Code:
      do {
      cout<< "Would you like to book another session? (Y/N): ";
      cin>> book_again;
      cout<< endl;
      } while (book_again !='y' && book_again !='Y' && book_again !='n' && book_again !='N');
    
      if (book_again == 'N' || book_again == 'n') {
      system("pause");
      }
    
      else if (book_again == 'Y' || book_again == 'y') {
      return 0;
      }
    can any1 help me with this please? when i input 'N' or 'n' the code works fine, but when i input 'Y' or 'y' it just shuts closes the program as opposed to repeating the program.
    What sweet nanny goat a go ru'n him belly...

  2. #2
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    >...it just shuts closes the program...

    That's what 'return 0' does.

    What you want is to put the booking part inside a loop that is executed when the book_again = 'y' or 'Y'. (You can make life easier with the tolower() or toupper() functions, as well.)

    Example:
    Code:
    book_again = 'y';
    
    while (tolower(book_again) == 'y')
    {
        //booking function
    
        cout << "again? (y/n) ";
        cin >> book_again;
    }
    *edit* - You'll need to #include <cctype> for the tolower() and toupper() functions.
    Last edited by Decrypt; 12-01-2005 at 03:00 PM.
    There is a difference between tedious and difficult.

  3. #3
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Your while loop will always be fales.
    How can book_again, one char be 'y' and 'Y' and 'n' and 'N'?
    Also, "!=" means not equal to, so I do not even begin to understand what you think that while loop would do.

    Code:
     do 
      {
      
    	 cout<< "Would you like to book another session? (Y/N): ";
    	cin>> book_again;
    	cout<< endl;
     
      
      } while (book_again  == 'y' || book_again == 'Y');
    
    
    
      if (book_again == 'N' || book_again == 'n')
      {
      system("pause");
      }
    
      
      //This following else-if statement will never be executed. If it is y/Y it will be
      //in the do-while loop, it will only leave the do-while loop when it is 'n' or 'N'
      //Thus the following can never be eval'd true and never 'run'
      else if (book_again == 'Y' || book_again == 'y') {
      return 0;
      }
      //End of something that never runs
    
       
       return 0;
        
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Your while loop will always be fales.

    The do-while loop is fine. It will run while the answer is not 'Y', 'y', 'N', or 'n'. It is meant to loop until the user gives a valid response to the question.

    The problem is that the entire program (or at least the part that you want to repeat) should be in a loop. That loop should exit if the answer is 'n' or 'N'. Otherwise it should continue. It's hard to say how best to do that without the rest of the code.

  5. #5
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    The do-while loop is fine. It will run while the answer is not 'Y', 'y', 'N', or 'n'. It is meant to loop until the user gives a valid response to the question.
    Yes, but in this case if you enter y/Y/n/N, any of those four options and it will terminate the do-while loop.

    I was not as clear when I said it will always be false, it will always be false…or true depending on how you read it for valid input, and so the loop will never run again for y/Y.

  6. #6
    Student kahli_mist's Avatar
    Join Date
    Nov 2005
    Location
    Preston, UK
    Posts
    10
    Code:
    #include <iostream.h>
    #include <iomanip.h>
    
    const int NAME_LEN = 25;
    const int DATE_LEN = 11;
    const int BUFFER_LEN = 11;
    
    using namespace std;
    
    // Paintball assignment. A program to produce quotes
    // for customers of a paintball company
    int main()
    {
      int final_cost;
      int paintball_total;
      int group_size; // size of the group
      int duration; // length of times in hours
      float charge; // charge for the booked session
      int paintballs = 20; //each player gets 20 free per hour
      float perioda = 2.60f;     // £2.60 per hour if playing less than or equal to 2 hour
      float periodb = 2.30f;     // £2.30 per hour if playing between 3 and 5 hours
      float periodc = 2.10f;     // £2.10 per hour if playing for 6 or 7 hours
      float periodd = 2.00f;     // £2.00 per hour if playing between 8 or 10 hours
      int ballsa = 0;            // groups upto 10 members get no bonus balls
      int ballsb = 50;           // groups between 11 and 15 get 50 bonus balls
      int ballsc = 100;          // groups between 16 and 20 get 100 bonus balls
      int ballsd = 160;          // groups between 21 and 25 get 160 bonus balls
      int ballse = 250;          // groups between 26 and 30 get 250 bonus balls
      char gear;                 // users answer Y or N to equipment hire
      char extra;                // users answer Y or N to extra paintballs
      int extra_balls=0;           // how many balls does the user want
      int balls_total=0;           //
      char name[NAME_LEN];
      char date;
      char buffer[BUFFER_LEN];
      char book_again;
    
       cout << "Please enter name - Initial and Surname ";
       fflush(stdin);
       cin.getline(name, NAME_LEN);
       cout << endl;
    
    do {
       cout << "Please enter the date of the booking (DD/MM/YYY) ";
       cin.getline (buffer, BUFFER_LEN);
       cout << endl;
       } while (strlen(buffer) !=10);
    
    
    do {
            // this section specifies group size
        cout << "Enter group size (30 being the max): ";
        cin >> group_size;
        cout << endl;
        if (!(group_size >= 1 && group_size <= 30))
        {
        cout << "Invalid group size, Please enter 1 to 30";
        cout << endl;
        }
    } while (!(group_size >= 1 && group_size <= 30));
    
    
    do {
          // this section specifies game duration
          cout<< "How long do you want to play (1 to 10 hours)?: ";
          cin>> duration;
          cout<< endl;
          if (!(duration >= 1 && duration <= 10));
          {
          cout << endl;
          }
          } while (!(duration >= 1 && duration <= 10));
    
            if (duration <= 1)
            {
            charge = perioda * (group_size * duration);
            }
            else if (duration >=2 && duration <=5)
            {
            charge = periodb * (group_size * duration);
            }
            if (duration >5 && duration <=7)
            {
            charge = periodc * (group_size * duration);
            }
            else if (duration >7 && duration <=10)
            {
            charge = periodd * (group_size * duration);
            }
    
        cin.ignore();
    
    
    
    {   // this section works out the cost for specified session
        cout<< "The session you have specified will cost: \234" << charge;
        cout<< endl;
        cin.ignore();
    }
    
    
    {   // this section works out how many free paintballs the group will receive
        if (group_size <=10) {
             paintballs = (paintballs*(group_size*duration))+ ballsa;
             }
             else if (group_size >10 && group_size <=15) {
             paintballs = (paintballs*(group_size*duration))+ ballsb;
             }
             else if (group_size >15 && group_size <=20) {
             paintballs = (paintballs*(group_size*duration))+ ballsc;
             }
             else if (group_size >20 && group_size <=25) {
             paintballs = (paintballs*(group_size*duration))+ ballsd;
             }
             else if (group_size >25 && group_size <=30) {
             paintballs = (paintballs*(group_size*duration))+ ballse;
             }
    
        cout<< "You will receive "<< paintballs <<" free paintballs for your game.";
        cin.ignore();
        cout<< endl;
    
    }
    
    
    {   //this section asks the user if they wish to hire equipment
        do {
        cout<< "Do you need to hire equipment at a charge of \2345 per group? (Y/N): ";
        cin>> gear;
        } while (gear!='N' && gear != 'n' && gear!='Y' && gear!='y');
    
        if (gear == 'N' || gear == 'n' ) {
        cout<< "Thankyou. You have chosen not to hire any equipment.";
        cin.ignore();
        cout<< endl;
        cout<< endl;
        }
        else if (gear == 'Y' || gear == 'y') {
        cout<< "Thankyou. You have chosen to hire equipment. This session will cost: \234"
            <<charge +5;
        cin.ignore();
        cout<< endl;
        cout<< endl;
        }
    }
    
    
    {   // this section asks the user if they wish to buy more balls
        do {
        cout<< "Do you want any more paintballs at 100 for \2346 (Y/N): ";
        cin>> extra;
        } while (extra !='n' && extra != 'N' && extra !='Y' && extra !='y');
    
        if (extra == 'N' || extra == 'n') {
        cout<< "Thankyou. You have requested NO extra paintballs";
        cout<< endl;
        cin.ignore();
        }
        else if (extra == 'Y' || extra == 'y') {
        cout<< "Thankyou. How many extra packets of paintballs would you like?: ";
        cin>> extra_balls;
    
        balls_total = extra_balls*6;
    
        cout<< "Thankyou. "<<extra_balls<<"packets will cost you: \234" <<balls_total;
        cin.ignore();
        cout<< endl;
        }
    }
    
    {
    
    
        final_cost = balls_total + charge;
    
        cout<< "The total charge for this session will be \234"<<final_cost<<", thankyou.";
        cout<< endl;
        cout<< endl;
        cout<< endl;
    }
    
    
    {
        paintball_total = paintballs + (extra_balls*100);
    }
    
    
    {
        cout << "---------------------------------------------------------------------------- " <<endl;
        cout << "   Name    |  Date    | Duration | Group | Equipment |  Total     | Total  | " <<endl;
        cout << "           |          |          | Size  |   Hire    | Paintballs | Cost   | " <<endl;
        cout << "---------------------------------------------------------------------------- " <<endl;
    
        cout << setw(10)<< name;
        cout << setw(12)<< buffer;
        cout << setw(7) << duration;
        cout << setw(10)<< group_size;
        cout << setw(7) << gear;
        cout << setw(15) << paintball_total;
        cout << setw(11) << final_cost;
    
    
    
    
    
        cout<< endl;
        cout << "---------------------------------------------------------------------------- " <<endl;
        cout<< endl;
        cout<< endl;
    }
    
    
    
      do {
      cout<< "Would you like to book another session? (Y/N): ";
      cin>> book_again;
      cout<< endl;
      } while (book_again ='y' && book_again ='Y' && book_again ='n' && book_again ='N');
    
      if (book_again == 'N' || book_again == 'n') {
      system("pause");
      }
    
      else if (book_again == 'Y' || book_again == 'y') {
      return 0;
      }
    
    
    }
    and there it is.


    i see the end part as:

    output 'do you want to book again' until user choses Y/y or N/n. specifying Y/y should then return the user to start of program. specifying N/n should stop the program...

    i can't get my head round it! it's something so simple and it's had me baffled for a good 45 mins. now that's hardcore stupidity in my books.
    What sweet nanny goat a go ru'n him belly...

  7. #7
    Student kahli_mist's Avatar
    Join Date
    Nov 2005
    Location
    Preston, UK
    Posts
    10
    sorry guys, that should be:

    Code:
    } while (book_again !='y' && book_again !='Y' && book_again !='n' && book_again !='N');
    at the bottom. thanks.
    What sweet nanny goat a go ru'n him belly...

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The entire program (or at least the part that you want to repeat) should be in a loop. That loop should exit if the answer is 'n' or 'N'. Otherwise it should continue. You can nest loops. Put all that code in main() inside a loop. Then run the loop while book_again is 'Y' or 'y'. As was mentioned before, the return 0 is what ends the program. In fact, if you put the return 0 after the system pause, your loop could be "infinite" since it would end when the user typed 'n' or 'N'.

  9. #9
    Student kahli_mist's Avatar
    Join Date
    Nov 2005
    Location
    Preston, UK
    Posts
    10
    Code:
    #include <iostream.h>
    #include <iomanip.h>
    
    const int NAME_LEN = 25;
    const int DATE_LEN = 11;
    const int BUFFER_LEN = 11;
    
    using namespace std;
    
    // Paintball assignment. A program to produce quotes
    // for customers of a paintball company
    int main()
    
    do {
    
    {
      int final_cost;
      int paintball_total;
      int group_size; // size of the group
      int duration; // length of times in hours
      float charge; // charge for the booked session
      int paintballs = 20; //each player gets 20 free per hour
      float perioda = 2.60f;     // £2.60 per hour if playing less than or equal to 2 hour
      float periodb = 2.30f;     // £2.30 per hour if playing between 3 and 5 hours
      float periodc = 2.10f;     // £2.10 per hour if playing for 6 or 7 hours
      float periodd = 2.00f;     // £2.00 per hour if playing between 8 or 10 hours
      int ballsa = 0;            // groups upto 10 members get no bonus balls
      int ballsb = 50;           // groups between 11 and 15 get 50 bonus balls
      int ballsc = 100;          // groups between 16 and 20 get 100 bonus balls
      int ballsd = 160;          // groups between 21 and 25 get 160 bonus balls
      int ballse = 250;          // groups between 26 and 30 get 250 bonus balls
      char gear;                 // users answer Y or N to equipment hire
      char extra;                // users answer Y or N to extra paintballs
      int extra_balls=0;           // how many balls does the user want
      int balls_total=0;           //
      char name[NAME_LEN];
      char date;
      char buffer[BUFFER_LEN];
      char book_again;
    
       cout << "Please enter name - Initial and Surname ";
       fflush(stdin);
       cin.getline(name, NAME_LEN);
       cout << endl;
    
    do {
       cout << "Please enter the date of the booking (DD/MM/YYY) ";
       cin.getline (buffer, BUFFER_LEN);
       cout << endl;
       } while (strlen(buffer) !=10);
    
    
    do {
            // this section specifies group size
        cout << "Enter group size (30 being the max): ";
        cin >> group_size;
        cout << endl;
        if (!(group_size >= 1 && group_size <= 30))
        {
        cout << "Invalid group size, Please enter 1 to 30";
        cout << endl;
        }
    } while (!(group_size >= 1 && group_size <= 30));
    
    
    do {
          // this section specifies game duration
          cout<< "How long do you want to play (1 to 10 hours)?: ";
          cin>> duration;
          cout<< endl;
          if (!(duration >= 1 && duration <= 10));
          {
          cout << endl;
          }
          } while (!(duration >= 1 && duration <= 10));
    
            if (duration <= 1)
            {
            charge = perioda * (group_size * duration);
            }
            else if (duration >=2 && duration <=5)
            {
            charge = periodb * (group_size * duration);
            }
            if (duration >5 && duration <=7)
            {
            charge = periodc * (group_size * duration);
            }
            else if (duration >7 && duration <=10)
            {
            charge = periodd * (group_size * duration);
            }
    
        cin.ignore();
    
    
    
    {   // this section works out the cost for specified session
        cout<< "The session you have specified will cost: \234" << charge;
        cout<< endl;
        cin.ignore();
    }
    
    
    {   // this section works out how many free paintballs the group will receive
        if (group_size <=10) {
             paintballs = (paintballs*(group_size*duration))+ ballsa;
             }
             else if (group_size >10 && group_size <=15) {
             paintballs = (paintballs*(group_size*duration))+ ballsb;
             }
             else if (group_size >15 && group_size <=20) {
             paintballs = (paintballs*(group_size*duration))+ ballsc;
             }
             else if (group_size >20 && group_size <=25) {
             paintballs = (paintballs*(group_size*duration))+ ballsd;
             }
             else if (group_size >25 && group_size <=30) {
             paintballs = (paintballs*(group_size*duration))+ ballse;
             }
    
        cout<< "You will receive "<< paintballs <<" free paintballs for your game.";
        cin.ignore();
        cout<< endl;
    
    }
    
    
    {   //this section asks the user if they wish to hire equipment
        do {
        cout<< "Do you need to hire equipment at a charge of \2345 per group? (Y/N): ";
        cin>> gear;
        } while (gear!='N' && gear != 'n' && gear!='Y' && gear!='y');
    
        if (gear == 'N' || gear == 'n' ) {
        cout<< "Thankyou. You have chosen not to hire any equipment.";
        cin.ignore();
        cout<< endl;
        cout<< endl;
        }
        else if (gear == 'Y' || gear == 'y') {
        cout<< "Thankyou. You have chosen to hire equipment. This session will cost: \234"
            <<charge +5;
        cin.ignore();
        cout<< endl;
        cout<< endl;
        }
    }
    
    
    {   // this section asks the user if they wish to buy more balls
        do {
        cout<< "Do you want any more paintballs at 100 for \2346 (Y/N): ";
        cin>> extra;
        } while (extra !='n' && extra != 'N' && extra !='Y' && extra !='y');
    
        if (extra == 'N' || extra == 'n') {
        cout<< "Thankyou. You have requested NO extra paintballs";
        cout<< endl;
        cin.ignore();
        }
        else if (extra == 'Y' || extra == 'y') {
        cout<< "Thankyou. How many extra packets of paintballs would you like?: ";
        cin>> extra_balls;
    
        balls_total = extra_balls*6;
    
        cout<< "Thankyou. "<<extra_balls<<"packets will cost you: \234" <<balls_total;
        cin.ignore();
        cout<< endl;
        }
    }
    
    {
    
    
        final_cost = balls_total + charge;
    
        cout<< "The total charge for this session will be \234"<<final_cost<<", thankyou.";
        cout<< endl;
        cout<< endl;
        cout<< endl;
    }
    
    
    {
        paintball_total = paintballs + (extra_balls*100);
    }
    
    
    {
        cout << "---------------------------------------------------------------------------- " <<endl;
        cout << "   Name    |  Date    | Duration | Group | Equipment |  Total     | Total  | " <<endl;
        cout << "           |          |          | Size  |   Hire    | Paintballs | Cost   | " <<endl;
        cout << "---------------------------------------------------------------------------- " <<endl;
    
        cout << setw(10)<< name;
        cout << setw(12)<< buffer;
        cout << setw(7) << duration;
        cout << setw(10)<< group_size;
        cout << setw(7) << gear;
        cout << setw(15) << paintball_total;
        cout << setw(11) << final_cost;
    
    
    
    
    
        cout<< endl;
        cout << "---------------------------------------------------------------------------- " <<endl;
        cout<< endl;
        cout<< endl;
    }
    
    
    
    do {
    cout<< "Would you like to book another session? (Y/N): ";
    cin>> book_again;
    cout<< endl;
    } while (book_again !='y' && book_again !='Y' && book_again !='n' && book_again !='N');
    
    if (book_again == 'N' || book_again == 'n') {
    system("pause");
    
    else if (book_again == 'y' || book_again == 'Y')
    
    
    } while (book_again == 'Y' || book_again == 'y');
    ok, so i've put all my code into a loop. surely this should mean that while book_again = 'Y' or book_again = 'y' ... the program keeps repeating. instead i'm getting a decleration syntax error on the opening 'do {' and i very nearly broke my glasses ripping them from my face in distress. i don't know what i'm doin' wrong here. but it's really doing my head in.

    i'm sorry guys, u must be laughing in disbelief but i am absolutely clueless. this is no joke i've been in this uni building since 9 this morning doing this program. i finally thought i'd done it and my mate says 'sweet, all you gotta do is bang it in a loop and ur done!' so he gets off. and i thought i'd be able to breeze it, but i'm missing summat blatantly simple.
    What sweet nanny goat a go ru'n him belly...

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Simple mistake. You put he do before the opening brace of main(). Of course, you also have to declare and initialize book_again before the do (putting the whole main() code into a loop wasn't meant literally). You should also get rid of that else if at the bottom, since it isn't really doing anything.

  11. #11
    Student kahli_mist's Avatar
    Join Date
    Nov 2005
    Location
    Preston, UK
    Posts
    10
    right-o. i've put my...
    Code:
    do {
    ...in between where i've declared my variables and the 'please enter initial and surname'. i've put a closing bracket above...
    Code:
    } while (book_again == 'Y' || book_again == 'y');
    now when i try and run the program everything is going sexy and smooth and i almost have a smile on my face when the...
    Code:
    } while (book_again == 'Y' || book_again == 'y');
    ...gets highlighted and a 'compound statement missing }' turns up on the scene.

    :'( i thought i'd got it sorted! DAMMIT! you guys have shocking patience.
    What sweet nanny goat a go ru'n him belly...

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There is a missing brace somewhere in your code.

    Make sure the code is nicely formatted (each new, nested block gets indented further than the preivous one). Match up each opening brace with each closing brace. Just look for some problem like that.

    For example, in your last code posted, there is an opening brace after the if at the bottom, but no closing brace. Maybe that's still in your current code.

  13. #13
    Student kahli_mist's Avatar
    Join Date
    Nov 2005
    Location
    Preston, UK
    Posts
    10
    i did it!

    thankyou soooooo so much. mate, u have no idea how much of my ass u have just saved. it's safe to say it's a very large portion. u, are a leg end.
    What sweet nanny goat a go ru'n him belly...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM