project

how to create a file named "Mobiledata.txt"containing information of 20 mobiles ,includes brand,model,price,color and usage time ?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Mobile
{
private:
string brand;
string model;
int price;
public:
mobile()
mobile(brand,model,color,...)
int getprice()
};
int main ()
{
public:
string brand,model,color;
int price,years
int i;
mobile arr[20];
ifstream in;
in.open("mobiledata.txt");
for (i=0;i<20;20;i++)
}


struggling this part
Last edited on
Have a look at this tutorial on file I/O:

http://www.cplusplus.com/doc/tutorial/files/

Can you be more specific? What, exactly, are you having trouble with?
Hello jeancy,

Welcome to the forum.

Creating the file is not that difficult, figuring out where to place the file so the program can find it can be hard.

First think about what type of data you will be storing, i.e., ints, doubles, strings or something else. The order can make reading the file very easy or very hard. Sometimes it is easier to put a string at the end of a line of input data. Then you might have to consider if your data can be separated with a space or would a coma work better.

In this case I might consider everything as a string with each field separated by comas and use the string function "stod" to convert "price" to a double. Just a thought for now until I have more to work with like an actual sample of your input file and what code you have started with.

Hope that helps

Andy
Are you trying to create a program that lets you input these values and then places the information into a file? For example creating a class called mobilePhone and then using all the variables above into this class. Then you would need 20 instances of that class each with its own data. Is this what you want to do?
yes sir
Hints:

Mobile.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <fstream>
#include <iostream>
#include <string>


class Mobile {
public:
    Mobile() = default;
    Mobile(const std::string& brand_arg, const std::string& model_arg,
           double price_arg);
    std::string brand;
    std::string model;
    double price {0.0};
    friend std::ostream& operator<<(std::ostream& os, const Mobile& obj);
    friend std::ofstream& operator<<(std::ofstream& ofs, const Mobile& obj);
    friend std::ifstream& operator>>(std::ifstream& ifs, Mobile& obj);
};



Mobile.cpp:
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
#include <fstream>
#include <iostream>
#include <string>
#include "Mobile.h"


Mobile::Mobile(const std::string& brand_arg, const std::string& model_arg,
               double price_arg)
    : brand {brand_arg}, model {model_arg}, price {price_arg}
    {}


std::ofstream& operator<<(std::ofstream& ofs, const Mobile& obj)
{
    ofs << obj.brand << ' ' << obj.model << ' ' << obj.price << '\n';
    return ofs;
}


std::ostream& operator<<(std::ostream& os, const Mobile& obj)
{
    return ( os << "Brand name: " << obj.brand 
                << "; model name: " << obj.model
                << "; price: " << obj.price << '\n');
}


std::ifstream& operator>>(std::ifstream& ifs, Mobile& obj)
{
    ifs >> obj.brand >> obj.model >> obj.price;
    return ifs;
}



main.cpp:
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
#include <fstream>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
#include "Mobile.h"


const std::string datafile("datafile.txt");


void waitForEnter();


int main()
{
    std::vector<Mobile> mobiles { {"Orange", "VeryUgly2017", 131.30},
                                  {"Somesongs", "ReadyToBurn", 666.00}, 
                                  {"MotorOliver", "BePatient", 99.99} };
    std::ofstream letsave(datafile);
    for(const auto& a : mobiles) { letsave << a; }
    letsave.close();
    mobiles.clear();
    std::ifstream letsread(datafile);
    Mobile tmpmobile;
    while(letsread >> tmpmobile) {
        mobiles.push_back(tmpmobile);
    }
    letsread.close();
    for(const auto& a : mobiles) { std::cout << a; }

    waitForEnter();
    return 0;
}


void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Topic archived. No new replies allowed.