Problem with a program that involves arrays

Alright, so my assignment wants a program that displays the shipping charge for the amount of items that a customer orders (based off of a table in the book. for example, if the customer orders 10 or fewer items than the shipping charge is 15). The assignment specifies that it wants the max amount of items (basically the thresholds for each shipping charge) in a one-dimensional array, and the shipping charges in a one-dimensional parallel array. The program has to allow the user to input the number of items, and then to display the appropriate shipping charge. I also have to use a sentinel value to end the program.

This is what I have so far:
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
#include <iostream>
using namespace std;

int main ()
{
    //declare arrays and variables
    int maxOrders[4]   = {10, 50, 100, 99999};
    int shipCharge[4]  = {15, 10, 5, 0};
    int orderAmt       = 0;
    
    //get order amount
    cout << "Number of items ordered "
         << "(negative number to end): ";
    cin >> orderAmt;
    
    //locate position of shipping charge amount
    while (orderAmt >= 0)
    {
    int sub = 0;
    for (int sub = 0; sub < 4; sub +=1)
        if (maxOrders[sub] > orderAmt)
        
        //end if
    //end for
    
    if (sub <= maxOrders[10]);
    cout << "The shipping charge is: " <<
    shipCharge[sub];
    
    if (sub > maxOrders[10] && sub <= maxOrders[50]);
    cout << "The shipping charge is: " <<
    shipCharge[sub];
    
    if (sub > maxOrders[50] && sub <= maxOrders[100]);
    cout << "The shipping charge is: " <<
    shipCharge[sub];
    
    if (sub > maxOrders[100] && sub <= maxOrders[99999]);
    cout << "The shipping charge is: " <<
    shipCharge[sub];
    }  //end while

    system("pause");
    return 0;
}   //end of main function 


The problem is that when I run the program and input a number it just freaks out. Can someone point me in the right direction? And yeah, I'm aware that the code is probably a mess. I'm in an online course so I'm basically teaching myself. Thanks for any help!
your while statement has no ending clause.. meaning it will run continuously until you stop it manually..
Try:
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
#include <iostream>
using namespace std;

int main ()
{
    //declare arrays and variables
    int maxOrders[4]   = {10, 50, 100, 99999};
    int shipCharge[4]  = {15, 10, 5, 0};
    int orderAmt       = 0;
    
    //get order amount
    cout << "Number of items ordered "
         << "(negative number to end): ";
    cin >> orderAmt;
    
    //locate position of shipping charge amount
    while (orderAmt >= 0)
    {
    int sub = 0;
    for (int sub = 0; sub < 4; sub +=1)
        if (maxOrders[sub] > orderAmt)
        
        //end if
    //end for
    
    if (sub <= maxOrders[10]);
    cout << "The shipping charge is: " <<
    shipCharge[sub];
    
    if (sub > maxOrders[10] && sub <= maxOrders[50]);
    cout << "The shipping charge is: " <<
    shipCharge[sub];
    
    if (sub > maxOrders[50] && sub <= maxOrders[100]);
    cout << "The shipping charge is: " <<
    shipCharge[sub];
    
    if (sub > maxOrders[100] && sub <= maxOrders[99999]);
    cout << "The shipping charge is: " <<
    shipCharge[sub];
	cout << "Number of items ordered "
        << "(negative number to end): ";
    cin >> orderAmt;
    }  //end while
    system("pause");
    return 0;
} 


I added in lines 41-43 :)
Yesss....an ending clause definitely helps there. D'oh.

Thanks so much for that!

However I'm still not quite getting the correct output, and I think it has something to do with all of my stupid cout statements (the program just states the wrong shipping charge four times when I test it). Are lines 26-40 even the correct way to go about writing this?

EDIT:

I changed the code if-else statements, but I'm still getting four answers when I run the program. Anyone know what I'm missing?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (sub <= maxOrders[10])
    cout << "The shipping charge is: " <<
    shipCharge[sub];
    
    else if (sub > maxOrders[10] && sub <= maxOrders[50])
    cout << "The shipping charge is: " <<
    shipCharge[sub];
    
    else if (sub > maxOrders[50] && sub <= maxOrders[100])
    cout << "The shipping charge is: " <<
    shipCharge[sub];
    
    else (sub > maxOrders[100] && sub <= maxOrders[99999]);
    cout << "The shipping charge is: " <<
    shipCharge[sub];
	
Last edited on
Well first off. You have a ; after the last else. Which means, the output underneath it will always be displayed.
This is what I used, compiled with Mingw g++. You might have to remove the std::endl.

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

int main ()
{
    //declare arrays and variables
    int maxOrders[4]   = {10, 50, 100, 99999};
    int shipCharge[4]  = {15, 10, 5, 0};
    int orderAmt       = 0;
    
    //get order amount
    cout << "Number of items ordered "
         << "(negative number to end): "<<std::endl;
    cin >> orderAmt;
    
    //locate position of shipping charge amount
    while (orderAmt >= 0)
    {
    
    if (orderAmt <= maxOrders[0])
	{
		cout << "The shipping charge is: " <<shipCharge[0]<<std::endl;
	}
    
    else if (orderAmt <= maxOrders[1])
	{
		cout << "The shipping charge is: " <<shipCharge[1]<<std::endl;
	}
    
    else if (orderAmt <= maxOrders[2])
	{
		cout << "The shipping charge is: " <<shipCharge[2]<<std::endl;
	}
    
    else if (orderAmt <= maxOrders[3])
	{
		cout << "The shipping charge is: " <<shipCharge[3]<<std::endl;
	}
	cout << "Number of items ordered "
        << "(negative number to end): "<<std::endl;
    cin >> orderAmt;
    }  //end while
    system("pause");
    return 0;
} 


PS: It appears as if the shipping gets cheaper as items increase, if this is not the case change line 8 to:
int shipCharge[4] = {0,5,10,15};

what compiler are you using?
That is better code.

@op, you should really be using { } braces for the if statements. Yeah I know you don't have to, but it makes it easier on everyone and yourself.
The code above is not very robust - if you enter anything other than a number the program goes hay wire - try implementing a switch statement, with a default clause to catch stupid user input. That's what i can say to improve it.
I'm using Dev-C++ 4.9.9.2. Your program does run under my compiler, didn't need to remove all the std::endl;

Ah, okay. I see where I went wrong here (lol, everywhere: my conditions, lack of brackets..) It seems like I tend to over think the wrong things. sigh.

And yeah, according to the table the shipping gets cheaper as the number of items increase.

Thanks so much for your help, you're the best.
Topic archived. No new replies allowed.