/ operation

R

Richard

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 . . .
 
M

Mark

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%.
 
R

Richard

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 . . .
 
R

Richard

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;








}
 
M

Mark

Sorry, make TotalRoom a double too, or write double(TotalRoom) so that
it is treated like one.
 
M

Mark

Sorry, make TotalRoom a double too, or write double(TotalRoom) so that
it is treated like one.
 
H

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
 
D

Dave Rahardja

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.
 
D

Dave Rahardja

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.
 
D

Dave Townsend

Richard said:
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 ?
 
K

Karl Heinz Buchegger

Dave said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top