Equation won't solve. Please help!!!

Nov 12, 2020 at 5:46am
Hi. I am working on my final project for class and I'm having trouble with the equation. For some reason, my numbers will not calculate correctly when entered. Can someone please help me? The code is pretty long because it is my first ever project. I am using Visual Studio 2019 for this project. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <math.h>
using namespace std;

int main()

{
    string name;
    cout << "Please enter your name: ";
    getline(cin, name);
    cout << "Hello " << name << ", Welcome to James' Dog Shipping Calculator!!\n";

    int choice,
        miles,
        weight,
        rates{};

    char healthCertificate,
        overWeight;

    cout << "Please answer the following questions.\n";
    cout << "Use Y for Yes and N for No.\n";
    cout << "Is your dog under 75lbs? ";
    cin >> overWeight;
    cout << "Do you have an updated Health Certificate? ";
    cin >> healthCertificate;

    if (overWeight == 'Y')
    {
        if (healthCertificate == 'Y')
        {
            cout << "You may continue to the next step.\n";
            
        }
        else
        {
            cout << "You must have an updated Health Certificate ";
            cout << "for us to ship your dog.\n";
        }
    }
    else 
    {
        cout << "Your dog must be under 75lbs to ship.\n";
        
    }
    

    int shipping_Cost;

    const int TOY_CHOICE = 1,
        TERRIER_CHOICE = 2,
        NONSPORT_CHOICE = 3,
        QUIT_CHOICE = 4;
    
    cout << "\t\tDog Group Menu\n\n"
        << "1. Toy Breed Group\n"
        << "2. Terrier Group\n"
        << "3. Non-Sporting Group\n"
        << "4. Quit the program\n\n"
        << "Enter your choice: ";
    cin >> choice;

    if (choice == TOY_CHOICE)
    {
        cout << "How much does your dog weigh? ";
        cin >> weight;
        while (weight > 75)
        {
            cout << "Your dog must be under 75lbs to ship. \n";

            cout << "How much does your dog weigh? ";
            cin >> weight;
        }
        cout << "Please enter the amount of miles from pickup city to destinated city:";
        cin >> miles;
        if (miles <= 150)
            rates = 3.00;
        shipping_Cost = (miles * rates) + (weight * 3);
        if (miles <= 200)
            rates = 2.50;
        shipping_Cost = (miles * rates) + (weight * 3);
        if (miles <= 250)
            rates = 2.25;
        shipping_Cost = (miles * rates) + (weight * 3);
        if (miles <= 300)
            rates = 2.00;
        shipping_Cost = (miles * rates) + (weight * 3);
        if (miles <= 399)
            rates = 1.75;
        shipping_Cost = (miles * rates) + (weight * 3);
        if (miles >= 400)
            rates = 1.50;
        shipping_Cost = (miles * rates) + (weight * 3);
        cout << "Your shipping charge will be $" << shipping_Cost << " for this trip." << endl;

    }
    else if (choice == TERRIER_CHOICE)
    {
        cout << "How much does your dog weigh?";
        cin >> weight;
        while (weight > 75)
        {
            cout << "Your dog must be under 75lbs to ship. \n";

            cout << "How much does your dog weigh? ";
            cin >> weight;
        }
        cout << "Please enter the amount of miles from pickup city to destinated city:";
        cin >> miles;
        if (miles <= 150)
            rates = 3.00;
        shipping_Cost = (miles * rates) + (weight * 2);
        if (miles <= 200)
            rates = 2.50;
        shipping_Cost = (miles * rates) + (weight * 2);
        if (miles <= 250)
            rates = 2.25;
        shipping_Cost = (miles * rates) + (weight * 2);
        if (miles <= 300)
            rates = 2.00;
        shipping_Cost = (miles * rates) + (weight * 2);
        if (miles <= 399)
            rates = 1.75;
        shipping_Cost = (miles * rates) + (weight * 2);
        if (miles >= 400)
            rates = 1.50;
        shipping_Cost = (miles * rates) + (weight * 2);
        cout << "Your shipping charge will be $" << shipping_Cost << " for this trip." << endl;
    }
    else if (choice == NONSPORT_CHOICE)
    {
        cout << "How much does your dog weigh?";
        cin >> weight;
        while (weight > 75)
        {
            cout << "Your dog must be under 75lbs to ship. \n";

            cout << "How much does your dog weigh? ";
            cin >> weight;
        }
        cout << "Please enter the amount of miles from pickup city to destinated city:";
        cin >> miles;
        if (miles <= 150)
            rates = 3.00;
        shipping_Cost = (miles * rates) + (weight * 2);
        if (miles <= 200)
            rates = 2.50;
        shipping_Cost = (miles * rates) + (weight * 2);
        if (miles <= 250)
            rates = 2.25;
        shipping_Cost = (miles * rates) + (weight * 2);
        if (miles <= 300)
            rates = 2.00;
        shipping_Cost = (miles * rates) + (weight * 2);
        if (miles <= 399)
            rates = 1.75;
        shipping_Cost = (miles * rates) + (weight * 2);
        if (miles >= 400)
            rates = 1.50;
        
        cout << "Your shipping charge will be $" << shipping_Cost << " for this trip." << endl;
    }
    else if (choice == QUIT_CHOICE)
    {
        cout << "Program is ending.\n";
    }
    return 0;
}

Last edited on Nov 12, 2020 at 5:53am
Nov 12, 2020 at 5:52am
1
2
3
4
5
6
        if (miles <= 150)
            rates = 3.00;
        shipping_Cost = (miles * rates) + (weight * 2);
        if (miles <= 200)
            rates = 2.50;
        shipping_Cost = (miles * rates) + (weight * 2);

Perhaps you need some extra braces, to make shipping_Cost part of the if statement.
1
2
3
4
        if (miles <= 150) {
            rates = 3.00;
            shipping_Cost = (miles * rates) + (weight * 2);
        }

Nov 12, 2020 at 6:13am
I entered all the braces and it still won't calculate. Thanks for your input.
Nov 12, 2020 at 6:27am
Show us your latest code.
Just saying you did it right doesn't mean you did.

Also, show some example inputs, actual answers and what you expected the answer to be.
Nov 12, 2020 at 6:44am
Ok. Here is the latest code.

Example: I ask the user for the weight of the dog. They enter 75.
Then I ask the user for miles needed to be traveled. They enter 150.
Since the miles are 150, the rate is $3.00.
shipping_Cost = (miles * rates) + (weight * 3).
For the information giving above, I'm getting $375 when I need to get $675.


#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <math.h>
using namespace std;

int main()

{
string name;
cout << "Please enter your name: ";
getline(cin, name);
cout << "Hello " << name << ", Welcome to James' Dog Shipping Calculator!!\n";

int choice,
miles,
weight,
rates{};

char healthCertificate,
overWeight;

cout << "Please answer the following questions.\n";
cout << "Use Y for Yes and N for No.\n";
cout << "Is your dog under 75lbs? ";
cin >> overWeight;
cout << "Do you have an updated Health Certificate? ";
cin >> healthCertificate;

if (overWeight == 'Y')
{
if (healthCertificate == 'Y')
{
cout << "You may continue to the next step.\n";

}
else
{
cout << "You must have an updated Health Certificate ";
cout << "for us to ship your dog.\n";
}
}
else
{
cout << "Your dog must be under 75lbs to ship.\n";

}


int shipping_Cost;

const int TOY_CHOICE = 1,
TERRIER_CHOICE = 2,
NONSPORT_CHOICE = 3,
QUIT_CHOICE = 4;

cout << "\t\tDog Group Menu\n\n"
<< "1. Toy Breed Group\n"
<< "2. Terrier Group\n"
<< "3. Non-Sporting Group\n"
<< "4. Quit the program\n\n"
<< "Enter your choice: ";
cin >> choice;

if (choice == TOY_CHOICE)
{
cout << "How much does your dog weigh? ";
cin >> weight;
while (weight > 75)
{
cout << "Your dog must be under 75lbs to ship. \n";

cout << "How much does your dog weigh? ";
cin >> weight;
}
cout << "Please enter the amount of miles from pickup city to destinated city:";
cin >> miles;
if (miles <= 150){
rates = 3.00;
shipping_Cost = (miles * rates) + (weight * 3);
}
if (miles <= 200){
rates = 2.50;
shipping_Cost = (miles * rates) + (weight * 3);
}
if (miles <= 250){
rates = 2.25;
shipping_Cost = (miles * rates) + (weight * 3);
}
if (miles <= 300){
rates = 2.00;
shipping_Cost = (miles * rates) + (weight * 3);
}
if (miles <= 399){
rates = 1.75;
shipping_Cost = (miles * rates) + (weight * 3);
}
if (miles >= 400){
rates = 1.50;
shipping_Cost = (miles * rates) + (weight * 3);
}
cout << "Your shipping charge will be $" << shipping_Cost << " for this trip." << endl;

}
else if (choice == TERRIER_CHOICE)
{
cout << "How much does your dog weigh?";
cin >> weight;
while (weight > 75)
{
cout << "Your dog must be under 75lbs to ship. \n";

cout << "How much does your dog weigh? ";
cin >> weight;
}
cout << "Please enter the amount of miles from pickup city to destinated city:";
cin >> miles;
if (miles <= 150){
rates = 3.00;
shipping_Cost = (miles * rates) + (weight * 2);
}
if (miles <= 200){
rates = 2.50;
shipping_Cost = (miles * rates) + (weight * 2);
}
if (miles <= 250){
rates = 2.25;
shipping_Cost = (miles * rates) + (weight * 2);
}
if (miles <= 300){
rates = 2.00;
shipping_Cost = (miles * rates) + (weight * 2);
}
if (miles <= 399){
rates = 1.75;
shipping_Cost = (miles * rates) + (weight * 2);
}
if (miles >= 400){
rates = 1.50;
shipping_Cost = (miles * rates) + (weight * 2);
}

cout << "Your shipping charge will be $" << shipping_Cost << " for this trip." << endl;
}
else if (choice == NONSPORT_CHOICE)
{
cout << "How much does your dog weigh?";
cin >> weight;
while (weight > 75)
{
cout << "Your dog must be under 75lbs to ship. \n";

cout << "How much does your dog weigh? ";
cin >> weight;
}
cout << "Please enter the amount of miles from pickup city to destinated city:";
cin >> miles;
if (miles <= 150){
rates = 3.00;
shipping_Cost = (miles * rates) + (weight * 2);
}
if (miles <= 200){
rates = 2.50;
shipping_Cost = (miles * rates) + (weight * 2);
}
if (miles <= 250){
rates = 2.25;
shipping_Cost = (miles * rates) + (weight * 2);
}
if (miles <= 300){
rates = 2.00;
shipping_Cost = (miles * rates) + (weight * 2);
}
if (miles <= 399){
rates = 1.75;
shipping_Cost = (miles * rates) + (weight * 2);
}
if (miles >= 400){
rates = 1.50;
shipping_Cost = (miles * rates) + (weight * 2);
}
cout << "Your shipping charge will be $" << shipping_Cost << " for this trip." << endl;
}
else if (choice == QUIT_CHOICE)
{
cout << "Program is ending.\n";
}
return 0;
}
Last edited on Nov 12, 2020 at 6:45am
Nov 12, 2020 at 6:50am
You don't need all that repetition.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    cout << "Welcome to The Dog Shipping Calculator\n\n";

    cout << "How much does your dog weigh (lbs)? ";
    double weight;
    cin >> weight;

    if (weight > 75)
    {
        cout << "Your dog must be under 75lbs to ship.\n";
        return 0;
    }

    cout << "Do you have an updated Health Certificate? ";
    char healthCertificate;
    cin >> healthCertificate;
    if (healthCertificate != 'Y' && healthCertificate != 'y')
    {
        cout << "You must have an updated Health Certificate "
             << "for us to ship your dog.\n";
        return 0;
    }

    cout << "Enter the distance in miles from pickup city to destination city: ";
    double miles;
    cin >> miles;

    double rates;
    if      (miles <= 150) rates = 3.00;
    else if (miles <= 200) rates = 2.50;
    else if (miles <= 250) rates = 2.25;
    else if (miles <= 300) rates = 2.00;
    else if (miles <= 399) rates = 1.75;
    else                   rates = 1.50;

    enum {TOY_CHOICE=1, TERRIER_CHOICE=2, NONSPORT_CHOICE=3};
    
    cout << "\nDog Group Menu\n"
        << "\t1. Toy Breed Group\n"
        << "\t2. Terrier Group\n"
        << "\t3. Non-Sporting Group\n"
        << "Enter your choice: ";

    int choice;
    cin >> choice;

    int multiplier;
    if (choice == TOY_CHOICE)
        multiplier = 3;
    else if (choice == TERRIER_CHOICE)
        multiplier = 2;
    else if (choice == NONSPORT_CHOICE)
        multiplier = 2;
    else {
        cout << "That choice is not supported. Quitting.\n";
        return 0;
    }
        
    double shipping_Cost = miles * rates + weight * multiplier;

    cout << fixed << setprecision(2);
    cout << "\nYour shipping charge will be $" << shipping_Cost << " for this trip.\n";

    return 0;
}

Nov 12, 2020 at 7:09am
Thanks. I really appreciate your response. It helped a lot.
Topic archived. No new replies allowed.