Help with easy Project

Jan 26, 2012 at 8:26pm
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!
Jan 27, 2012 at 12:23am
http://www.cplusplus.com/reference/iostream/fstream/
Have you even tried to work on this yourself yet?
Jan 27, 2012 at 3:00am
No because I don't even know where to start. That's why I'm on here.
Jan 27, 2012 at 3:08am
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.

-Biscuit
Jan 27, 2012 at 5:06am
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


And here is my program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*********************\
* cs250a   *
* Project 3           *
* This will show all  *
* cars matching the   *
* user's desired      *
* specifications      *
\*********************/
#include <iostream>
#include <cstdlib>
#include <fstream>

int main()
{
        using namespace 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).
Jan 28, 2012 at 5:20am
biscuit? anyone? help please
Jan 28, 2012 at 5:31am
Line 15 should be on line 12.
Jan 28, 2012 at 7:49am
yeah I know but do you know how to do the next step, what I am actually asking for?
Jan 28, 2012 at 1:56pm
maybe you should make a car class. could save you a lot of time.
Last edited on Jan 28, 2012 at 1:56pm
Jan 28, 2012 at 5:00pm
^^ I agree with this. Classes can make life pretty easy
Jan 28, 2012 at 6:27pm
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.
Jan 28, 2012 at 10:43pm
if you dont know how to make a class then you should use functions . what part are you confused about? (specifically)
Last edited on Jan 28, 2012 at 10:43pm
Jan 29, 2012 at 3:49am
according to your project description you need a struct to store the car info in, so create a struct http://www.cplusplus.com/doc/tutorial/structures/
Jan 29, 2012 at 8:59am
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?
Jan 29, 2012 at 9:03am
Bit off topic here, is there any point in making a class, then passing the values into a struct for file output... Or is it worth just using a struct?
Jan 29, 2012 at 9:04am
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.
Topic archived. No new replies allowed.