Entering multiple values at once with option to type -1 to quit

Hi. I am new to the forum and not quite sure how this works. In my program, I am asking the user to input 4 values (weight, length, height and width) all on one line. The user has the option to enter -1 to quit. How can I make this work without having the user enter -1 0 0 0 (0's to fill the remaing 3 variables)?

Right now I am using:
cin >> weight >> weight >> length >> height >> width;

I'd appreciate any help. Thanks!
Is there any particular reason it has to be 4 values on one line? This problem would be much simpler if you could have the user input each value separately and check for the escape character after each with a simple if statement?
1
2
3
4
5
6
7
while( std::cin >>  weight && weight != -1 && std::cin >> length >> height >> width )
{

      // ...

}
std::cout << "you entered -1. quitting\n" ;
JLBorges - Thank you so much! This fixed that problem but can you take a look at my code below? I need the first while loop to continue and ask user to enter the next package info if -1 hasn't been entered. Before I added the code you suggested, the loop was repeating but no stopping when user input -1.

#include <iostream>
#include <iomanip>

using namespace std;

// function prototypes
int getGirth(int, int, int);
int getCost(int [], int, int);

int main()
{
const int size = 15;
int weightArray[size] = {1, 2, 3, 5, 7, 10, 13, 16, 20, 25, 30, 35, 40, 45, 50};
float costArray[size] = {1.50, 2.10, 4.00, 6.75, 9.90, 14.95, 19.40, 24.20, 27.30,
31.90, 38.50, 43.50, 44.80, 47.40, 55.20};
int transaction = 1;
int weight = 0;
int accepted = 0;
int rejected = 0;
int s1 = 0;
int s2 = 0;
int s3 = 0;
int girth,
number;

cout << "For each transaction, enter package weight and 3 dimensions. Enter -1 to quit." << endl;

while (weight != -1)
{
cout << "\nEnter package weight and 3 dimensions: ";
while (cin >> weight && weight != -1 && cin >> s1 >> s2 >> s3)
{
if ((weight <= 0) || (s1 <= 0) || (s2 <= 0) || (s3 <= 0))
{
cout << "Error - package weight and dimensions must be larger than 0" << endl;
cout << "Please re-enter transaction" << endl;
}
else
{
girth = getGirth(s1, s2, s3);
cout << "Transaction: " << setw(7) << transaction << endl;
if ((weight > 50) || (s1 > 36) || (s2 > 36) || (s3 > 36) || (girth > 60))
{
cout << "Status: " << setw(12) << "Rejected" << endl;
cout << "Weight: " << setw(12) << weight << endl;
cout << "Cost: " << setw(14) << "-" << endl;
cout << endl;
rejected++;
}
else
{
cout << "Status: " << setw(12) << "Accepted" << endl;
cout << "Weight: " << setw(12) << weight << endl;
number = getCost(weightArray, size, weight);
accepted++;
cout << "Cost: " << fixed << setw(14) << setprecision(2) << costArray[number];
cout << endl;
}
}
transaction++;
}
}
cout << rejected;
cout << accepted;

}

int getGirth(int s1, int s2, int s3)
{
int largest,
g;
if (s1 > s2 && s1 > s3)
largest = s1;
else if (s2 > s3 && s2 > s1)
largest = s2;
else if (s3 > s1 && s3 > s2)
largest = s3;
g = 2*(s1 + s2 + s3 - largest);
return g;
}

int getCost(int w[], int s, int value)
{
int index = 0;
int position = -1;
bool found = false;

while (index < s && !found)
{
if (w[index] >= value)
{
found = true;
position = index;
}
index++;
}
return position;
}
Get rid of the while (weight != -1) loop. And then,

1
2
3
4
5
6
7
8
while ( std::cout << "\nEnter package weight and 3 dimensions (-1 to quit): " &&
        std::cin >> weight && weight != -1 &&
        std::cin >> length >> height >> width )
{
    // ...
    // do your stuff
    // ...
}
JLBorges - Thank you! That worked! :)
If you are familiar with functions:

1
2
3
4
5
6
bool read_package_data( int& weight, int& length, int& width, int& height )
{
    return std::cout << "\nEnter package weight and 3 dimensions (-1 to quit): " &&
           std::cin >> weight && weight != -1 &&
           std::cin >> length >> height >> width ;
}


And then:
1
2
3
4
5
6
while ( read_package_data( weight, length, width, height ) )
{
    // ...
    // do your stuff
    // ...
}
I will definitely try the function but in the mean time I'm having a problem with one of my loops.

Entry:
Trans 1 - 1, 2, 3, 3 (accepted - correct)
Trans 2 - 50, 0 , 10, 10 (prompt to re-enter - correct)
Trans 2 - 50, 10, 10, 10 (rejected - correct)
Trans 3 - 45, 20, 20, 20 (accepted - should reject)

If I enter 45, 20, 20, 20 as a first transaction, it correctly rejects but once prompted to re-enter, the accepted/rejected loop works incorrectly. Any suggestions?

#include <iostream>
#include <iomanip>

using namespace std;

// function prototypes
int getGirth(int, int, int);
int getCost(int [], int, int);

int main()
{
const int size = 15;
int weightArray[size] = {1, 2, 3, 5, 7, 10, 13, 16, 20, 25, 30, 35, 40, 45, 50};
float costArray[size] = {1.50, 2.10, 4.00, 6.75, 9.90, 14.95, 19.40, 24.20, 27.30,
31.90, 38.50, 43.50, 44.80, 47.40, 55.20};
int transaction = 1;
int weight = 0;
int accepted = 0;
int rejected = 0;
int s1 = 0;
int s2 = 0;
int s3 = 0;
int number;
char girth;

cout << "For each transaction, enter package weight and 3 dimensions. Enter -1 to quit." << endl;

while (cout << "\nEnter package weight and 3 dimensions: " &&
cin >> weight && weight != -1 && cin >> s1 >> s2 >> s3)
{
if ((weight <= 0) || (s1 <= 0) || (s2 <= 0) || (s3 <= 0))
{
cout << "Error - package weight and dimensions must be larger than 0" << endl;
cout << "Please re-enter transaction" << endl;
}
else
{
girth = getGirth(s1, s2, s3);
cout << "Transaction: " << setw(7) << transaction << endl;
if ((weight > 50) || (s1 > 36) || (s2 > 36) || (s3 > 36) || (girth > 60))
{
cout << "Status: " << setw(12) << "Rejected" << endl;
cout << "Weight: " << setw(12) << weight << endl;
cout << "Cost: " << setw(14) << "-" << endl;
cout << endl;
rejected++;
}
else
{
cout << "Status: " << setw(12) << "Accepted" << endl;
cout << "Weight: " << setw(12) << weight << endl;
number = getCost(weightArray, size, weight);
accepted++;
cout << "Cost: " << fixed << setw(14) << setprecision(2) << costArray[number];
cout << endl;
}
transaction++;
}
}

cout << "Number of rejected packages: " << rejected << endl;
cout << "Number of accepted packages: " << accepted << endl;

}

int getGirth(int s1, int s2, int s3)
{
int largest,
g;
if (s1 > s2 && s1 > s3)
largest = s1;
else if (s2 > s3 && s2 > s1)
largest = s2;
else if (s3 > s1 && s3 > s2)
largest = s3;
g = 2*(s1 + s2 + s3 - largest);
return g;
}

int getCost(int w[], int s, int value)
{
int index = 0;
int position = -1;
bool found = false;

while (index < s && !found)
{
if (w[index] >= value)
{
found = true;
position = index;
}
index++;
}
return position;
}
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
#include <iostream>
#include <iomanip>
#include <algorithm>

// function prototypes
int getGirth( int, int, int );
int getCost( const int[], int, int );

bool read_package_data( int& weight, int& length, int& width, int& height );
bool valid( int weight, int length, int width, int height );
bool over_sized( int weight, int length, int width, int height );

int main()
{
    const int size = 15;
    const int weightArray[size] = {1, 2, 3, 5, 7, 10, 13, 16, 20, 25, 30, 35, 40, 45, 50};
    const double costArray[size] = {1.50, 2.10, 4.00, 6.75, 9.90, 14.95, 19.40, 24.20, 27.30,
                                    31.90, 38.50, 43.50, 44.80, 47.40, 55.20 };
    int transaction = 0 ;
    int weight = 0;
    int length = 0;
    int width = 0;
    int height = 0;
    int accepted = 0;
    int rejected = 0;

    std::cout << "For each transaction, enter package weight and 3 dimensions. Enter -1 to quit.\n" ;

    while ( read_package_data( weight, length, width, height ) )
    {
        ++transaction ;
        if ( valid( weight, length, width, height ) )
        {
            const bool accept = !over_sized( weight, length, width, height ) ;

            if(accept) ++accepted ;
            else ++rejected ;

            std::cout << "Status: " << std::setw(12) << ( accept ? "Accepted" : "Rejected" )
            << "\nWeight: " << std::setw(12) << weight << '\n' ;

            if(accept)
            {
                const double cost = costArray[ getCost( weightArray, size, weight ) ] ;
                std::cout << "Cost: " << std::fixed << std::setw(14) << std::setprecision(2) << cost << '\n' ;
            }
        }

        else // ! valid( weight, length, width, height )
        {
            std::cout << "Error - package weight and dimensions must be larger than 0\n"
            "Please re-enter transaction\n" ;
        }
    }

    std::cout << "Number of rejected packages: " << rejected << '\n'
              << "Number of accepted packages: " << accepted << '\n' ;
}

bool read_package_data( int& weight, int& length, int& width, int& height )
{
    std::cout << "\nEnter package weight and 3 dimensions (-1 to quit): " ;
    return std::cin >> weight && weight != -1 && std::cin >> length >> height >> width ;
}

bool valid( int weight, int length, int width, int height )
{ return weight > 0 && length > 0 && width > 0 && height > 0 ; }

bool over_sized( int weight, int length, int width, int height )
{
    const int girth = getGirth( length, width, height );
    return ( weight > 50 ) || ( length > 36 ) || ( width > 36 ) || ( height > 36 ) || ( girth > 60 ) ;
}

int getGirth( int length, int width, int height )
{
    // http://en.cppreference.com/w/cpp/algorithm/max
    return 2 * ( length + width + height - std::max( { length, width, height } ) );
}

int getCost( const int w[], int s, int value )
{
    for( int index = 0 ; index < s ; ++index ) if( w[index] >= value ) return index ;
    return s-1 ; // max possible cost
}
Topic archived. No new replies allowed.