Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com TypeCasting

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 10-27-2012, 01:23 AM PM User | #1 pbracing33b New Coder   Join Date: Mar 2012 Posts: 70 Thanks: 6 Thanked 0 Times in 0 Posts TypeCasting I have a program that is suppose to find the percentage of occupied rooms in the hotel. I have most of my program running, but when I try to typecast my percentage the percentage isn't coming out right and I am not sure as to why. Can someone please take a look at my code to see why this isn't working? Code: ```#include #include #include using namespace std; int main() { //Varibles int occupied = 0,floors = 0,rooms = 0, heartBreak = 0; int percentage = 0, total = 0, occupiedTotal = 0,unoccupied = 0, unoccupiedTotal = 0; int mostempty = 0, HBfloor; //Input cout << "How many floors does the hotel have? "; cin >> floors; while (floors < 1 ) // Validate input { cout << "Please enter floors greater than or equal to ten.\n"; cout << "How many floors does the hotel have? "; cin >> floors; } //Run a for loop to get the amount of rooms, and it's occupancy rate! for (int cnt = 1; cnt<=floors; cnt++) { if (cnt == 2)//Skip Floor two { continue; } cout << "How many rooms are on floor "<> rooms; while (rooms < 10) // Validate input { cout << "Please enter rooms greater than or equal to ten.\n"; cout << "How many rooms are on floor "<> rooms; } total += rooms;//Accumulator cout<<"How many of those are occupied?"; cin>> occupied; while(occupied > rooms)//validate input { cout<<"Error, Number of occupied rooms cannot exceed rooms listed \n"; cout<<"How many of those are occupied?"; cin>> occupied; } occupiedTotal += occupied;//Accumulator unoccupied = rooms - occupied; if (unoccupied > mostempty)//find the floor with the least amount of rooms occupied { mostempty = unoccupied; HBfloor = cnt; } } //Processing Section unoccupied = total - occupiedTotal; percentage = (double)total/ occupiedTotal; //output cout<<"The hotel has a total " <
 10-27-2012, 02:04 AM PM User | #2 Spookster Supreme Overlord     Join Date: May 2002 Location: Marion, IA USA Posts: 6,264 Thanks: 4 Thanked 82 Times in 81 Posts This doesn't make sense. You would see why this isn't working if you run it in a debugger and look at what is happening with each value. percentage = (double)total/ occupiedTotal; Why are you using int type here for all 3 variables? And why are you trying to typecast one of them? __________________ Spookster CodingForums Supreme Overlord All Hail Spookster
 10-27-2012, 02:31 AM PM User | #3 pbracing33b New Coder   Join Date: Mar 2012 Posts: 70 Thanks: 6 Thanked 0 Times in 0 Posts Sorry I forgot to change that I was playing with it earlier and forgot to change it back, but it didn't work the other way around either it should've been this to (and was to begin with) : Code: `percentage = occupiedTotal/ (double)total;` I initially did have all doubles but my teacher wanted me to typecast it! Last edited by pbracing33b; 10-27-2012 at 02:36 AM..

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 03:44 AM.