/ operation

Discussion in 'C++' started by Richard, Sep 28, 2005.

  1. Richard

    Richard Guest

    I do no know why the % amount print out as 0 % for the program below. The
    operation seems right. Can anyone help. I am new to c++

    code
    #include <iostream>

    #include <iomanip>

    using namespace std;

    int main()

    {


    // ask for information

    int Floor, Room, Occuppied, TotalRoom =0, TotalOccuppied=0,
    TotalUnOccuppied;

    int PercentOccuppied;

    cout<<"How many floors the hotel has?:";

    cin>>Floor;

    for (int count=1; count <= Floor; count++)

    {

    if (count==13)

    continue;

    cout<<"How many rooms in floor "<<count<<"? : ";

    cin>>Room;

    TotalRoom +=Room;

    cout<<"How many rooms are occuppied? : ";

    cin>>Occuppied;

    TotalOccuppied +=Occuppied;

    }

    cout<<"There are "<<TotalRoom<<" rooms in this hotel. "<<endl;

    cout<<"There are "<<TotalOccuppied<<" rooms that are occuppied. "<<endl;

    TotalUnOccuppied= TotalRoom - TotalOccuppied;

    cout<<"There are "<<TotalUnOccuppied<<" rooms that are not
    occuppied."<<endl;

    PercentOccuppied = TotalOccuppied/TotalRoom;

    //cout<<setprecision(2);

    //cout.setf(ios::fixed | ios::showpoint);

    cout<<"The percent of rooms that are occuppied is "<<PercentOccuppied<<"
    %"<<endl;

    }

    Output

    How many floors the hotel has?:2
    How many rooms in floor 1? : 50
    How many rooms are occuppied? : 5
    How many rooms in floor 2? : 50
    How many rooms are occuppied? : 5
    There are 100 rooms in this hotel.
    There are 10 rooms that are occuppied.
    There are 90 rooms that are not occuppied.
    The percent of rooms that are occuppied is 0 %
    Press any key to continue . . .
     
    Richard, Sep 28, 2005
    #1
    1. Advertising

  2. Richard

    Mark Guest

    Two problems.

    10/100 = 0.1
    Since "PercentOccupied" is an integer, it rounds down.
    Make it a float or a double.

    Secondly, you are going to want to multiply that number by 100 to get a
    percent. It should be 10%, not 0.1%.
     
    Mark, Sep 28, 2005
    #2
    1. Advertising

  3. Richard

    Richard Guest

    another problem

    I think my logic is out of wax. I do not understand how this code work, and
    it does not work. the program is prompted the user to enter a series of
    numbers, then it find the largest and smallest. thanks again

    #include <iostream>

    #include <iomanip>

    using namespace std;

    int main()

    {

    double Number, Greatest, Lowest;

    do

    {

    cout<<"Enter an interger number:";

    cin>>Number;

    Greatest = Number;

    Lowest = Number;

    if (Greatest >= Number)

    {

    Greatest = Number;

    }

    if (Lowest <= Number)

    {

    Lowest = Number;

    }

    }while ( !(Number == -99));

    cout<<"The largest number you entered is: "<<Greatest<<endl;

    cout<<"The Lowest number you entered is: "<<Lowest<<endl;

    }

    output

    Enter an interger number:58
    Enter an interger number:96
    Enter an interger number:-9
    Enter an interger number:-36
    Enter an interger number:85
    Enter an interger number:-99
    The largest number you entered is: -99
    The Lowest number you entered is: -99
    Press any key to continue . . .
     
    Richard, Sep 28, 2005
    #3
  4. Richard

    Richard Guest

    I changed the code as you advised, but still the same output. % is still 0.

    #include <iostream>

    #include <iomanip>

    using namespace std;

    int main()

    {


    // ask for information

    int Floor, Room, Occuppied, TotalRoom =0, TotalOccuppied=0,
    TotalUnOccuppied;

    double PercentOccuppied;

    cout<<"How many floors the hotel has?:";

    cin>>Floor;

    for (int count=1; count <= Floor; count++)

    {

    if (count==13)

    continue;

    cout<<"How many rooms in floor "<<count<<"? : ";

    cin>>Room;

    TotalRoom +=Room;

    cout<<"How many rooms are occuppied? : ";

    cin>>Occuppied;

    TotalOccuppied +=Occuppied;

    }

    cout<<"There are "<<TotalRoom<<" rooms in this hotel. "<<endl;

    cout<<"There are "<<TotalOccuppied<<" rooms that are occuppied. "<<endl;

    TotalUnOccuppied= TotalRoom - TotalOccuppied;

    cout<<"There are "<<TotalUnOccuppied<<" rooms that are not
    occuppied."<<endl;

    PercentOccuppied = (TotalOccuppied/TotalRoom)*100;

    //cout<<setprecision(2);

    //cout.setf(ios::fixed | ios::showpoint);

    cout<<"The percent of rooms that are occuppied is "<<PercentOccuppied<<"
    %"<<endl;








    }

    "Mark" <> wrote in message
    news:...
    > Two problems.
    >
    > 10/100 = 0.1
    > Since "PercentOccupied" is an integer, it rounds down.
    > Make it a float or a double.
    >
    > Secondly, you are going to want to multiply that number by 100 to get a
    > percent. It should be 10%, not 0.1%.
    >
     
    Richard, Sep 28, 2005
    #4
  5. Richard

    Mark Guest

    Sorry, make TotalRoom a double too, or write double(TotalRoom) so that
    it is treated like one.
     
    Mark, Sep 28, 2005
    #5
  6. Richard

    Mark Guest

    Sorry, make TotalRoom a double too, or write double(TotalRoom) so that
    it is treated like one.
     
    Mark, Sep 28, 2005
    #6
  7. Richard

    Jay Nabonne Guest

    On Tue, 27 Sep 2005 19:45:02 -0500, Richard wrote:

    > I changed the code as you advised, but still the same output. % is still 0.
    >

    <snip>
    >
    > PercentOccuppied = (TotalOccuppied/TotalRoom)*100;
    >


    You're multiplying 0 times 100.

    Try:

    PercentOccuppied = (TotalOccuppied*100)/TotalRoom;

    - Jay
     
    Jay Nabonne, Sep 28, 2005
    #7
  8. Richard

    hde Guest

    A thing to remember is, in c++ if you want floating point
    precision(decimal points) you have to make sure you are dividing by a
    floating point. If you divide by a integer even though you are storing
    the quotient in a floating point typed variable, c++ will round the
    value due to dividing by an int. Make sure all the variables in the
    equation are floating point or use a cast.

    Cheers
    Harley D. Eades III
     
    hde, Sep 28, 2005
    #8
  9. On 27 Sep 2005 20:42:17 -0700, "hde" <> wrote:

    >A thing to remember is, in c++ if you want floating point
    >precision(decimal points) you have to make sure you are dividing by a
    >floating point. If you divide by a integer even though you are storing
    >the quotient in a floating point typed variable, c++ will round the
    >value due to dividing by an int. Make sure all the variables in the
    >equation are floating point or use a cast.


    This assertion may be confusing to a beginner. The types involved in an
    operation has nothing to do with division with a floating point, but rather on
    the way C++ promotes types in an expression.

    Maybe the interested parties can read up on C++ numerical type promotions.
     
    Dave Rahardja, Sep 28, 2005
    #9
  10. Re: another problem

    On Tue, 27 Sep 2005 19:41:51 -0500, "Richard" <> wrote:

    >I think my logic is out of wax. I do not understand how this code work, and
    >it does not work. the program is prompted the user to enter a series of
    >numbers, then it find the largest and smallest. thanks again
    >
    >#include <iostream>
    >
    >#include <iomanip>
    >
    >using namespace std;
    >
    >int main()
    >
    >{
    >
    >double Number, Greatest, Lowest;
    >
    >do
    >
    >{
    >
    >cout<<"Enter an interger number:";
    >
    >cin>>Number;
    >
    >Greatest = Number;
    >
    >Lowest = Number;
    >
    >if (Greatest >= Number)
    >
    >{
    >
    >Greatest = Number;
    >
    >}
    >
    >if (Lowest <= Number)
    >
    >{
    >
    >Lowest = Number;
    >
    >}
    >
    >}while ( !(Number == -99));
    >
    >cout<<"The largest number you entered is: "<<Greatest<<endl;
    >
    >cout<<"The Lowest number you entered is: "<<Lowest<<endl;
    >
    >}
    >
    >output
    >
    >Enter an interger number:58
    >Enter an interger number:96
    >Enter an interger number:-9
    >Enter an interger number:-36
    >Enter an interger number:85
    >Enter an interger number:-99
    >The largest number you entered is: -99
    >The Lowest number you entered is: -99
    >Press any key to continue . . .
    >


    You're initializing Greatest and Lowest each time you retrieve a number from
    cin. You need to initialize Greatest and Lowest _once_ (possibly using some
    logic to trap for the first assignment), and only perform the comparison in
    each subsequent loop.
     
    Dave Rahardja, Sep 28, 2005
    #10
  11. "Richard" <> wrote in message
    news:...
    > I do no know why the % amount print out as 0 % for the program below. The
    > operation seems right. Can anyone help. I am new to c++
    >
    > code
    > #include <iostream>
    >
    > #include <iomanip>
    >
    > using namespace std;
    >
    > int main()
    >
    > {
    >
    >
    > // ask for information
    >
    > int Floor, Room, Occuppied, TotalRoom =0, TotalOccuppied=0,
    > TotalUnOccuppied;
    >
    > int PercentOccuppied;
    >
    > cout<<"How many floors the hotel has?:";
    >
    > cin>>Floor;
    >
    > for (int count=1; count <= Floor; count++)
    >
    > {
    >
    > if (count==13)
    >
    > continue;
    >
    > cout<<"How many rooms in floor "<<count<<"? : ";
    >
    > cin>>Room;
    >
    > TotalRoom +=Room;
    >
    > cout<<"How many rooms are occuppied? : ";
    >
    > cin>>Occuppied;
    >
    > TotalOccuppied +=Occuppied;
    >
    > }
    >
    > cout<<"There are "<<TotalRoom<<" rooms in this hotel. "<<endl;
    >
    > cout<<"There are "<<TotalOccuppied<<" rooms that are occuppied. "<<endl;
    >
    > TotalUnOccuppied= TotalRoom - TotalOccuppied;
    >
    > cout<<"There are "<<TotalUnOccuppied<<" rooms that are not
    > occuppied."<<endl;
    >
    > PercentOccuppied = TotalOccuppied/TotalRoom;
    >
    > //cout<<setprecision(2);
    >
    > //cout.setf(ios::fixed | ios::showpoint);
    >
    > cout<<"The percent of rooms that are occuppied is "<<PercentOccuppied<<"
    > %"<<endl;
    >
    > }
    >
    > Output
    >
    > How many floors the hotel has?:2
    > How many rooms in floor 1? : 50
    > How many rooms are occuppied? : 5
    > How many rooms in floor 2? : 50
    > How many rooms are occuppied? : 5
    > There are 100 rooms in this hotel.
    > There are 10 rooms that are occuppied.
    > There are 90 rooms that are not occuppied.
    > The percent of rooms that are occuppied is 0 %
    > Press any key to continue . . .
    >
    >


    Why do computer science professors come up with such lame examples,
    come on, are they just entirely lacking in imagination or are they too tied
    up
    in their own pet research that they can't be bothered to put the effort into
    teaching ?
     
    Dave Townsend, Oct 1, 2005
    #11
  12. Dave Townsend wrote:
    >
    >
    > Why do computer science professors come up with such lame examples,
    > come on, are they just entirely lacking in imagination or are they too tied
    > up
    > in their own pet research that they can't be bothered to put the effort into
    > teaching ?


    Because somewhere one has to start. It is surprisingly
    difficult to come up with assignments when all you can
    work with are: variables, input/output and loop constructs.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 3, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MajorTom
    Replies:
    4
    Views:
    2,274
    MajorTom
    May 27, 2005
  2. Sunil Shukla

    floating point operation in VHDL

    Sunil Shukla, Sep 1, 2004, in forum: VHDL
    Replies:
    1
    Views:
    635
    ALuPin
    Sep 2, 2004
  3. david ullua
    Replies:
    13
    Views:
    668
  4. raan
    Replies:
    2
    Views:
    452
  5. Buzz Lightyear
    Replies:
    10
    Views:
    1,123
    Alexander Bartolich
    Aug 12, 2009
Loading...

Share This Page