1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Need some help with entry C++

Discussion in 'Programming & Webmastering' started by Atnevon, Jun 27, 2008.

  1. Atnevon

    Atnevon

    Joined:
    Sep 5, 2007
    Messages:
    443 (0.17/day)
    Thanks Received:
    29
    Well, the title is simple. I have an intro C++ assignment that for some reason, will not work. I am just not sure why it isn't.

    Here is a background:

    I have to build a program (win32) to show a list of people attending a conference. Here is the price:

    1-3: 150.00 per person
    4-9: 100.00 per person
    10+: 90.00 per person

    Now, i need the user to keep inputing people (from different companies) untill they tell it to stop. (I am required to use a while loop).

    The program is suppost to calculate the total number of people entered, and display the average cost per person.

    What I keep getting is the number of people to equal -1, and cost of 0. I'm about to stop my foot and swear some more, but not sure how that would help.

    If anyone can give me any tips on how to get this to work. I would be most grateful.

    Just as a note. I'm not asking you do it for me, I wont learn anything that way. I want to understand the whos and why, otherwise I'd just be a cheating P.O.S.

    Thanks alot TPU,
    -Andrew


    I have to do this in Visual Studio C++ Express 2005.
    And here is my code so far:

     
  2. JoJo_Whit3

    Joined:
    May 1, 2008
    Messages:
    43 (0.02/day)
    Thanks Received:
    4
    you're getting -1 each time because you are setting the registrants variable to -1 each time when you break from the while loop. You need another int that keeps a count of the registrants.

    Then in your loop you'd do something like

    Code:
    count += registrants
    and then use that count variable to do all of your price calculations at the end
     
    Atnevon says thanks.
  3. Atnevon

    Atnevon

    Joined:
    Sep 5, 2007
    Messages:
    443 (0.17/day)
    Thanks Received:
    29
    Thanks. I'll give that a try

    EDIT:

    Ok, I tried this and got the total people. It is a start.

    [QUOTE = changed this}

    //Lab 2 - ITCS 1214
    //Created and revised by Andrew Lichtenhan


    #include <iostream>
    #include <iomanip>

    using std::cout;
    using std::cin;
    using std::endl;
    using std::fixed;
    using std::setprecision;

    int main()
    {
    //declare variables
    int registrants = 0;
    double price = 0.0;
    double charge = 0.0;
    double cost = 0.0;
    double totalRegistrants = 0.0;

    //find how many people are attending
    cout << "How many company registrants will be attending? (Enter -1 to stop): ";
    cin >> registrants;

    while (registrants >= 0)
    {
    //make sure at least one person attends
    charge = registrants * price;
    totalRegistrants = totalRegistrants + registrants;

    //get remaining temperatures
    cout << "...and in addition to that company?: ";
    cin >> registrants;
    } //end while

    //make if/else for price ranges
    if (registrants >=1 && registrants <= 3)
    price = (registrants * 150.00);
    else if (registrants >=4 && registrants <= 9)
    price = (registrants * 100.00);
    else if (registrants >=10)
    price = (registrants * 90.00);
    {
    //calculate and display the average price per person
    cost = registrants * price;
    charge = cost / totalRegistrants;

    cout << "Total Registrants: " << totalRegistrants << endl;
    cout << "Average Price: " << charge << endl;

    }
    return 0;
    } //end of main function

    [/QUOTE]
     
  4. Atnevon

    Atnevon

    Joined:
    Sep 5, 2007
    Messages:
    443 (0.17/day)
    Thanks Received:
    29
    crap. I revised code. Newest code is at the top post now. Will reply correctly next time.

    Anyway. I got the correct number to count with my change, now I just need the adverage price to display. Im getting -0 now.
     
  5. JoJo_Whit3

    Joined:
    May 1, 2008
    Messages:
    43 (0.02/day)
    Thanks Received:
    4
    you're almost there...you just need to edit this code

    Code:
    //make if/else for price ranges
    if (registrants >=1 && registrants <= 3)
    price = (registrants * 150.00);
    else if (registrants >=4 && registrants <= 9)
    price = (registrants * 100.00);
    else if (registrants >=10)
    price = (registrants * 90.00);
    {
    //calculate and display the average price per person
    cost = registrants * price;
    charge = cost / totalRegistrants;
    since registrants is -1, you will never end up in any of those if statements. you need to do the comparisons with the totalRegistrants
     
    Atnevon says thanks.
  6. Atnevon

    Atnevon

    Joined:
    Sep 5, 2007
    Messages:
    443 (0.17/day)
    Thanks Received:
    29
    Holy crap. This is where my mind falls apart. I know I'm staring right at it, and it is flowing well in my head, but not to the compy. grr.

    You mentioned I need to do a comparison with the totalRegisters. Used -1 just because it is an easy negitive numer to end my loop.

    This is what I attribute to my lack of math skills. Hence I hope to do shader graphics someday, and chartactor modeling. Because art and programming are 2 totally different sides of the brain.

    I'll update when I think I found something...or get frustrated as hell.


    EDIT:

    You mentioned registrants coming out to -1. Is this due to the code, which you posted, coming up with that, or is it due to the way I initialed my variables to begin with. I feel i just hit the brick wall and am stuck looking into it.
     
    Last edited: Jun 27, 2008
  7. JoJo_Whit3

    Joined:
    May 1, 2008
    Messages:
    43 (0.02/day)
    Thanks Received:
    4
    Ok...first off since you're using Visual Studio, you should learn to set breakpoints and step through code. This will help you out a lot when debugging in the future. If you don't know how to do this, do a quick google search and you should be able to find a tutorial.

    Ok, registrants is going to be -1 because thats how the user breaks from while loop

    Code:
    while (registrants >= 0)
    {
    //make sure at least one person attends
    charge = registrants * price;
    totalRegistrants = totalRegistrants + registrants;   // accumulated total
    
    //get remaining temperatures
    cout << "...and in addition to that company?: ";
    cin >> registrants;  // *** registrants gets set to -1 here by the user to break from while loop
    } //end while
    then, the very next thing you do is

    Code:
    //make if/else for price ranges
    if (registrants >=1 && registrants <= 3)
    price = (registrants * 150.00);
    but registrants was just set to -1 by the user. therefore, you're if/else statements will never be true.

    you want to do the comparisons with the totalRegistrants entered...not just the last value entered by the user

    to do this, the code would look like
    Code:
    if (totalRegistrants >=1 && totalRegistrants <= 3)
    ...
    
    hopefully, this helps
     
    Atnevon says thanks.
  8. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.99/day)
    Thanks Received:
    5,615
    Location:
    Cheeseland (Wisconsin, USA)
    Atn, why are you doing "charge = registrants * price" in your while loop?
    When the while loop executes, price = 0, so charge will always be zero.
    It looks like you don't need that line at all.
     
  9. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.99/day)
    Thanks Received:
    5,615
    Location:
    Cheeseland (Wisconsin, USA)
    Here's another little tip that may help you out in the future.
    Don't use variables to perform multiple functions.

    For instance you are using the registrants variable to count people and also to determine the end of input.
    You should try to avoid this as it complicates things.
    Expecially when your code gets more complex.

    I would create a char variable called something like "Completed".
    Then do a while loop with that variable...
    Code:
    Char Completed = 'N';
    Int Registrants = 0;
    Int TotalRegs = 0;
    
    while (Completed == 'N')
    {
        cout << "How many to register?"
        cin >> registrants;
        TotalRegs = TotalRegs + Registrants;
    
        cout << "Are you done? (Y or N)"
        cin >> Completed;
    }
    
    // continue program .....
    
    This way the Registrants variable has only one purpose, to count people.

    I write almost exclusively in C#, so forgive me if my C++ syntax is a little off base :D
     
    Last edited: Jun 27, 2008
    Atnevon says thanks.
  10. Oliver_FF

    Oliver_FF New Member

    Joined:
    Oct 15, 2006
    Messages:
    546 (0.19/day)
    Thanks Received:
    65
    You might want a == in that while loop Kreij ;) It'll compile and run but won't work XD
     
  11. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.99/day)
    Thanks Received:
    5,615
    Location:
    Cheeseland (Wisconsin, USA)
    LOL .. thanks Oliver, I always used it in C# but was not sure if C++ used the same syntax.
    Too lazy to try it. FIXED !!!

    Like I said, it's been awhile since I've done any C++ programming.
     
  12. Atnevon

    Atnevon

    Joined:
    Sep 5, 2007
    Messages:
    443 (0.17/day)
    Thanks Received:
    29
    Well, holy hell. I finally, by some miracle got it to work. I am going to study it in a week or so when I have the time to really sit and ponder. This is the last week of class, and have to crank another program out. As one of my coworkers suggested:

    "If it ghostly magically works, don;t F$%K with it. Wait a bit, and look later. This way you can go full speed in your next project without your last one bugging you."

    I like the while loop you suggested Kreij. It makes more since to me just looking at it. However, my textbook has not really touched anything like a confirmation series to continue loops, so my instructor might get suspicious that someone did it for me. But I will keep that little set in my mind for when a more appropriate time should come.

    Here is my code, just to share it with you all. I essential moved and changed some variables, and moved my if statements so they did their job correctly.

    Thanks agian TPU,
    -Andrew

     
  13. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.99/day)
    Thanks Received:
    5,615
    Location:
    Cheeseland (Wisconsin, USA)
    Great job Atn!!

    As long as you searched for an answer and got the code working, without a total copy and paste, you are learning. EVERYONE learns from the code of others.
    That is what programming is all about.
    If your instructor has a problem with that, tell him to make an account here on TPU and take us on. We love a challenge :toast:
     
    Atnevon says thanks.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page