C++ Code
audiobliss
Posts: 12,518
I just finished writing code for another C++ program, and I just had to share.
This problem was rather involved, or at least for my level of expertise, because of all the loops and input validation. We JUST covered the chapter on loops this past week. Last class period one guy said we all better start on this lab early because the last problem is a pain, so I wasn't looking forward to it. But, I just sat down and wrote it, and besides a few missing semicolons, it runs prefectly. No exaggeration, missing semicolons is the only thing I had wrong. I probably spent about 25 - 30 minutes on it, I guess.
Anyhoo, I was just so excited that I wrote a complicated program perfectly on the first try, I had to share! :D
Of course, I haven't tested the program with different data THAT extensively...but I'm pretty sure it doesn't have any hiccups.
And my program: (Sorry for the formatting...I can't get spaces to show up in the post.)
This problem was rather involved, or at least for my level of expertise, because of all the loops and input validation. We JUST covered the chapter on loops this past week. Last class period one guy said we all better start on this lab early because the last problem is a pain, so I wasn't looking forward to it. But, I just sat down and wrote it, and besides a few missing semicolons, it runs prefectly. No exaggeration, missing semicolons is the only thing I had wrong. I probably spent about 25 - 30 minutes on it, I guess.
Anyhoo, I was just so excited that I wrote a complicated program perfectly on the first try, I had to share! :D
Of course, I haven't tested the program with different data THAT extensively...but I'm pretty sure it doesn't have any hiccups.
Write a program that calculates the occupancy rate for a hotel. The program should start by asking the user how many floors the hotel has. A loop should then iterate once for each floor. In each iteration, the loop should ask the user for the number of rooms on the floor and how many of them are occupied. After all the iterations, the program should display how many rooms the hotel has, how many of them are occupied, how many are unoccupied, and the percentage of rooms that are occupied. The percentage may be calculated by dividing the number of rooms occupied by the number of rooms.
NOTE: It is traditional that many hotels do not have a 13th floor. The loop in this program should skip the entire 13th iteration.
Input validation: Do not accept a value less than 1 for the number of floors. Do not accept a number less than 10 for the number of rooms on a floor. Do not accept a value for occupied rooms on a floor that exceeds the number of rooms on that floor.
And my program: (Sorry for the formatting...I can't get spaces to show up in the post.)
#include "stdafx.h"
#include< iostream >
#include< iomanip >
using namespace std;
int main()
{
int totalFloors, rooms, occupied, totalRooms = 0, totalOccupied = 0, empty;
double percentage;
//Get input
cout << "How many floors does the hotel have? ";
cin >> totalFloors;
//Input validation
while (totalFloors < 1)
{
cout << "Error; please enter the number of floors again: ";
cin >> totalFloors;
}
//Main loop
for (int floor = 1; floor <= totalFloors; floor++)
{
if (floor == 13)
continue;
cout << "How many rooms are on floor " << floor << " ? ";
cin >> rooms;
while (rooms < 10)
{
cout << "Please enter at least 10. ";
cin >> rooms;
}
//Update accumulator
totalRooms += rooms;
//Get more input
cout << "How many of the rooms on floor " << floor << " are occupied? ";
cin >> occupied;
while (occupied > rooms)
{
cout << "The number of occupied rooms cannot exceed " << endl;
cout << "the total number of rooms on the floor." << endl;
cout << "Please enter a valid number. ";
cin >> occupied;
}
//Update accumulator
totalOccupied += occupied;
}
//Calculate output
empty = totalRooms - totalOccupied;
percentage = totalOccupied / static_cast< double >(totalRooms);
//Display output
cout << "\n\n\n";
cout << "The hotel has " << totalRooms << " rooms total." << endl;
cout << "Total occupied rooms is " << totalOccupied << "." << endl;
cout << "Total unoccupied rooms is " << empty << "." << endl;
cout << fixed << showpoint << setprecision(2);
cout << "The percentage of occupied rooms is " << (100 * percentage) << "%." << endl;
//Pause to allow user to view output
cout << endl << endl;
system("pause");
return 0;
}
In UseGeorge Grand wrote: »
PS3, Yamaha CDR-HD1300, Plex, Amazon Fire TV Gen 2
Pioneer Elite VSX-52, Parasound HCA-1000A
Klipsch RF-82ii, RC-62ii, RS-42ii, RW-10d
Epson 8700UB
In Storage
[Home Audio]
Rotel RCD-02, Yamaha KX-W900U, Sony ST-S500ES, Denon DP-7F
Pro-Ject Phono Box MKII, Parasound P/HP-850, ASL Wave 20 monoblocks
Klipsch RF-35, RB-51ii
[Car Audio]
Pioneer Premier DEH-P860MP, Memphis 16-MCA3004, Boston Acoustic RC520
Post edited by audiobliss on
Comments
-
congrats !
whoda thunk you were smart ?!!!!:eek:
j/k
that`s actually pretty cool stuff
must be a gas when it does what you want/tell it to do.....Cary SLP-98L F1 DC Pre Amp (Jag Blue)
Parasound HCA-3500
Cary Audio V12 amp (Jag Red)
Polk Audio Xm Reciever (Autographed by THE MAN Himself) :cool:
Magnum Dynalab MD-102 Analog Tuna
Jolida JD-100 CDP
Polk Audio LSi9 Speaks (ebony)
SVS PC-Ultra Sub
AQ Bedrock Speaker Cables (Bi-Wired)
MIT Shotgun S1 I/C`s
AQ Black Thunder Sub Cables
PS Audio Plus Power Cords
Magnum Dynalab ST-2 FM Antenna
Sanus Cherry wood Speak Stands
Adona AV45CS3 / 3 Tier Rack (Black /Gold)
:cool: -
HAHA, brings back some memories of my college days. Wait until you get to Assembly language, that's where the fun really starts...
-
Dammit PM, you beat me to it. "Real men write assembly."If you will it, dude, it is no dream.
-
bobman1235 wrote: »Dammit PM, you beat me to it. "Real men write assembly."
-
I can just see a conversation at your guy`s house now..
0010011100010101010011011110100101
yep !
the she said,
1o10010101o100o01o100100001111101010101010101Cary SLP-98L F1 DC Pre Amp (Jag Blue)
Parasound HCA-3500
Cary Audio V12 amp (Jag Red)
Polk Audio Xm Reciever (Autographed by THE MAN Himself) :cool:
Magnum Dynalab MD-102 Analog Tuna
Jolida JD-100 CDP
Polk Audio LSi9 Speaks (ebony)
SVS PC-Ultra Sub
AQ Bedrock Speaker Cables (Bi-Wired)
MIT Shotgun S1 I/C`s
AQ Black Thunder Sub Cables
PS Audio Plus Power Cords
Magnum Dynalab ST-2 FM Antenna
Sanus Cherry wood Speak Stands
Adona AV45CS3 / 3 Tier Rack (Black /Gold)
:cool: -
Hahahahaha. Thank heavens, we're not touching assembly language! I'm perfectly happy letting some other programming language represent my commands!George Grand wrote: »
PS3, Yamaha CDR-HD1300, Plex, Amazon Fire TV Gen 2
Pioneer Elite VSX-52, Parasound HCA-1000A
Klipsch RF-82ii, RC-62ii, RS-42ii, RW-10d
Epson 8700UB
In Storage
[Home Audio]
Rotel RCD-02, Yamaha KX-W900U, Sony ST-S500ES, Denon DP-7F
Pro-Ject Phono Box MKII, Parasound P/HP-850, ASL Wave 20 monoblocks
Klipsch RF-35, RB-51ii
[Car Audio]
Pioneer Premier DEH-P860MP, Memphis 16-MCA3004, Boston Acoustic RC520 -
Polkmaniac wrote: »Too funny...that's twice this week someone's beat you to the punch...
Gettin' slow in my old ageIf you will it, dude, it is no dream. -
Dude, that's good... I stopped after I learned JavaScript cause that's all I needed...
-
I can just see a conversation at your guy`s house now..
0010011100010101010011011110100101
yep !
the she said,
1o10010101o100o01o100100001111101010101010101
man, thats harsh, did she really do that?
-CodyMusic is like candy, you have to get rid of the rappers to enjoy it -
Ok so really good at C ++ here, I have something I like someone to check out for me.
Speakers
Carver Amazing Fronts
CS400i Center
RT800i's Rears
Sub Paradigm Servo 15
Electronics
Conrad Johnson PV-5 pre-amp
Parasound Halo A23
Pioneer 84TXSi AVR
Pioneer 79Avi DVD
Sony CX400 CD changer
Panasonic 42-PX60U Plasma
WMC Win7 32bit HD DVR -
bobman1235 wrote: »Dammit PM, you beat me to it. "Real men write assembly."
Yes "Real men write assembly", but to be honest I don't know C++ or just I haven't learned it. Or just I don't really have the need to learn it.
Speakers
Carver Amazing Fronts
CS400i Center
RT800i's Rears
Sub Paradigm Servo 15
Electronics
Conrad Johnson PV-5 pre-amp
Parasound Halo A23
Pioneer 84TXSi AVR
Pioneer 79Avi DVD
Sony CX400 CD changer
Panasonic 42-PX60U Plasma
WMC Win7 32bit HD DVR -
Do they even teach assembly anymore? What a waste of a semester. The two languages you need to learn are C# and Java. If you want to consider SQL a language, you need to learn that too. After you master that, then move on to coding frameworks, .Net and J2EE. With those in hand, you'll be admirably equipped to battle for a job with an offshore Indian programmer that costs less than half of your starting salary. Good luck!
-
wallstreet wrote: »Do they even teach assembly anymore? What a waste of a semester. The two languages you need to learn are C# and Java. If you want to consider SQL a language, you need to learn that too. After you master that, then move on to coding frameworks, .Net and J2EE. With those in hand, you'll be admirably equipped to battle for a job with an offshore Indian programmer that costs less than half of your starting salary. Good luck!
Waste of a semester? I use assembly every day. Do anything with embedded systems or firmware and you'll need assembly. Do any OS work and you'll most likely need assembly. Knowing assembly even if you don't use it gives you a much larger understanding of how a computer actually works, as opposed to high level languages that really give you no idea of what's actually going on. You may not care, but plenty of people do, otherwise your C# and J2EE wouldn't have anything to run on.If you will it, dude, it is no dream. -
wallstreet wrote: »Do they even teach assembly anymore? What a waste of a semester. The two languages you need to learn are C# and Java. If you want to consider SQL a language, you need to learn that too. After you master that, then move on to coding frameworks, .Net and J2EE. With those in hand, you'll be admirably equipped to battle for a job with an offshore Indian programmer that costs less than half of your starting salary. Good luck!
I know one language that sets me apart from all of the offshore folks... care to guess what that is? -
Well would that be Assembly Hmmm....
BTW building a Company around some Assembly Language device I build to work between TiVo / ReplayTV and 4DTV C-Band satellite.
www.dvrblackbox.com
Speakers
Carver Amazing Fronts
CS400i Center
RT800i's Rears
Sub Paradigm Servo 15
Electronics
Conrad Johnson PV-5 pre-amp
Parasound Halo A23
Pioneer 84TXSi AVR
Pioneer 79Avi DVD
Sony CX400 CD changer
Panasonic 42-PX60U Plasma
WMC Win7 32bit HD DVR -
wallstreet wrote: »The two languages you need to learn are C# and Java.
No, C++ is still the king of the hill and will be so for a long time to come. Sure there are plenty of website building jobs out there that C# is good for but for serious software design nothing beats C++ and some low level C is in most cases good enough, you can do some enhancing with ASM but most of the times it's not needed (thank god). -
I can just see a conversation at your guy`s house now..
0010011100010101010011011110100101
yep !
the she said,
1o10010101o100o01o100100001111101010101010101
You actually only said about 2 or 3 letters right there..
HMMMM....hardly a sentence....smarty:p
Ill build the computers and let you guys develope the software to help me do my job easier.........
......hahahaha suckersssssssss...:DMonitor 7b's front
Monitor 4's surround
Frankinpolk Center (2 mw6503's with peerless tweeter)
M10's back surround
Hafler-200 driving patio Daytons
Tempest-X 15" DIY sub w/ Rythmik 350A plate amp
Dayton 12" DVC w/ Rythmik 350a plate amp
Harman/Kardon AVR-635
Oppo 981hd
Denon upconvert DVD player
Jennings Research (vintage and rare)
Mit RPTV WS-55513
Tosh HD-XA1
B&K AV5000
Dont BAN me Bro!!!!:eek: -
01010011 01100011 01110010 01100101 01110111 00100000 01111001 01101111 01110101 00100000
01100111 01110101 01111001 01110011 00101100 00100000 01001001 00100111 01101101 00100000
01100111 01101111 01101001 01101110 01100111 00100000 01101000 01101111 01101101 01100101 -
Polkmaniac wrote: »01010011 I 01100011 a 01110010 m 01100101 P 01110111 o 00100000 l 01111001 k 01101111 m 01110101 a 00100000 n
01100111 i 01110101 a 01111001 c 01110011 a 00101100 n 00100000 d 01001001 i 00100111 a 01101101 m 00100000 a
01100111 n 01101111 i 01101001 d 01101110 i 01100111 o 00100000 t 01101000 t 01101111 o 01101101 o 01100101 !
Monitor 7b's front
Monitor 4's surround
Frankinpolk Center (2 mw6503's with peerless tweeter)
M10's back surround
Hafler-200 driving patio Daytons
Tempest-X 15" DIY sub w/ Rythmik 350A plate amp
Dayton 12" DVC w/ Rythmik 350a plate amp
Harman/Kardon AVR-635
Oppo 981hd
Denon upconvert DVD player
Jennings Research (vintage and rare)
Mit RPTV WS-55513
Tosh HD-XA1
B&K AV5000
Dont BAN me Bro!!!!:eek: -
Polkmaniac wrote: »01010011 01100011 01110010 01100101 01110111 00100000 01111001 01101111 01110101 00100000
01100111 01110101 01111001 01110011 00101100 00100000 01001001 00100111 01101101 00100000
01100111 01101111 01101001 01101110 01100111 00100000 01101000 01101111 01101101 01100101
I don't know what's more sad, that I translated that, or that it only took me two minutes.
I need to really analyze my life here.If you will it, dude, it is no dream. -
audiobliss wrote: »Anyhoo, I was just so excited that I wrote a complicated program perfectly on the first try, I had to share! :D
I just compiled your code and after the first run I found a serious bug in the code, want me to tell you where or do you want to try to find out yourself? There's at least one other.
Let me know if you need any hints. -
Is it "int"? That doesn't work for really large numbers...
or is it: if (floor == 13)? -
No, I think int has enough capacity. int = signed int, -32768 to 32767. Although he should have used unsigned int, 0 to 65535, and check for negative inputs.
The biggest problem comes when a user enters characters instead of integers. You have to remember C++ is a strong-typed language. I already PM'ed AB solution for it as I didn't think anyone here would be interested to see it.