0

I am working on an assignment where I have to create a program that the user can personalize the options in buying a new car. I have a .txt file with two columns, one column has the price(double), the other column has the option(string). I want to have two parallel arrays that I can use so that the user can enter the string option and a vector will automatically update the price of the car. This is the .txt file and its contents.

5000.0 Leather Seats
1000.0 DVD System
800.0 10 Speakers
1400.0 Navigation System
500.0 CarPlay
500.0 Android Auto
2000.0 Lane Monitoring
800.0 3/36 Warranty
999.0 6/72 Warranty
1500.0 Dual Climate
225.0 Body Side Molding
49.0 Cargo Net
87.0 Cargo Organizer
700.0 450W Audio
1000.0 Heated Seats

Edit: I apologize for not putting enough information, I am new here. So This is the code I have so far:

void optionsLoop();
void printOptions();
char selectModel();

///function prototypes

int main(int argc, char const *argv[]) 
{
    ifstream inStream;
    int menuChoice;
    char model;
    const int SIZE = 30;



inStream.open("options.txt");
    if (inStream.fail())
    {
        cout << "Error opening file";
        exit (0);
    }

 do 
   {
    optionsLoop ();
    cout << "Enter choice: ", cin >> menuChoice; 
    switch (menuChoice)
    {
        case 1: 
        model = selectModel();

        break;

        case 2:
        printOptions();
    }
   }
   while(menuChoice !=6);


    return 0;
}/// main


void optionsLoop()
{
        cout << " " <<endl;
        cout << "Bob Cat Auto Dealership" << endl;
        cout << "Please select choice" << endl;
        cout << "1. Select a model (E, L, X)" << endl;
        cout << "2. Display available options and prices" << endl; 
        cout << "3. Add an option" << endl;
        cout << "4. Remove an option" << endl;
        cout << "5. Cancel Order" << endl;
        cout << "6. Quit" << endl;

}


char selectModel() 
{   

    char model;
    cout << "Enter the model (E, L, X ) : ";
    cin >> model;
    model = toupper(model);
    return model;


}


So I have a file called options.txt that has the two columns, the right column has the name of the option and the left is the price. I use ifstream to read the information into the program.

I would like to read the file as two parallel arrays so I could use it in the rest of my program.

For example for option 1 in the options loop, I need to have the user choose which model he/she wants,then display the starting price. Whatever options the user chooses in option 3 of the menu would increase the price of the car. So if the user types in leather seats, the price would increase by $5000. This is why I need them parallel.

I hope this clarifies things. I am new to arrays and stackoverflow!

  • 2
    What do you mean with parallel arrays? Anyway, please show your effort and tell us where you got stuck as described in [Ask]. – Lukas-T Jun 15 '20 at 18:26
  • 3
    Wouldn't it be better to store each item in a `struct Item { double price; std::string Name; };` and then keep a `std::vector` for the whole file? – Ted Lyngmo Jun 15 '20 at 18:39

1 Answers1

0

Prefer to use std::vector rather than an array.
Prefer to use std::vector<structure> rather than parallel arrays.
Prefer to research the internet for "C++ read space separated arrays".

Here we go:

double some_value;
std::string name;
std::vector<double> value_database;
std::vector<string> name_database;
//...
while (input_file >> some_value)
{
    std::getline(input_file>>skipws, name);
    value_database.push_back(value);
    name_database.push_back(name);
}

I found the std::getline(input_file>>skipws, from searching through StackOverflow. :-)

Edit: Must use arrays
For those instruction classes that must use arrays, here's another method:

double value_again;
std::string name_again;
unsigned int array_capacity = 8U;
double * value_array = new double[array_capacity];
std::string * name_array = new std::string[array_capacity];
unsigned int records_read = 0U;
while (input_file >> value_again)
{
  char separator;
  input >> noskipws >> separator;
  std::getline(input, name_again);
  ++records_read;
  if (records_read > array_capacity)
  {
    unsigned int new_capacity = array_capacity * 2;
    double * larger_value_array = new double [new_capacity];
    std::copy(&value_array[0], &value_array[array_capacity], &larger_value_array[0]);
    delete [] value_array;
    value_array = larger_value_array;
    std::string * larger_name_array = new std::string [new_capacity];
    std::copy(&name_array[0], &name_array[array_capacity], &larger_name_array[0]);
    delete [] name_array;
    name_array = larger_name_array;
    array_capacity = new_capacity;
  }
  value_array[records_read] = value_again;
  name_array[records_read] = name_again;
  ++records_read;
}

The code fragment above continuously checks the records read versus the capacity of the arrays. If the arrays are full, new arrays are allocated, old data copied to new arrays, then old arrays are deleted. This memory management is already performed (behind the scenes) with std::vector.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154