# Help with c++ program



## Infinite (Oct 15, 2007)

Hey guys I have my program finished, but when I want to calculate The total shipping charges for the program its always $0.00... here is the code.


```
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int weight;
    
    double distance, Tshipping=0, nshippingdis=0.0;
    
    // amount of shipping cost per 500 miles
    const double onekg = 1.10, twokg = 1.10, threekg = 2.20, fourkg = 2.20, fivekg = 2.20, sixkg = 2.20, 
                 sevenkg = 3.70, eightkg = 3.70, ninekg = 4.80, tenkg = 4.80; 
    
    
    // get desired package weight and distance (in kg and in miles)
    cout << "Please enter the weight of the package you want shipped: ";
    cin >> weight;
    cout << "\nPlease enter the distance the package will be shipped: \n";
    cin >> distance;
    cin.ignore(1, 'n');
    
    
    
    // Will end if weight is less than or equal to 0 OR if weight is greater than 10
    if (weight <= 0 || weight > 10)
       {
               cout << "Sorry please restart program weight must be between 0 and 10.\n";
               cin.get();
               
       }
       
    // Will end if distance is less than 10 OR greater than 3000
    else if ( distance < 10 || distance > 3000)
         {
              cout << "Sorry please restart program distance must be between 10 and 3000.\n";
              cin.get();
              
         }
         
   
    else
    {
    
    // Will now calculate the Total Shipping charges
    switch(weight)
         {
                  nshippingdis = distance / 500;
                  case 1: Tshipping = onekg * nshippingdis;
                  break;
                  
                  nshippingdis = distance / 500;
                  case 2: Tshipping = onekg * nshippingdis;
                  break;
                  
                  nshippingdis = distance / 500;
                  case 3: Tshipping = onekg * nshippingdis;
                  break;
                  
                  nshippingdis = distance / 500;
                  case 4: Tshipping = onekg * nshippingdis;
                  break;
                  
                  nshippingdis = distance / 500;
                  case 5: Tshipping = onekg * nshippingdis;
                  break;
                  
                  nshippingdis = distance / 500;
                  case 6: Tshipping = onekg * nshippingdis;
                  break;
                  
                  nshippingdis = distance / 500;
                  case 7: Tshipping = onekg * nshippingdis;
                  break;
                  
                  nshippingdis = distance / 500;
                  case 8: Tshipping = onekg * nshippingdis;
                  break;
                  
                  nshippingdis = distance / 500;
                  case 9: Tshipping = onekg * nshippingdis;
                  break;
                  
                  nshippingdis = distance / 500;
                  case 10: Tshipping = onekg * nshippingdis;
                  break;
         }
                  cout << fixed << setprecision(2);
                  cout << "\n\nThe total shipping charges is $";
                  cout << Tshipping << endl;
         
    cin.get();
    return 0;
    }
                 
                  
}
```


----------



## ricer333 (Sep 17, 2004)

So, in your huge SWITCH statement you are multiplying onekg by nshippingdis. However, when you first define nshipppingdis you set it to 0.0. You never change the nshippingdis to anything else. If you hadn't set it to 0.0 to start, I would guess that you would get some nasty behavior (large numbers) as that double wouldn't be set.

I would also like to comment that your questions for user input should be handled more gracefully than just telling the user to restart the program. You should give ranges (say 0-10) for weight and (10-3000) for distance. That way the user knows good ranges. But I would have some sort of loop around that logic that keeps asking until the user inputs good values.

A do, while loop is great for this!

do{
ask question to user
}
while(input is not good!)

Good questions and good luck!


----------

