Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > / operation

Reply
Thread Tools

/ operation

 
 
Richard
Guest
Posts: n/a
 
      09-27-2005
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 . . .


 
Reply With Quote
 
 
 
 
Mark
Guest
Posts: n/a
 
      09-27-2005
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%.

 
Reply With Quote
 
 
 
 
Richard
Guest
Posts: n/a
 
      09-28-2005
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 . . .


 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      09-28-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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%.
>



 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      09-28-2005
Sorry, make TotalRoom a double too, or write double(TotalRoom) so that
it is treated like one.

 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      09-28-2005
Sorry, make TotalRoom a double too, or write double(TotalRoom) so that
it is treated like one.

 
Reply With Quote
 
Jay Nabonne
Guest
Posts: n/a
 
      09-28-2005
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

 
Reply With Quote
 
hde
Guest
Posts: n/a
 
      09-28-2005
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

 
Reply With Quote
 
Dave Rahardja
Guest
Posts: n/a
 
      09-28-2005
On 27 Sep 2005 20:42:17 -0700, "hde" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Dave Rahardja
Guest
Posts: n/a
 
      09-28-2005
On Tue, 27 Sep 2005 19:41:51 -0500, "Richard" <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Boolean operation and arithmetic operation Buzz Lightyear C++ 10 08-12-2009 01:27 PM
I/O operation, file operation behaviou raan C++ 2 08-16-2007 07:13 PM
Does bit operation always work more efficiently than math operation? david ullua C Programming 13 03-01-2006 11:02 PM
DWL-G650 operation with (and without) WPA2 Fred Marshall Wireless Networking 6 05-08-2005 02:14 AM
my AMD compaq laptop frooze about 4 minutes of operation =?Utf-8?B?SGFycnkgTGV1bmc=?= Wireless Networking 0 08-17-2004 04:15 AM



Advertisments