Skipping input collection of description

When I run this code it keeps skipping input collection of description. it doesn't matter whether I set part_description as a string array or 2D character array, it'll skip description. has to be 2D char Array per homework assignment.

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
void parts_store()
{
    int S=10, part_no[S], qty[S], i_min, i_max;
    float cost[S], total[S];
    char part_description[100][S];

    for (int i = 0; i<S; i++)
    {
        cout << "Part Number >: ";
        cin.clear();
        cin >> part_no[i];

        cout << "Description >: ";
        cin.clear();
        cin.getline(part_description[i], 100);

        cout << "Unit Cost >: $";
        cin.clear();
        cin >> cost[i];

        cout << "Quantity >: ";
        cin.clear();
        cin >> qty[i];

        total[i] = cost[i] * qty[i];
        cout << "Total Cost = $" << total[i];
     }
     for (int i = 0; i<S; i++)
     {
        int n = (i!=9)?i+1:9;
        i_min = (cost[n]<cost[i])?n:i;
        i_max = (cost[n]>cost[i])?n:i;
     }
     cout << "Least Expensive Product:" << endl;
     cout << " P/N | Description | Cost | Qty | Total Cost" << endl;
     cout << part_no[i_min] << " | " << part_description[i_min] << " | $" << cost[i_min] << " | " << qty[i_min] << " | $" << total[i_min] << endl;
     cout << "Most Expensive Product:" << endl;
     cout << " P/N | Description | Cost | Qty | Total Cost" << endl;
     cout << part_no[i_max] << " | " << part_description[i_max] << " | $" << cost[i_max] << " | " << qty[i_max] << " | $" << total[i_max] << endl;

    //find least expensive AND most expensive
}
> cin >> part_no[i];
This will leave a trailing newline on the input stream.

> cin.getline(part_description[i], 100);
This sees the newline as the first character of input, and claims that's success.

You should read up on cin.ignore().

You should get rid of all those cin.clear() calls, as they're resetting the stream error state.
You should be paying attention to the error state, not ignoring it and carrying on.

Topic archived. No new replies allowed.