I'm new and have little knowledge of what I'm doing

My problem is I need to read from a txt and using 3 files that are 2 cpps and 1 header file to read that data in the txt into class variables that are private and then sort and sort and output them into seperate txts based on if their prices is greater then 40000. any advice or help would be greatly appreciated the code below is simple as was just trying to get one value to output so far.
Car.h
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
#ifndef CAR_H
#define CAR_H
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream lis;
class Cars{
        
        private:
        string make;
        string model;
        int year;
        int price;
        
        public:
        void setmake(string maketype); 
        void setmodel(string modeltype);
        void setyear(int yeartype);
        void setprice(int pricetype);
        
        string getmake();
        string getmodel();
        int getyear();
        int getprice();
        
        
        
    };
    #endif
    

sec.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 <iostream>
#include <fstream>
#include <string>
#include "Car.h"
using namespace std;
void Cars::setmake(string maketype){
  make=maketype;
  lis.open("list.txt");
  lis>>maketype;
  lis.close();
  };
void Cars::setmodel(string modeltype){
  model=modeltype;
};
void Cars::setyear(int yeartype){
  year=yeartype;
};
void Cars::setprice(int pricetype){
  price=pricetype;
};
string Cars::getmake(){
    return make;
}
string Cars::getmodel(){
    return model;
}
int Cars::getprice(){
    return price;
}
int Cars::getyear(){
    return year;
}

main.cpp
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <fstream>
#include <string>
#include "Car.h"
using namespace std;
int main(){
    Cars t1;
    cout<<t1.getmake();
    return 0;
}








I assume the make is the "one value" you're trying to output as a test.
The string make is initialized as an empty string when you create your Car object (main.cpp line 7).
Then, you call getmake and print out that empty string.

You need to
(1) call setmake somewhere in your calling code, before you call getmake.
(2) get rid of your global variable ifstream lis. It's only going to cause you trouble. Are you sure that you want to be opening a file inside of setmake? It seems like it would be better to open that in main, and then call the appropriate set methods.

e.g.
1
2
3
void Cars::setmake(string maketype){
  make=maketype;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    Cars t1;

    std::ifstream f("list.txt");
    if (!f)
        cout << "error opening list.txt\n;"

    std::string make;
    f >> make;
    t1.setmake(make);

    std::string other_attribute;
    f >> other_attribute;

    cout << t1.getmake() << endl;
}
Last edited on
Your response solved so many hours of struggling, thank you!
Topic archived. No new replies allowed.