formatted data

Hey guys need help realy fast , i am young engineer student, i have to finish my program tomorrow. I need to make 2 programms, first one should make a text file which contains 10. different cars, i should show its brand, gear(manual,automatical), cars year,and registration number. i have already done that.

The second program should search for cars. I need to find cars with typing manual or automatical. it need to show automatical gear cars if i write automatical or if i write manual, it should show manual cars. I have 24 hours from right now could anybody help me?
I have to make it in dev c++.

Last edited on
Have anything written down?

You read from the file and check the strings.
So far it depends on how you've written the first program ie how the format of how the data is saved from the file. your program will read from the file line by line so your data in the file would look something like this:

Car1, Brand, Gear, year,
Car2,Brand, Gear, Year,


once your program reads back the file once it identifies that a kewyword manual or automatic it then displays that line of string.

Thats one way how to do it.
If you add some code to show that would help;
I have to make it in dev c++.

I'm afraid with dev-cpp you'd be fighting with one hand tied behind your back but given your time constraints its probably unwise to switch to Code::Blocks or Visual Studio 2017 now
Anyway, consider using a struct:
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
# include <iostream>
# include <string>
# include <vector>
# include <algorithm>
# include <fstream>
# include <sstream>

struct Car
{
    std::string m_brand;
    bool m_hasGear;
    size_t m_year;
    std::string m_regNum;

    //now overload ctor with these data members
};
//overload input operator >> for Car
//overload output operator << for Car

int main()
{
    std::vector<Car> carsVec{};
    //use emplace_back() to create 10 Car instances in-situ within carsVec;
    //use overloaded >> operator to write carsVec to file
    //for searching manual/automatic cars you could either do it from the file or ...
    // ... read all the Car objects back from file into another vector and ...
    // use std::find_if() on the second vector
    //either way you'd probably need std::istringstream to read the file
}

If this approach is of interest research all unfamiliar terms thoroughly and, only then, come back here with specific questions if you have any
Last edited on
Hello arturszm,

First you will have to know how the data file is written before you can read the file. Since you have not shown what the data file looks like or any attempt you have made on the second program. All I can suggest what you can do.

I am thinking of a struct to hold the data for each car and a vector of these structs to make searching easier.

Until I know what you have done it is hard to go any farther.

Hope that helps,

Andy
Topic archived. No new replies allowed.