0

I have a project where I need to create a shopping list with checkout functionality. I am trying to create an array using a users input. They supply how many products they are purchasing and I need to use that to define the size of the array.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct shopList {
    double pluCode;
    string product;
    int saleType; // 0 = per unit, 1 = per pound
    double price;
    double inventory;


};

int main(){

    char line[255];
    const int items = 0;
    int n;

    ofstream outfile;
    outfile.open("products.txt");

    cout << "How many items in your checkout: ";
    cin >> items;

    shopList shop[items];

    for (n = 0; n < items; n++) {
        cout << "Enter the PLU code: ";
        cin >> shop.pluCode;
        outfile << shop.pluCode << " ";

        cout << "Enter product name: ";
        cin >> shop.product;
        outfile << shop.product << " ";

        cout << "Enter the price type (0 for per unit, 1 for per pound): ";
        cin >> shop.saleType;
        outfile << shop.saleType << " ";

        cout << "Enter the price of the product: ";
        cin >> shop.price;
        outfile << shop.price << " ";

        cout << "How much will you purchase: ";
        cin >> shop.inventory;
        outfile << shop.inventory << " " << "\n";

    }



    outfile.close();

    ifstream infile;
    infile.open("products.txt");
    infile.getline(line, 255);
    cout << line << endl;
}
6
  • 1
    You can't do that in standard C++. Use std::vector (or possibly allocate using new) Commented Mar 4, 2015 at 20:28
  • Don't use char[255]. Use std::string instead. Commented Mar 4, 2015 at 20:38
  • @MatsPetersson I'm quite sure "can't do that in standard C++" is false. You've said why in your own comment. Commented Mar 4, 2015 at 20:38
  • @PawełStawarz: No, you can't use variable length arrays in C++. There are ALTERNATIVE solutions that solve the same problem. Commented Mar 4, 2015 at 20:50
  • @MatsPetersson I can't agree with you. There's no mention about VLAs in the question itself, you're judging that's the case only because of how the author tried to implement it. The question is "I need to use that [user input] to define the size of the array.", and the answer most certainly isn't "can't do that" :) Commented Mar 4, 2015 at 22:07

1 Answer 1

1

It's possible , you just have to change your declaration like that;

int items = 0 ; cin >> items;

shopList *shop =  new  shopList [items];
Sign up to request clarification or add additional context in comments.

5 Comments

Remember to use delete [] shop; to deallocate the array.
@sexobert How would I change "cin >> shop.pluCode;" to adjust? It is giving me "Error: expression must have class type".
Of course , don't forget to " delete [] shop " after using your array
@AaronHavenar , your variable shop is a pointer to a shopList array. shop is an array, so you must do some thing like that shop[n].pluCode (where i is a counter) instead of shop.pluCode;
@AaronHavenar You have to use this syntax everywhere you want to use your array. shop[n]."witch attributes of your shopList structure" ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.