473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

/ operation

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 . . .
Sep 27 '05 #1
11 1353
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%.

Sep 28 '05 #2
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 . . .
Sep 28 '05 #3
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" <mn*******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr 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%.

Sep 28 '05 #4
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

Sep 28 '05 #5
Sorry, make TotalRoom a double too, or write double(TotalRoom) so that
it is treated like one.

Sep 28 '05 #6
Sorry, make TotalRoom a double too, or write double(TotalRoom) so that
it is treated like one.

Sep 28 '05 #7
hde
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

Sep 28 '05 #8
On 27 Sep 2005 20:42:17 -0700, "hde" <hd*@foobar-qux.org> 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.
Sep 28 '05 #9
On Tue, 27 Sep 2005 19:41:51 -0500, "Richard" <no********@yahoo.com> 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.

Sep 28 '05 #10

"Richard" <no********@yahoo.com> wrote in message
news:np********************@comcast.com...
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 ?

Oct 1 '05 #11
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
kb******@gascad.at
Oct 3 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Marcus | last post by:
Hi I have a very complex sql query and a explain plan. I found there is a full table scan in ID=9 9 8 TABLE ACCESS (FULL) OF 'F_LOTTXNHIST' (Cost=84573 Card=185892...
4
by: Hardy Wang | last post by:
Hi, I have a win form application, when a button is clicked, a lengthy operation will be triggered. During the time program is still running, this application seems not to be able to response to...
0
by: John Jenkins | last post by:
Hi, any help on this would be greatly apprciated. I have been given a wsdl file from a customer generated by Oracle 10g Web Services tools. When I use wsdl.exe to attempt to produce proxy classes I...
1
by: Laurent Lequenne | last post by:
Hello There, I just converted a VS 2003 C++ Project into VS 2005. I already made some changes in my headers files, has I had compilations errors with enums declarations. Now everything compiles...
0
by: relaxedrob | last post by:
Hi All, I have a portType such as this: <portType name="CMLeJobSoapGetEmpBrand"> <operation name="EJobGetEmpBrand"> <input message="tns:EJobEmpBrdReq" name="EJobEmpBrdReq"/> <output...
8
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item...
6
by: Cerebrus99 | last post by:
Hi all, I'm making a Windows application that does some lengthy retrieval operations from a database and possibly from a internet resource. I want to show that the operation is going on, by...
3
by: JuHui | last post by:
I wrote a script to get 100 pages from a server. like below: 1:import httplib 2:conns = httplib.HTTPConnection("www.mytest.com") 3:conn.request("GET", "/") sometimes a socket error was...
2
by: Robinson | last post by:
I can start an Asynchronous operation against a data source with SQLCommand.BeginExecuteReader, allowing me to loop, checking for user cancellation before the operation has completed, but how then...
0
by: Default User | last post by:
I work on creating test cases for a SOAP-based set of servers, using soapUI. I received and updated set of WSDL and schema files, and when I made new tests and mock server operations, all of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.