Hello, I am pretty new to C++. I received a project today in which I am supposed to create a data file named ./current_inventory which will list about twenty cars, there mileage and price. The user will select from a menu asking if they are looking for a Chevrolet, Ford, Honda, Toyota or Porsche and then enter their maximum mileage and price. I then need to use two boolean functions, get_next_auto and compare_autos to read the next auto from the database and compare it to the desired auto respectively and finally print out how many autos match their desired car requirements. Get_next_auto needs to read values from the input file and into a structure. Compare_autos will compare the vaules in two structures. Will someone please point me in the right direction, I am pretty confused and would really appreciate the help. I know this sounds rudimentary, but I do not even know how I would create a database with each car's mileage, make and price and then get my program to read through them. PLEASE HELP ME! THANK YOU!
Well, you're gonna want to start by looking at fstream. This stands for file stream, which you will need for your file I/O. A text file can act as a database pretty easily, and generally is what is used for small things like this. Things like SQL aren't needed unless you're working with large numbers of entries.
As you read through fstream, you will see there are many functions for file manipulation. I can't really help you much until you post some code that you've started on. Check out my link, give it some good reading, and then give it a shot. I don't care if the code is completely wrong, that gives us something to base some advice off of.
This is all I have so far I have no idea where to go from here and I doubt what I have is right. Here is my data table which I will probably expand on. It is in file ./current_inventory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Chevrolet Malibu 22 22110
Chevrolet Colorado 18 19730
Chevrolet Impala 19 24495
Ford Fiesta 29 13200
Ford Focus 28 16500
Ford Fusion 41 20200
Honda Civic 38 26155
Honda Accord 34 21380
Honda Odyssey 28 28225
Toyota Corolla 27 16130
Toyota Camry 25 21955
Toyota Sienna 19 25060
Porsche Panamera 27 87500
Porsche Boxster 20 56500
Porsche Cayman 21 59200
/*********************\
* cs250a *
* Project 3 *
* This will show all *
* cars matching the *
* user's desired *
* specifications *
\*********************/
#include <iostream>
#include <cstdlib>
#include <fstream>
int main()
{
usingnamespace std;
ifstream in_stream;
ofstream out_stream;
int model
int mileage
int price
in_stream.open("./current_inventory");
if (in_stream.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
out_stream.open("proj3.cc");
if (out_stream.fail())
{
cout << "Output file opening failed.\n";
exit(1);
}
cout << "Please describe the car you are looking for:\n\n";
cout << " 1: Chevrolet ";
cout << " 2: Ford ";
cout << " 3: Honda ";
cout << " 4: Toyota ";
cout << " 5: Porsche ";
cout << "\n";
cout << "Please select your model: "
cin >> model;
cout << "Please enter a meximum acceptable mileage and price. ";
cin >> mileage >> price;
I now need to use get_next_auto and compare_auto(somehow?) to see how many cars from the data match the user's specifications (model,max mileage and price).
Again, I am using fstream but do not know how to do this I do not know how to make a car class or how to use it I'm just showing what I need to do and I need someone to show me the next step, please.
I'm so lost you guys I really need help my projects due in a few days and I haven't been able to do any more. I really just need someone to show me an example of what to do next with what I have. Can someone PLEASE just type a couple lines of example code for how I might want to read in my data?
kapo, I am specifically confused on how to get the data table I have into my program. Like how do I take the user's input and read in each car's type, mileage and price one at a time to compare it? Please show me an example of how to do it because all of these answers so far are just giving me a bunch of vague mixed ideas of things that I have no idea how to do. Everyone keeps telling me you should do this or this or that but that's not really my question, I need to SEE an example of what I am asking. Thank you for your time.