program exits

Hello, any idea why the program closes before allowing me to input? Doesn't let me input a thing, not complete yet, just wanted to figure out before moving on.


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
#include <iostream>
#include <iomanip>
using namespace std;

int main()

{

	                                    
	int weight, distance;


	float a, x, y, z;
	a = 1.10;
	x = 2.20;
	y = 3.70;
	z = 4.80;
	
	
	
	
	
	
	
	                                        cout << "Freight Shipping Rates\n";

	                             cout << "--------------------------------------------------------\n";

	   cout << "5 pounds or less";                         cout.width(39);         cout << "$1.10\n";
	   cout << "Over 5 pounds but not more than 15 ";      cout.width(20);         cout << "$2.20\n";
	   cout << "over 15 pounds but not more than 30";      cout.width(20);         cout << "$3.70\n";
	   cout << "over 30 pounds but not more than 50";      cout.width(20);         cout << "$4.80\n";

	                      cout << "-----------------------------------------------------------------\n";
						  cout << "NOTICE: We do not ship packages over 50 pounds\n";
						  cout << "We do not ship less than 10 miles or more than 3,000 miles\n";

						 

						  cout << "Please enter the weight in pounds of your package";
						  cin >> weight;

						  cout << "Please enter the distance in miles of your shipment";
						  cin >> distance;

						  cout << if (weight weight <= 5) weight             *= a;
						  else if (weight > 5 && weight  <= 15)   weight      *= x;
						  else if (weight > 15 && weight <= 30)   weight     *= y;
						  else if (weight > 30 && weight < 50) weight *= z;


   system("PAUSE");
	return 0;
}
Last edited on
Hello, any idea why the program closes before allowing me to input?
There is really no reason for this.

Take a look at line 46. The compiler complains about the stray cout << and weight.
cout << if (weight weight <= 5) weight *= a;
I don’t think you can “cout” an “if” that way...

...cout.width(39);...
cout.width? :-O

I’d suggest not to declare all variables at the beginning of a function.
It’s far better if you declare them when you really need them.

system("PAUSE"); is Windows only. I’d avoid it.

using namespace std;
Introducing the entire standard namespace can take some surprises along ;-)
I’d avoid it.

The following code based on yours runs:
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
#include <iomanip>
#include <iostream>
#include <limits>


void waitForEnter();


int main()
{
    std::cout << "Freight Shipping Rates\n"
                 "--------------------------------------------------------\n"
                 "5 pounds or less" << std::setw(39) << "$1.10\n"
                 "Over 5 pounds but not more than 15 " << std::setw(20) << "$2.20\n"
                 "over 15 pounds but not more than 30" << std::setw(20) << "$3.70\n"
                 "over 30 pounds but not more than 50" << std::setw(20) << "$4.80\n"
                 "-----------------------------------------------------------------\n"
                 "NOTICE: We do not ship packages over 50 pounds\n"
                 "We do not ship less than 10 miles or more than 3,000 miles\n";

    std::cout << "Please enter the weight in pounds of your package: ";
    int weight {0};
    std::cin >> weight;
    std::cin.ignore(1);

    std::cout << "Please enter the distance in miles of your shipment: ";
    int distance {0}; // std::distance() declared in <iterator>
    std::cin >> distance;
    std::cin.ignore(1);

    double a, x, y, z;
    a = 1.10;
    x = 2.20;
    y = 3.70;
    z = 4.80;
    if      (weight <= 5)                 { weight *= a; }
    else if (weight > 5 && weight  <= 15) { weight *= x; }
    else if (weight > 15 && weight <= 30) { weight *= y; }
    else if (weight > 30 && weight  < 50) { weight *= z; }
    // else... <-- what if weight > 50?
    std::cout << "weight: " << weight << '\n';
    waitForEnter();
    return 0;
}


void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Topic archived. No new replies allowed.