using cin.clear and cin.ignore

i am building a marketplace calculator and i need help with writing the logic for seller input data validation.

currently I am using cin.clear and cin.ignore but I'm not exactly sure what it does and I'm not comfortable using code that I found online without knowing that it does.

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
174
175
176
177
178
#include <iostream>
#include <iomanip>


using namespace std;

/*****************Prototypes****************/

void displayMainMenu();

double askSellerForMarketPlace();

void promptForInteger(string, int &, int, int);

void createProducts(string [], int []);

void promptForDouble(string, double &, double, double);

//bool isNum(int);

//double calculateCommission(double, double);
//double sellerEarnings(double, double);
//void displayResults(double, double, double);


/*****************Main Function****************/
int main() {

//    double commissionRate = askSellerForMarketPlace();
//    if (commissionRate > -1) { //sentinel value
//        double price;
//        promptForDouble("Please enter a price", price, 0.01, DBL_MAX);
////        double commission = calculateCommission(price, commissionRate);
////        double earnings = sellerEarnings(price, commission);
////        displayResults(price, commission, earnings);
//    }

    string product_name[1]; // product_name array initialization, initializing array with 1 element
    int product_price[1]; // product_price array initialization, initializing array with 1 element
    createProducts(product_name, product_price);

    return 0;
}


/*****************Headers****************/
// Function to display menu options
void displayMainMenu() {
    cout << endl;
    cout << "............................ MENU ............................" << "\n";
    cout << "." << setw(61) << "." << "\n";
    cout << "." << setw(20) << "1. Mercari" << setw(25) << "3. Tradesy" << setw(16) << "." << "\n";
    cout << "." << setw(61) << "." << "\n";
    cout << "." << setw(20) << "2. Poshmark" << setw(25) << "4. Quit Program" << setw(16) << "." << "\n";
    cout << "." << setw(61) << "." << "\n";
    cout << ".............................................................." << "\n";
    cout << endl;
}

// Function to ask seller to choose marketplace
double askSellerForMarketPlace() {
//    displayMainMenu();
    int marketplaceNum;
    promptForInteger("Please choose a marketplace", marketplaceNum, 1, 4);

    switch (marketplaceNum) {
        case 1:
            return 0.10; // mercari
        case 2:
            return 0.20; // poshmark
        case 3:
            return 0.149; // tradesy
        case 4:
            return -1; // quit program
    }
    return -1;
}

// Function to prompt data type in integer
void promptForInteger(string prompt, int &choice, int low, int high) {
    cout << prompt << ": ";
    cin >> choice;

    if (choice < low) {
        cout << "Value is too low" << endl;
        promptForInteger(prompt, choice, low, high); // using recursion here, calling itself
    } else if (choice > high) {
        cout << "Value is too high" << endl;
        promptForInteger(prompt, choice, low, high); // using recursion here, calling itself
    }
}

//// Function to prompt data type in double
//void promptForDouble(string prompt, double &val, double low, double high) {
//    cout << prompt << ": ";
//    cin >> val;
//
//    if (val < low) {
//        cout << "Value is too low" << endl;
//        promptForDouble(prompt, val, low, high); // using recursion here, calling itself
//    } else if (val > high) {
//        cout << "Value is too high" << endl;
//        promptForDouble(prompt, val, low, high); // using recursion here, calling itself
//    }
//}


/*
// Function to calculate marketplace commission
double calculateCommission(double price, double commissionRate) {
    return price * commissionRate;
}


// Function to calculate seller earnings
double sellerEarnings(double price, double totalCommission) {
    return price - totalCommission;
}
*/

// Function to create two arrays: productName and productPrice
// This function takes in two empty arrays which will be filled at the end of the function
void createProducts(string productName[], int productPrice[]) {
    int productCount = 0, itemPrice = 0, totalSalesPrice = 0, totalCommissionAmount = 0, totalSalesEarnings = 0;
    double commissionRate = 0;
    string itemName;

    displayMainMenu();
    commissionRate = askSellerForMarketPlace();
    while (commissionRate > -1) {
        cout << "Please enter the number of products: ";
        cin >> productCount;

        while (productCount <= 0 && (!(cin >> productCount))) {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(),'\n');
                cout << "INVALID INPUT. Please enter a numeric value greater than 0" << endl;
                cout << "Number of products: ";
            }

        productName = new string[productCount]; // building new productName array
        productPrice = new int[productCount]; // building new productPrice array


        for (int i = 0; i < (productCount); i++) {
            cout << "Please enter the product name: ";
            cin >> itemName;
//            getline(cin, itemName);
//            cin.ignore();
            productName[i] = itemName; // stores new product name element
            cout << "Please enter the product price: ";
            cin >> itemPrice;
            productPrice[i] = itemPrice; // stores new product price element
        }

        cout << endl;

        cout << "Item Name" << "\t\t" << "Item Price" << endl;
        cout << "---------" << "\t\t" << "----------" << endl;
        for (int i = 0; i < (productCount); i++) {
            cout << productName[i] << right << setw(15) << "$" << productPrice[i]
                 << endl; // prints out parallel array
            totalSalesPrice = totalSalesPrice + productPrice[i];
        }

        totalCommissionAmount = totalSalesPrice * commissionRate;
        totalSalesEarnings = totalSalesPrice - totalCommissionAmount;

        cout << endl;
        cout << "TOTAL SALES:  " << "\t\t" << left << setw(3) << "$" << right << setw(9) << totalSalesPrice << endl;
        cout << "TOTAL COMMISSION: " << "\t\t" << left << setw(3) << "$" << right << setw(9) << totalCommissionAmount << endl;
        cout << "TOTAL EARNINGS: " << "\t\t" << left << setw(3) << "$" << right << setw(9) << totalSalesEarnings << endl;

        displayMainMenu();
        commissionRate = askSellerForMarketPlace();

    }
}
1
2
3
4
5
6
        while (productCount <= 0 && (!(cin >> productCount))) {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(),'\n');
                cout << "INVALID INPUT. Please enter a numeric value greater than 0" << endl;
                cout << "Number of products: ";
            }


cin.clear() removes/clears any error flags on cin (common input) so that it can work as normal again.
Then cin ignore skips the numeric limit of characters (2147483647 on my system) or until the next new line. Giving you cin not in an error state waiting for input on the next new line.
Topic archived. No new replies allowed.