By comparison

This is a discussion on By comparison within the C++ Programming forums, part of the General Programming Boards category; to the rest of the problems posten on this forum, my own seem completely inferior. i'm stuck. lol, i haven't ...

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

    Unhappy By comparison

    to the rest of the problems posten on this forum, my own seem completely inferior.

    i'm stuck.

    lol, i haven't much (any) C++ experience (i started my first program about 15 hours ago)

    anyway, i hope u brains can help me with this as C++ seems to be a pretty cool thing to learn, but i'm baffled.

    Code:
    #include <iostream.h>
    
    using namespace std;
    
    //const float standard_charge = 2.00; // standard charge,
    							   // per person, per hour
    
    // Paintball assignment. A program to produce quotes
    // for customers of a paintball company
    void main()
    {
      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.60;
      float periodb = 2.30;
      float periodc = 2.10;
      float periodd = 2.00;
      int ballsa = 0;
      int ballsb = 50;
      int ballsc = 100;
      int ballsd = 160;
      int ballse = 250;
      char gear;
    
    cout<< "Enter group size (30 being the max): ";
    cin>> group_size;
    cout<< endl;
    cin.ignore();
    
    cout<< "Thankyou. How long do you want to play (time in whole hours)?: ";
    cin>> duration;
    
    if (duration <= 2) {
    charge = perioda * (group_size * duration);
    }
    else if (duration >2<=5) {
    charge = periodb * (group_size * duration);
    }
    if (duration >5<=7) {
    charge = periodc * (group_size * duration);
    }
    else if (duration >7<=10) {                         //seems to * by 2.10 as opposed to 2.00
    charge = periodd * (group_size * duration);
    }
    
    cout<< endl;
    cin.ignore();
    
    cout<< "The session you have specified will cost: " << charge;
    cout<< endl;
    cin.ignore();
    
    
    cout<< "You will receive "<< paintballs <<" free paintballs for your game.";
    
    if (group_size <= 10) {                                           //users only ever get 20 free paintballs
    paintballs = ((group_size * duration) * paintballs) + ballsa;
    }
    else if (group_size >10<=15) {
    paintballs = (paintballs * (group_size * duration)) + ballsb;
    }
    if (group_size >15<=20) {
    paintballs = (paintballs * (group_size * duration)) + ballsc;
    }
    else if (group_size >20<=25) {
    paintballs = (paintballs * (group_size * duration)) + ballsd;
    }
    if (group_size >25<=30) {
    paintballs = (paintballs * (group_size * duration)) + ballse;
    }
    cout<< endl;
    cin.ignore();
    
    
    gear = 0;
    
    do {
    cout<< "Do you need to hire equipment at a charge of 5 per group? (Y/N): ";
    cin>> gear;
    } while (gear != 0 || 1);
    
    if (gear = 0) {
    cout<< "Thankyou. You have chosen not to hire any equipment.";
    }
    else if (gear = 1) {
          cout<< "Thankyou. You have chosen to hire equipment. This session will cost: "
              <<charge +5;
    }
    the whole 'do while' loop thing at the bottom?? i just don't get it. i want my program to ask the user whether or not they need to hire equipment and based on their answer Y or N the program then adds 5 to the final cost.

    also ... another thing that baffled me is the free paintballs. each paintballer gets 20 free paintballs per hour. i then wanted to add a group booking bonus wereby larger groups get an extra 50 or 100 etc. balls. but my code seems to spill out 20 free paintballs every time.

    aaaand i'm also having trouble working out what is wrong (although i realise that very little of my code is actually any good) with my duration. the first three groups 0-2, 3-5, 5-7 work fine i think, but the 8-10 doesn't.

    sorry if this is all a bit trivial. i'm sure u lot get annoyed with dunce questions.
    What sweet nanny goat a go ru'n him belly...

  2. #2
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Didn't see it hard enough...just corrected errors and changed void main to int main...never write void main
    Code:
    #include <iostream>
    
    using namespace std;
    
    //const float standard_charge = 2.00; // standard charge,
    							   // per person, per hour
    
    // Paintball assignment. A program to produce quotes
    // for customers of a paintball company
    int main()
    {
      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;
      float periodb = 2.30f;
      float periodc = 2.10f;
      float periodd = 2.00f;
      int ballsa = 0;
      int ballsb = 50;
      int ballsc = 100;
      int ballsd = 160;
      int ballse = 250;
      char gear;
    
    cout<< "Enter group size (30 being the max): ";
    cin>> group_size;
    cout<< endl;
    cin.ignore();
    
    cout<< "Thankyou. How long do you want to play (time in whole hours)?: ";
    cin>> duration;
    
    if (duration <= 2) {
    charge = perioda * (group_size * duration);
    }
    else if (duration >2<=5) {
    charge = periodb * (group_size * duration);
    }
    if (duration >5<=7) {
    charge = periodc * (group_size * duration);
    }
    else if (duration >7<=10) {                         //seems to * by 2.10 as opposed to 2.00
    charge = periodd * (group_size * duration);
    }
    
    cout<< endl;
    cin.ignore();
    
    cout<< "The session you have specified will cost: " << charge;
    cout<< endl;
    cin.ignore();
    
    
    cout<< "You will receive "<< paintballs <<" free paintballs for your game.";
    
    if (group_size <= 10) {                                           //users only ever get 20 free paintballs
    paintballs = ((group_size * duration) * paintballs) + ballsa;
    }
    else if (group_size >10<=15) {
    paintballs = (paintballs * (group_size * duration)) + ballsb;
    }
    if (group_size >15<=20) {
    paintballs = (paintballs * (group_size * duration)) + ballsc;
    }
    else if (group_size >20<=25) {
    paintballs = (paintballs * (group_size * duration)) + ballsd;
    }
    if (group_size >25<=30) {
    paintballs = (paintballs * (group_size * duration)) + ballse;
    }
    cout<< endl;
    cin.ignore();
    
    
    gear = 0;
    
    do {
    cout<< "Do you need to hire equipment at a charge of 5 per group? (Y/N): ";
    cin>> gear;
    } while (gear != 0 || 1);
    
    if (gear = 0) {
    cout<< "Thankyou. You have chosen not to hire any equipment.";
    }
    else if (gear = 1) 
    {
          cout<< "Thankyou. You have chosen to hire equipment. This session will cost: "
              <<charge +5;
    }
    return 0;
    }

  3. #3
    Student kahli_mist's Avatar
    Join Date
    Nov 2005
    Location
    Preston, UK
    Posts
    10
    safe man, nice one.

    the thing is.... i'm still getting the same wrong answers!

    i dunno what the crack is like, but i can't clock it.

    using 10 for the first 2 inputs should charge 200 not 210. i'm still only gettin' 20 free paintballs instead of 2,250 and when the user choses Y or N for their gear selection it still just loops. i need it to get out the loop and state the new charge (if the user selected Y then there is an additional 5 charge if N then no additional charge).

    cheers ... i'll keep battling. lol
    What sweet nanny goat a go ru'n him belly...

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    #include <iostream.h> ---> <iostream>
    else if (duration >2<=5)
    I'm not sure where you got that syntax for writing a compound conditional: it's neither valid in mathematics nor is it valid in computer programming. If you want to check if 'duration' is greater than 2 AND less than or equal to 5, you do this:

    if(duration > 2 && duration<=5)

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    (gear != 0 || 1) will always be true. I will add parenthesis so you see what's going on

    (gear != 0) || (1), the 1 will always evaluate true
    so no matter what gear != 0 equals, the 1 will evaluate true making the whole expression true;

    you probably meant: while( gear!=0 && gear!=1)

  6. #6
    Student kahli_mist's Avatar
    Join Date
    Nov 2005
    Location
    Preston, UK
    Posts
    10
    safe, nice one guys. much appreciated.
    What sweet nanny goat a go ru'n him belly...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. std::string comparison versus int comparison
    By leeor_net in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2009, 07:28 AM
  3. Bug in iterator comparison in C++ standard?
    By steev in forum C++ Programming
    Replies: 14
    Last Post: 07-12-2008, 12:02 AM
  4. [Warning] comparison between pointer and integer
    By rkooij in forum C Programming
    Replies: 5
    Last Post: 05-12-2006, 08:43 AM
  5. comparison between pointer and integer
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-07-2006, 12:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21