0

Hi there apologise if my question is poorly worded, I'm struggling to find a solution to my problem.

The purpose of my program is to allow the user to enter predefined bar codes that associate with items and a price. The user enters as many barcodes as they want, and when they're done they can exit the loop by pressing "F" and then total price for all the items is displayed.

This is my code so far, I'm very new to programming..

#include <iostream>
#include <iomanip>

using namespace std;

int index_of(int arr[], int item, int n) {
    int i = 0;
    while (i < n) {
            if(arr[i] == item) {
                return i;
            }
            i++;
    }
    return -1;
}

const int SIZE = 10;

int main()
{
    string item [SIZE] = {"Milk", "Bread", "Chocolate", "Towel", "Toothpaste", "Soap", "Pen", "Biscuits", "Lamp", "Battery"};

    int barcode [SIZE] = {120001, 120002, 120003, 120004, 120005, 120006, 120007, 120008, 120009, 120010};

    float price [SIZE] = {10.50, 5.50, 8.00, 12.10, 6.75, 5.20, 2.00, 4.45, 20.50, 10.00};

    cout << "*************************************************************" << endl;
    cout << "WELCOME TO THE CHECKOUT SYSTEM" << endl;
    cout << "Please scan a barcode or manually enter the barcode ID number" << endl;
    cout << "*************************************************************\n" << endl;

    int newBarcode;

    while (true){
        cout << "Please enter a barcode (Type 'F' to finish): ", cin >> newBarcode;
        int index = index_of(barcode, newBarcode, (sizeof(barcode) / sizeof(barcode)[0]));
        cout << "\n>> Name of item: " << item[index] << endl;
        cout << ">> Price of item: \x9C" << setprecision (4)<< price[index] << endl;
        cout << ">> " <<item[index] << " has been added to your basket. \n" << endl;

        float total = 0 + price[index];



        cout << ">> Your current basket total is: \x9C" << setprecision(4) << total << endl;









        /*float total = 0;
        float newtotal = 0;

        price[index] = total;

        total = newtotal;

        cout << ">> " << "Basket total: " << newtotal << endl; */



    }



    return 0;
}

Louis K
  • 11
  • 2

2 Answers2

0

You will need to iterate over all items and add their value to a variable. You can do it the old way:

float sum = 0;
for(int i = 0; i < SIZE; i++) {
    sum += price [i];
}

Or the C++11 way:

float sum = 0;
for(float p : price) {
    sum += p;
}

However I must point out a few important issues with your code:

  1. Your array has a fixed size but user can enter as many entries as he wants. To avoid this issue, use vector. It behaves like array but has dynamic size. Simply use push_back() to add a new element.
  2. Don't use separate containers (arrays) for the same group of objects. It's a bad coding practice. You can define a structure for product which will contain name, barcode and price, then make one container for all of the products.

Edit

I'm sorry, I misunderstood your problem. There are many ways to solve this, the most elegant way is to create a map where key is the bar code and value is your product object or just a price.

map<int, float> priceMap;
priceMap.insert(pair<int, float>([your bar code here], [your price here]))

Afterwards just create a vector of bar codes, fill it with user data and iterate over it sum all prices:

float sum = 0;
for(int b : userBarcodes) {
    sum += priceMap.at(b);
}
  • I tried implementing the c++11 method, but It added the total for all the items in the array, which is 85, I want to find the total only for the items that are entered by the user, for example if they entered 0120001, and then 0120002 the total would be 16 – Louis K Dec 02 '19 at 14:53
  • Yeah, sorry. I now understand your problem. I edited the answer – Dominik Mistera Dec 02 '19 at 15:06
0
  1. You are trying to read from cin into an int. As you decide to put a stopping condition on 'F' input you must read into a string. Then decide what to do with the value. You will need to check if the input is an int or not. You can do it as given here or here.
  2. Or you may change the stopping condition to a less likely integer like -1. And make sure you always read an int into newBarcode.
  3. There are various small errors which are hard to list out. I have changed them in the code below which is implementing point 2 (You have to add the stopping condition).
  4. One of the error or wrong practice is to declare new variables inside a loop. In most cases you can declare the variables outside and change there values in the loop.
  5. I replaced (sizeof(barcode) / sizeof(barcode)[0] with SIZE as the lists are predefined and unchanging. Anyways you should use (sizeof(barcode) / sizeof(barcode[0]) for length calculation.
#include <iostream>
#include <iomanip>

using namespace std;

int index_of(int arr[], int item, int n) {
    int i = 0;
    while (i < n) {
            if(arr[i] == item) {
                return i;
            }
            i++;
    }
    return -1;
}

const int SIZE = 10;

int main()
{
    string item [SIZE] = {"Milk", "Bread", "Chocolate", "Towel", "Toothpaste", "Soap", "Pen", "Biscuits", "Lamp", "Battery"};

    int barcode [SIZE] = {120001, 120002, 120003, 120004, 120005, 120006, 120007, 120008, 120009, 120010};

    float price [SIZE] = {10.50, 5.50, 8.00, 12.10, 6.75, 5.20, 2.00, 4.45, 20.50, 10.00};

    cout << "*************************************************************" << endl;
    cout << "WELCOME TO THE CHECKOUT SYSTEM" << endl;
    cout << "Please scan a barcode or manually enter the barcode ID number" << endl;
    cout << "*************************************************************\n" << endl;

    int newBarcode;
    float total = 0;
    int index;
    while (true){

        cout << "Please enter a barcode (Type -1 to finish): \n";
        cin >> newBarcode;
        while(cin.fail()) {
            cout << "Not an integer" << endl;
            cin.clear();
            cin.ignore(100,'\n'); 
            cin >> newBarcode;
        }
        index = index_of(barcode, newBarcode, SIZE);
        cout << index;
        if (index == -1) {
            cout << "Apologies here for unsupported barcode\n";
            continue;
        } else {
            cout << ">> Name of item: " << item[index] << endl;
            cout << ">> Price of item: " << price[index] << "\n";
            cout << ">> " <<item[index] << " has been added to your basket. \n";

            total = total + price[index];

            cout << ">> Your current basket total is: " << total << "\n";

        }

    }

    return 0;
}

Your question could be more helpful to others if you find out what is wrong with your implementation and ask implementation specific questions which will probably be already answered. Asking what is wrong with my code is not quite specific.