Home > OS >  The receipt have to display the fruit that user chose
The receipt have to display the fruit that user chose

Time:10-12

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

string getItemName(int k) {
    if(k == 0) {
        return "Sunkist Orange";
    } else if(k == 1) {
        return "Strawberry";
    } else if(k == 2) {
        return "papaya";
    } else if(k == 3) {
        return "Star Fruit";
    } else if(k == 4) {
        return "Kiwi";
    }
    return "";
}
    
int main() {
    double prices[] = {2.00, 22.00, 5.00, 6.00, 10.00};
    double total = 0.0;
    string cart[50];
    int key = 0;
    int weight = 0;
    int index = 0;
    cout << "Welcome to Only Fresh Fruit Shop\n\nToday's fresh fruit <Price per Kg>\n";
    cout << "0-Sunkist Orange RM2\n";
    cout << "1-Strawberry RM22\n";
    cout << "2-Papaya RM5\n";
    cout << "3-Star Fruit RM6\n";
    cout << "4-Kiwi RM10\n";

    while (key != -1) {
        double current = 0.0;
        cout << "Enter fruit code <-1 to stop>: " << endl;
        cin >> key;
        if (key == -1) {
            break;
        }

        cout << getItemName(key) << endl;
        cout << "Enter weight <kg> : " << endl;
        cin >> weight;
        current = prices[key]   weight;
        total = total   current;
    }

    cout << "-------------------------------------------------------\nReciept\n";
    for(int i = 0; i < index; i  ) {
        cout << cart[i] << "\n";
    }
    cout << "TOTAL = RM" << total << endl;

    return 0;
}

This is my code so far. The system have to display what fruit the user have chosen at in the receipt. My code is not working on the receipt part. Is there any other way on how to improvise the code to make it simpler? How can I improvise?

CodePudding user response:

At very first you can re-organise your data better:

struct Product
{
    std::string name;
    double price;
};

This struct keeps the data closely related to a single product (fruit in this case) together locally.

You might organise these in an array (preferrably std::array, alternatively raw) making access to simpler – making your getItemName function obsolete entirely. Instead of a static array a std::vector would allow to manage your products dynamically (adding new ones, removing obsolete ones, ...).

You can even use this array to output your data (and here note that your condition in the while loop is redundant; if the inner check catches, the outer one cannot any more as you break before; if the inner one doesn't, the outer one won't either, so prefer a – seeming – endless loop):

std::vector<Product> products({ {"Apple", 2.0 }, { "Orange", 3.0 } });

for(;;)
{
    std::cout << "Welcome ... \n";
    for(auto i = products.begin(); i != products.end();   i)
    {
        std::cout << i - products.begin() << " - " << i->name
                  << " RM " << i-> price << '\n';
    }

    // getting key, exiting on -1

    if(0 <= key && key < products.size()
    {
        // only now get weight!
    }
    else
    {
        std::cout << "error: invalid product number" << std::endl;
    }
}

Now for your cart you might just add indices into the vector or pointers to products – note, though, that these will invalidate if you modify the vector in the mean-time – if you do so you need to consider ways to correctly update the cart as well – alternatively you might just empty it. Inconvenient for the user, but easy to implement…

In any case, such a vector of pointers to products would easily allow to add arbitrary number of elements, not only 50 (at least as much as your hardware's memory can hold...) and would allow for simple deletion as well.

Calculating the full price then might occur only after the user has completed the cart:

// a map allows to hold the weights at the same time...
std::map<Product*, weight> cart;

for(;;)
{
    // ...
    if(0 <= key && key < products.size()
    {
        double weight;
        std::cin >> weight;

        // TODO: check for negative input!
        //       (always count with the dumbness of the user...)
                           

        cart[&products[key]]  = weight;
        // note: map's operator[] adds a new entry automatically,
        // if not existing
    }

Finally you might iterate over the cart, printing some information and calculating total price for the shopping cart:

double total = 0.0;
for(auto& entry : cart) // value type of a map always is a std::pair
{
    std::cout << entry.first->name << "..." << entry.second << " kg\n";

    total  = entry.first->price * entry.second;
    // note: you need MULTIPLICATION here, not
    // addition as in your code!
}

std::cout << "Total price: RM " << total << std::endl;

CodePudding user response:

This should do it whilst staying close to original code, I also improved you're method of gathering price/name a bit, try to catch the out of index exceptions or check if current index is NULL. Good luck!

#include <iostream>
#include <string>
#include <sstream>
#include <vector>;
using namespace std;

std::vector<std::string> itemList = {"Sunkist Orange", "Strawberry", "Papaya", "Star Fruit", "Kiwi"}; 
//If you dont want this to be global put it in the getItemName function

string getItemName(int k) {
    return itemList.at(k);
}

int main() {
    std::vector<double> prices = { 2.00, 22.00, 5.00, 6.00, 10.00 };
    double total = 0.0;
    int key = 0, weight = 0;
    cout << "Welcome to Only Fresh Fruit Shop\n\nToday's fresh fruit <Price per Kg>\n";
    cout << "0-Sunkist Orange RM2\n";
    cout << "1-Strawberry RM22\n";
    cout << "2-Papaya RM5\n";
    cout << "3-Star Fruit RM6\n";
    cout << "4-Kiwi RM10\n";

    while (key != -1) {
        double current = 0.0;
        cout << "Enter fruit code <-1 to stop>: " << endl;
        cin >> key;
        if (key == -1) {
            break;
        }

        cout << getItemName(key) << endl;
        cout << "Enter weight <kg> : " << endl;
        cin >> weight;
        current  = prices.at(key)   weight;
        total  = total   current;

        cout << "-------------------------------------------------------\nReciept\n";
        cout << "Purchased: " << getItemName(key) <<"  "<< "TOTAL = RM" << total << "\n" << endl;
    }

    return 0;
}

CodePudding user response:

I noticed there is a string cart[50] and int index = 0which you did not use throuoght the whole code except printing it at the end of the code. I am guessing that you want to add the fruit into the cart but it seems like you have not done so.

double price[50];
while (key != -1) {
    double current = 0.0;
    cout << "Enter fruit code <-1> to stop: " << endl;
    cin >> key;
    if (key == -1) break;
    cout << getItemName(key) << endl;
    cout << "Enter weight <kg> : " << endl;
    cin >> weight;
    cart[index] = getItemName(key);
    price[index] = prices[key]
    current = prices[key] * weight;
    total  = current;
    index  ;
}
for(int i = 0; i < index; i  ) {
    cout << cart[i] << "     " << price[i] << "\n";
}

I have added some code so that cart[index] = getItemName(key). When you print each element of cart, it will now work. Also, current = prices[key] * weight is the correct one, not addition (unless your rules are different).

on a side note are you malaysian

  • Related