Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 09-24-2005, 05:38 PM   #1 (permalink)
subodhgupta1
Registered User
 
Join Date: Sep 2005
Posts: 24
subodhgupta1 is on a distinguished road
Shipping Charges

Hi,
Please anybody can look at this program and tell me what is the problem that I can't get the validation to work

The Fast Freight Shipping Company charges the following rates:

Write a program that asks for the weight of the package and the distance it is to be shipped then displays the charges.

Weight of Package (in kilograms) | Rate Per 500 Miles Shipped

2 Kg or less $1.10

Over 2 Kg but not more than 6 Kg $2.20

Over 6 Kg but not more than 10 Kg $3.70

Over 10 Kg but not more than 20 Kg $4.80

Input Validation: Do not accept values of 0 or less for the weight of the package. Do not accept weights of more than 20Kg (this is the maximum weight the company will ship,). Do not accept distances of less than 10 miles or more than 3,000 miles. These are the company’s minimum and maximum shipping distances.

Code:
//---------------------------------------------------------------------------
#include <iostream.h>
#include <iomanip>
#include <cstring>
#include <fstream>
#include <utility>
#include <conio.h>
using namespace std;
//---------------------------------------------------------------------------
int main (void)
{
// Assuming Varibles
double wt_package;
double distance;
double charges;
double rate;

// Asking for inputs.
cout << "Please enter the Wt. Package: ";
cin >> wt_package;
cout << "Please enter the distance: ";
cin >> distance;


// Dertermining the price range.
if (wt_package>0.0 || wt_package<20.0)
// if (wt_package<=0.0){
//cout<< "Please enter the wt. of package above 0.0";
//}else
if (wt_package>0.0 && wt_package<2.0){
rate=1.10;
}else
if (wt_package>2.0 && wt_package<6.0){
rate=2.20;
}else
if (wt_package>6.0 && wt_package<10.0){
rate=3.70;
}else
if (wt_package>10.0 && wt_package<20.0){
rate=4.80;
}//else
// if (wt_package>20.0){
// cout<< "Please enter the wt. of package below or equal to 20.0";
//}


// Dertermining the price range.
if (wt_package>10.0 || wt_package<3000.0)

//cout<< "Please enter the distance above 10.0 and below or equal to 3000.0";

if (distance>10.0 && distance<500.0){
distance=1.0;
}else
if (distance>500.0 && distance<1000.0){
distance=2.0;
}else
if (distance>1000.0 && distance<1500.0){
distance=3.0;
}else
if (distance>1500.0 && distance<2000.0){
distance=4.0;
}else
if (distance>2500.0 && distance<=3000.0){
distance=5.0;
}

// calculating
charges = wt_package * rate * distance;

// Displaying the result.
cout << "\nThe charges for shipping is: $" << charges << endl;
cout << endl << endl << endl << "Press Any Key to Continue...";
getch ();
return 0;
}

Last edited by Valmont; 09-25-2005 at 06:40 AM.
subodhgupta1 is offline   Reply With Quote
Old 09-25-2005, 07:36 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
This is a starter:
Code:
#include <iostream>

using namespace std;

//Kg = weight in Kilogram
static const double R1 = 1.10; // 0 < Kg <= 2
static const double R2 = 2.20; // 2 < Kg <= 6
static const double R3 = 3.70; // 6 < Kg <= 10
static const double R4 = 4.80; // 10 < Kg <= 20
//Distance step (in miles)
static const unsigned DIST_STEP = 500;

int main ()
{
  double wt_package;
  cout << "Please enter the Wt. Package (Kg): ";
  cin >> wt_package;
  while(wt_package <=0 || wt_package > 20)
  {
    cout<<"ERROR: Weight too small or too big. Please enter the weight: ";
    cin>>wt_package;
  }
  
  double distance;
  cout << "Please enter the distance (miles): ";
  cin >> distance;
  while(distance < 10 || distance > 3000)
  {
    cout<<"ERROR: distance too small or to big. Please enter the distance: ";
    cin>>distance;
  }
  
  //Determine the shipping rate based on weight.
  //Assumes input validation took place.
  double wt_rate;
  wt_package <= 2 ? wt_rate=R1 : 0;
  wt_package > 2 && wt_package <= 6 ? wt_rate=R2 : 0;
  wt_package > 6 && wt_package <= 10 ? wt_rate=R3 : 0;
  wt_package > 10 ? wt_rate = R4 : 0;
  
  //Determine the shipping rate based on distance.
  unsigned dist_rate = static_cast<unsigned>(distance/DIST_STEP);
  
  //Calculate the total cost.
  double total_cost = dist_rate * wt_rate;
  //Display the total cost.
  cout << total_cost <<endl;
  
  //Prevent console flashing.
  std::cin.get(); //Optional.
  
}
__________________
Valmont is offline   Reply With Quote
Old 09-28-2005, 12:30 AM   #3 (permalink)
subodhgupta1
Registered User
 
Join Date: Sep 2005
Posts: 24
subodhgupta1 is on a distinguished road
I get an incorrect answer

I get an incorrect answer may be due to the following reason:

cout << "Please enter the distance (miles): ";
cin >> distance;
while(distance < 10 || distance > 3000)
{
cout<<"Please enter the distance above 10.0 and below or equal to 3000.0: ";
cin>>distance;
}

if (distance<=500.0){
dist_const=1.0;
}else
if (distance<=1000.0){
dist_const=2.0;
}else
if (distance<=1500.0){
dist_const=3.0;
}else
if (distance<=2000.0){
dist_const=4.0;
}else
if (distance<=2500.0){
dist_const=5.0;
}else
if (distance<=3000.0){
dist_const=6.0;
}
subodhgupta1 is offline   Reply With Quote
Old 09-28-2005, 01:57 AM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,713
redhead is on a distinguished road
This is just too little code, theres no way of knowing what is going on...
I get an incorrect answer ?? what did you expect to get, what do you get...
dist_const=1.0; is the dist_const variable declared as const double or what ?? the name suggests it, and you can't change the value of any const type.
If you take a look at Valmonts solution to that
Code:
  //Determine the shipping rate based on distance.
  unsigned dist_rate = static_cast<unsigned>(distance/DIST_STEP);
You will by your logic see, that it is off by one, given a distance below 500 it results in 0, given a distance between 500 and 1000 gives a value of 1, so if you were to add one to this divission, you'd see the only time it dosn't add up, is when the distance is at an even 100.
You'd might look a bit further up into Valmonts code, and find the way he deals with the wt_rate could be a similar way you could deal with your distance...
Perhaps something like:
Code:
unsigned short int addition = distance%500 ? 1 : 0;
unsigned dist_rate = static_cast<unsigned>((distance/DIST_STEP) +addition);
Or perhaps by turning to the use of the standard C mathmatical ceil() function.
Code:
unsigned dist_rate = static_cast<unsigned>ceil(distance/DIST_STEP);
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 09-28-2005, 08:02 PM   #5 (permalink)
subodhgupta1
Registered User
 
Join Date: Sep 2005
Posts: 24
subodhgupta1 is on a distinguished road
Thank you guys I got it finially working but you guys have been great.
Thanks Valmont and redhead
subodhgupta1 is offline   Reply With Quote
Old 09-28-2005, 08:11 PM   #6 (permalink)
subodhgupta1
Registered User
 
Join Date: Sep 2005
Posts: 24
subodhgupta1 is on a distinguished road
Post Hotel Occupancy

Hotel Occupancy
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 most hotels do not have a thirteenth floor. The loop in this program should skip the entire thirteenth iteration.

Input Validation: Do not accept a value less than one for the number of floors. Do not accept a number less than 10 for the number of rooms on a floor.

This is what I tried and seem to go nowhere
Code:
//---------------------------------------------------------------------------
#include <iostream.h>
#include <iomanip>
#include <cstring>
#include <fstream>
#include <utility>
#include <conio.h>
using namespace std;
//---------------------------------------------------------------------------
int main (void)
{
  // Assuming Varibles
   int no_of_floors;
   int no_of_rooms;
   int no_of_occupied_rooms;
   //int total_no._of_rooms;
   //int total_no._of_occupied_rooms;
   //int total_no._of_unoccupied_rooms;
   //double percentage_of_occupied_rooms;

  	// Asking for inputs.
   cout << "Please enter no. of floors in the building: ";
	cin >> no_of_floors;
   //cout << "Please enter the distance: ";
	//cin >> distance;

   	// Calculating the look
   if (no_of_floors > 0)
   {
        //int floor=1;
        while (no_of_floors <= no_of_floors)
        {
          cout << "Please enter no. rooms in the floor: ";
          cin>> no_of_rooms;
           cout << "Please enter no. occupied rooms in the floor: ";
          cin>> no_of_occupied_rooms;
        }
        ++no_of_floors;
        no_of_rooms = no_of_rooms + no_of_rooms ;
        no_of_occupied_rooms = no_of_occupied_rooms + no_of_occupied_rooms;

   }

   // calculating


	// Displaying the result.
   cout << "Total no. rooms in the floor: " << no_of_rooms << endl;
   cout << "Total no. rooms in the floor: " << no_of_occupied_rooms << endl;
   cout << endl << endl << endl << "Press Any Key to Continue...";
    getch ();
    return 0;
    }

Last edited by redhead; 09-29-2005 at 01:43 AM.
subodhgupta1 is offline   Reply With Quote
Old 09-29-2005, 03:17 AM   #7 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,713
redhead is on a distinguished road
Think about normal loops ie:
Code:
#include <iostream>

int main()
{
  unsigned int no_floors=0, no_rooms=0, no_op_rooms=0, temp=0;
  while(no_floors < 1)
  {
    std::cout << "Enter number of floors in building: ";
    std::cin >> no_floors;
    if(no_floors < 1)
       std::cout << "Error please enter 1 (ONE) or more floors" << std::endl;
  }
  for(int i=1; i <= no_floors; ++i)
  {
    if(i == 13)
      continue; /* skip 13th floor */
    /* bad programming style to use gotos
     * but if we know what we're doing.... 
     */
    start_over:
    std::cout << "Enter number of rooms on " << i << ". floor: ";
    std::cin >> temp;
    if(temp < 10)
    {
      std::cout << "Error you should give 10 or more rooms pr. floor" << std::endl;
      goto start_over;
    }
    no_rooms += temp;
    std::cout << "Enter number of occupied rooms on " << i << ". floor: ";
    std::cin >> temp;
    /* figuring out a way to check for more occupied rooms
     * than the floor has, is left as a user implementation 
     */
    no_op_rooms += temp;
  }
  std::cout << "The calculation is the users implementation" << std::endl;
  return 0;
}
This is just written directly into the composer here, and has not been tested/compiled/anything, if it will work, only god knows.. An even he might be wrong.

If you were to create a combination where it should portrait the number of rooms and occupied rooms pr. floors, then you should use the std::vector type for your no_rooms and no_op_rooms, where every instance in those vectors represents the floors.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 09-29-2005 at 03:55 AM.
redhead is offline   Reply With Quote
Old 10-01-2005, 03:40 PM   #8 (permalink)
subodhgupta1
Registered User
 
Join Date: Sep 2005
Posts: 24
subodhgupta1 is on a distinguished road
Cannot Display percentage

Hi,
The program does everything but calculating percentage, please take a look at the code down below:

//---------------------------------------------------------------------------
#include <iostream.h>
#include <iomanip>
#include <cstring>
#include <fstream>
#include <utility>
#include <conio.h>

using namespace std;
//---------------------------------------------------------------------------

#include <iostream>

int main()
{
unsigned int no_floors=0, no_rooms=0, no_op_rooms=0, temp=0;
int no_unop_rooms;
double pert_occ;

while(no_floors < 1 || no_floors > 13)
{
cout << "Enter number of floors in building: ";
cin >> no_floors;
if(no_floors < 1 || no_floors > 13)
cout << "Error please enter 1 or more floors and less than 13" << endl;
}
for(int i=1; i <= no_floors; ++i)
{
if(i == 13)
continue; /* skip 13th floor */
start_over:
cout << "Enter number of rooms on " << i << ". floor: ";
cin >> temp;
if(temp < 10)
{
cout << "Error you should give 10 or more rooms pr. floor" << endl;
goto start_over;
}
no_rooms += temp;
cout << "Enter number of occupied rooms on " << i << ". floor: ";
cin >> temp;
no_op_rooms += temp;
}

// calculating
no_unop_rooms = no_rooms - no_op_rooms;
pert_occ = (no_op_rooms/no_rooms);

//Displaying the results
cout << endl << "Results-------------------"<<endl;
cout << "The total no. of rooms hotel has: " << no_rooms << endl;
cout << "The total no. of occupied rooms hotel has: " << no_op_rooms << endl;
cout << "The total no. of unoccupied rooms hotel has: " << no_unop_rooms << endl;
cout << "The percentage of rooms that are occupied: " << pert_occ << "%" << endl;
cout << endl << endl << endl << "Press Any Key to Continue...";
getch ();
return 0;
}
subodhgupta1 is offline   Reply With Quote
Old 10-01-2005, 04:55 PM   #9 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Ok, time to play serious. Use the CODE tags and I don't want to see "conio.h" in this forum anymore. That's not part of the ISO C/C++ standard.
__________________
Valmont is offline   Reply With Quote
Old 10-01-2005, 08:13 PM   #10 (permalink)
subodhgupta1
Registered User
 
Join Date: Sep 2005
Posts: 24
subodhgupta1 is on a distinguished road
Ok no problem I will not use....

Hi,
Sorry to use #include <conio.h> but need your help to fix this program. It will not generate the percentage other than 0.
Thanks
subodhgupta1 is offline   Reply With Quote
Old 10-02-2005, 02:20 AM   #11 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,713
redhead is on a distinguished road
Why do you only accept between 1 and 13 floors ? when the description say:
Quote:
NOTE: It is traditional that most hotels do not have a thirteenth floor. The loop in this program should skip the entire thirteenth iteration.
skip is not the same as "reject more than" or "allow only".
If you look at my code, the only part missing there, is what is left in the comments ie. calculating the percentage.
And the basics in calculating percentage in this form is to multiply the number of occupied rooms with 100 and devide that value with the total number of rooms, it's basic math 101.
It's even mentioned in the description
Quote:
The percentage may be calculated by dividing the number of rooms occupied by the number of rooms.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-02-2005, 03:33 AM   #12 (permalink)
subodhgupta1
Registered User
 
Join Date: Sep 2005
Posts: 24
subodhgupta1 is on a distinguished road
Hi Redhead
That's what I did but the program yields 0 what so ever. So like you suggested using vector but I'm not acquainted with vector function so if you could show me that, that will be fantastic.
Thanks
subodhgupta1 is offline   Reply With Quote
Old 10-02-2005, 07:56 AM   #13 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,713
redhead is on a distinguished road
You need to think in the fact that you are dividing int's where the only way to get a double/float value out of that is to convert the result into double/float, else the result will always be what any int type can represent.
Code:
 pert_occ = static_cast<double>(100.0*no_op_rooms/no_rooms);
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-02-2005, 08:42 AM   #14 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,713
redhead is on a distinguished road
Since I'm not here to do your homework, then I'm reluctant to provide you with the answer to your request..
But anyway I made it.. Now your challenge is to explain from line 15 to 55 exactly what is going on.
Code:
#include <iostream>
#include <vector>

int main()
{
  unsigned int no_floors=0, temp=0, counter = 0;
  std::vector <int> no_rooms, no_op_rooms;
  while(no_floors < 1)
  {
    std::cout << "Enter number of floors in building: ";
    std::cin >> no_floors;
    if(no_floors < 1)
       std::cout << "Error please enter 1 (ONE) or more floors" << std::endl;
  }
  for(int i=1; i <= no_floors; ++i)
  {
    if(i == 13)
      continue; /* skip 13th floor */
    /* bad programming style to use gotos
     * but if we know what we're doing.... 
     */
    start_rooms:
    std::cout << "Enter number of rooms on " << i << ". floor: ";
    std::cin >> temp;
    if(temp < 10)
    {
      std::cout << "Error you should give 10 or more rooms pr. floor" << std::endl;
      goto start_rooms;
    }
    no_rooms.push_back(temp);
  start_op_rooms:
    std::cout << "Enter number of occupied rooms on " << i << ". floor: ";
    std::cin >> temp;
    if(i > 13)
      counter = i-2;
    else
      counter = i-1;
    if(no_rooms[counter] < temp)
      {
	std::cout << "Error there can not be more occupied rooms" << std::endl;
	std::cout << "than the floor provides, which is " << no_rooms[counter] << std::endl;
	goto start_op_rooms;
      }
    no_op_rooms.push_back(temp);
  }
  std::cout << "For the hotel, the room statistics are:" << std::endl;
  std::cout << "Floor:\tRooms:\tOccupied:\tFree:\tPercentage:" << std::endl;
  for(int i=0; i < no_rooms.size(); ++i)
    {
      std::cout << "  " << i+1 << ")\t  " << no_rooms[i] /* number of rooms */
		<< "\t  " << no_op_rooms[i]           /* occupied rooms */
		<< "\t\t  " << (no_rooms[i] - no_op_rooms[i])/* free rooms */
		<< "\t  " << static_cast<double>(100.0*no_op_rooms[i]/no_rooms[i])
		<< std::endl;
    }
  return 0;
}
If you wan't the output to look nice and shiny, use your own time to fiddle with it.. I didn't waste 8 years on my PhD to learn how to use set_with(), set_fill() and set_precision() to mangle with the output.

If you're observant, you'll notice a small error in the program, if you can locate it and solve it, then i havn't wasted my time trying to teach you the basics in this assignment.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 11-20-2005, 01:21 PM   #15 (permalink)
subodhgupta1
Registered User
 
Join Date: Sep 2005
Posts: 24
subodhgupta1 is on a distinguished road
Post New Problem please help

Write a C++ program that loads a 101-by-101 array of integers from a data file (provided) . Invoke the “load-array” function to load the array (provided —see below) . Your program then should make use of a function that totals the elements on the middle row and center column of the array, and then returns that sum to the caller. Your program should display the result.

Provide your entire solution.
Code:
#define FILENAME “C:\\data.dat” 
bool Load Array (mt array[] [101]) 
ifstream input_file; 
input file.open (FILENAME) 
if ( input_file) 
cout << “Cannot Open Input Data File.” << endi; 
return false; 
for (mt row = 0; row < 101; ++row) 
for (mt ccl = 0; ccl < 101; ++col) 
input_file >> array[row] [col] 
input file.close () 
return true;
Data of data.dat file:
72
-9
-99
// edit removed, even I know what 10.000 random numbers looks like

Last edited by redhead; 11-20-2005 at 01:45 PM. Reason: added [code] tags, and removed emensly large ammount of input-data
subodhgupta1 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help with first php project sarah31 PHP 10 06-14-2004 09:56 PM
SMS provider that offer SMS shipping world-wide for under 0,05 € ??? gicio Lounge 2 05-28-2003 03:46 AM


All times are GMT -8. The time now is 01:40 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting