Help with reading and writing to files

Mar 1, 2017 at 11:49pm
So the program I am trying to create is as follows.

Schedule for jobs. I need to be able to enter for example

Customer
Job Name
Front (This is an Int)
Back (This is an Int)
Due Date (Monday, Tuesday, Wednesday, Thursday, Friday)


Etc....

Then I want the program to to ask what day to show the jobs that are on the schedule for that day. Then show a list of jobs for that day that will show the details.

I'm having trouble figuring out the best way to do this, should I write each job to its own file, or all one or what.

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 <fstream>
#include <stdlib.h>
using namespace std;


int main () {
	ifstream inFile;
	inFile.open("list.txt");

	//Check for Error
	if (inFile.fail ()) {
		cerr << "File not found" << endl;
		
		ofstream inFile;
		inFile.open ("list.txt");
		cout << "File created" << endl;

		inFile << "First Line." << endl << "Second Line.";
		inFile.close ();
	}
	else {
		cout << "File found" << endl;
	}


	return 0;
}
Last edited on Mar 2, 2017 at 12:01am
Mar 2, 2017 at 2:24am
you could set up a PrintJobs struct (or class) along the following lines and overload the stream insertion << and extraction >> operators for writing to and reading from output and input streams respectively. You can write to a .txt file and read into a std::vector<PrintJobs>. Then finding a list of jobs for any given day would be a matter of searching the vector elements by their m_days data-member:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

enum class Days {Mon = 1, Tue, Wed, Thu, Fri, Sat, Sun};

struct PrintJobs
{
    std::string m_customer;
    std::string m_jobName;
    int m_front;
    int m_back;
    Days m_day;
};
Mar 2, 2017 at 12:30pm
when and how would I use "struct PrintJobs". Couldn't I just define customer, jobname, front, back, and day in the int main?
Mar 2, 2017 at 12:45pm
show some (psuedo)code for your preferred approach to help understand the relative advantage/disadvantage of either approach. particularly, is the printJob info already in a file or will it be first input by hand from the program and then saved to file which is then read back into the program and searched? in general i'd veer towards using the struct but you might have something else in mind for which your way could be superior
Mar 2, 2017 at 1:04pm
display menu (enterPrintjobs, showPrintjobs)

if enterPrintjobs
ask for customerName, jobName, etc...

if showPrintjobs
ask what day you want to see jobs for
show a list of print jobs (shown below, without the dashes)




Customer Name---------Job Name------------------Front-------Back---------Day

John Doe -----------------School Shirts -------------1------------2------------M



Kind of like that.
Last edited on Mar 2, 2017 at 1:07pm
Mar 2, 2017 at 2:33pm
I shall get into trouble if I do your entire homework for you (and it's not going to help you anyways) so I'm going to get you started with a reasonable enough push and you'll have to fill in some of the blanks yourself or at least show a good effort in those directions: things that remain after below program:
(a) input validation steps, (b) formatted header row for print function, (c) reading and writing to files (which would be the main part) (d) part (c) could also do with overloading the stream extraction operator >> (e) for extra credit, investigate what would be the best way to record the day of the week variable - earlier I had suggested enum, and I think you can find something better than what I've got below. Good luck!
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <iomanip>

struct PrintJobs
{
    std::string m_customer = " ";
    std::string m_jobName = " ";
    int m_front = 0;
    int m_back = 0;
    int m_day = 1;
};
std::ostream& operator << (std::ostream& os, const PrintJobs& pJ)
{
    os.width(10);
    os << std::left << pJ.m_customer;
    os.width(10);
    os << std::left << pJ.m_jobName;
    os.width(10);
    os << std::left << pJ.m_front;
    os.width(5);
    os << std::left << pJ.m_back;
    os.width(5);
    os << std::left << pJ.m_day;
    return os;
}

int main()
{
    bool fQuit = false;
    std::vector<PrintJobs> jobs{};
    while (!fQuit)
    {
        std::cout << "1. Enter job \t2. Show all jobs \t3. Search jobs by day \t4. Quit";
        std::cout << "\nEnter your choice \n";
        int choice{};
        std::cin >> choice;//input validation required
        std::cin.ignore();
        switch (choice)
        {
            case 1:
            {
                PrintJobs temp{};
                std::cout << "Customer name \n";
                getline(std::cin, temp.m_customer);
                std::cout << "Job name \n";
                getline(std::cin, temp.m_jobName);
                std::cout << "Front \n";
                std::cin >> temp.m_front;
                std::cout << "Back \n";
                std::cin >> temp.m_back;
                std::cout << "Enter day number (Mon(1), Tue(2), Wed(3), Thu(4), Fri(5), Sat(6), Sun(7)) \n";
                std::cin >> temp.m_day;
                std::cin.ignore();
                jobs.push_back(std::move(temp));
            }
            break;
            case 2:
            {
                if(jobs.size() == 0)
                {
                    std::cout << "No jobs to display \n";
                }
                else
                {
                    for (const auto& elem : jobs)
                    {
                        std::cout << elem << "\n";
                    }
                }
            }
            break;
            case 3:
            {
                if(jobs.size() == 0)
                {
                    std::cout << "No jobs to search \n";
                }
                else
                {
                    std::cout << "Enter day number to search: (Mon(1), Tue(2), Wed(3), Thu(4), Fri(5), Sat(6), Sun(7)) \n";
                    int daySearch{};
                    std::cin >> daySearch;
                    std::cin.ignore();
                    std::cout << "Searching ... \n";
                    bool match = false;
                    for (const auto& elem : jobs)
                    {
                        if (elem.m_day == daySearch)
                        {
                            std::cout << elem << "\n";
                            match = true;
                        }
                    }
                    if(match == false)
                    {
                        std::cout << "No matches were found \n";
                    }
                }
            }
            break;
            case 4:
            fQuit = true;
            std::cout << "Goodbye \n";
            break;
            default:
            std::cout << "Incorrect choice, try again \n";
            break;
        }
    }
}
Mar 2, 2017 at 4:07pm
Hey, I really appreciate the help. Could you do me one favor? Could you go back through and add some comments explaining what going on, that way I can better understand.
Mar 2, 2017 at 4:24pm
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <string>
#include <vector>
#include <utility>//std::move()
#include <iomanip>//print output formatting

struct PrintJobs
{
    std::string m_customer = " ";
    std::string m_jobName = " ";
    int m_front = 0;
    int m_back = 0;
    int m_day = 1;
};
std::ostream& operator << (std::ostream& os, const PrintJobs& pJ)//overload operator << for printing objects
{
    os.width(10);//set width
    os << std::left << pJ.m_customer;//align left
    os.width(10);
    os << std::left << pJ.m_jobName;
    os.width(10);
    os << std::left << pJ.m_front;
    os.width(5);
    os << std::left << pJ.m_back;
    os.width(5);
    os << std::left << pJ.m_day;
    return os;
}

int main()
{
    bool fQuit = false;
    std::vector<PrintJobs> jobs{};
    while (!fQuit)
    {
        std::cout << "1. Enter job \t2. Show all jobs \t3. Search jobs by day \t4. Quit";
        std::cout << "\nEnter your choice \n";
        int choice{};
        std::cin >> choice;//input validation required so that user doesn't enter non int's, char, space etc
        std::cin.ignore();//removes the '\n' left behind in the input stream
        switch (choice)
        {
            case 1:
            {
                PrintJobs temp{};
                std::cout << "Customer name \n";
                getline(std::cin, temp.m_customer);//read string from cin into member variable
                std::cout << "Job name \n";
                getline(std::cin, temp.m_jobName);
                std::cout << "Front \n";
                std::cin >> temp.m_front;
                std::cout << "Back \n";
                std::cin >> temp.m_back;
                std::cout << "Enter day number (Mon(1), Tue(2), Wed(3), Thu(4), Fri(5), Sat(6), Sun(7)) \n";
                std::cin >> temp.m_day;
                std::cin.ignore();
                jobs.push_back(std::move(temp));//places the temp object created into the vector<PrintJobs>
            }
            break;
            case 2:
            {
                if(jobs.size() == 0)//make sure we do have some jobs to display, etc
                {
                    std::cout << "No jobs to display \n";
                }
                else
                {
                    for (const auto& elem : jobs)//C++11 range loop feature, google if unsure
                    {
                        std::cout << elem << "\n";
                    }
                }
            }
            break;
            case 3:
            {
                if(jobs.size() == 0)
                {
                    std::cout << "No jobs to search \n";
                }
                else
                {
                    std::cout << "Enter day number to search: (Mon(1), Tue(2), Wed(3), Thu(4), Fri(5), Sat(6), Sun(7)) \n";
                    int daySearch{};
                    std::cin >> daySearch;
                    std::cin.ignore();
                    std::cout << "Searching ... \n";
                    bool match = false;
                    for (const auto& elem : jobs)
                    {
                        if (elem.m_day == daySearch)
                        {
                            std::cout << elem << "\n";
                            match = true;
                        }
                    }
                    if(match == false)
                    {
                        std::cout << "No matches were found \n";
                    }
                }
            }
            break;
            case 4:
            fQuit = true;
            std::cout << "Goodbye \n";
            break;
            default:
            std::cout << "Incorrect choice, try again \n";
            break;
        }
    }
}
Mar 2, 2017 at 11:13pm
So I added in some variables, now when I enter a new job and i get to "flash y/n" It freaks out after I type y or n.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <iomanip>
using namespace std;

struct PrintJobs {
	string m_customer = " ";
	string m_jobName = " ";
	int m_front = 0;
	int m_back = 0;
	int m_day = 1;
	int m_quanity = 0;
	bool m_flash = false;
	char m_type;
};
ostream& operator << (ostream& os, const PrintJobs& pJ) {
	os.width (10);
	os << left << pJ.m_customer;
	os.width (15);
	os << left << pJ.m_jobName;
	os.width (10);
	os << left << pJ.m_quanity;
	os.width (10);
	os << left << pJ.m_front;
	os.width (5);
	os << left << pJ.m_back;
	os.width (10);
	os << left << pJ.m_flash;
	os.width (15);
	os << left << pJ.m_type;
	os.width (5);
	os << left << pJ.m_day;
	return os;
}

int main () {
	bool fQuit = false;
	vector<PrintJobs> jobs{};
	while (!fQuit) {
		cout << "1. Enter job \t2. Show all jobs \t3. Search jobs by day \t4. Quit";
		cout << "\nEnter your choice \n";
		int choice{};
		cin >> choice;
		cin.ignore ();
		switch (choice) {
		case 1:
		{
			PrintJobs temp{};
			cout << "Customer name \n";
			getline (cin, temp.m_customer);
			cout << "Job name \n";
			getline (cin, temp.m_jobName);
			cout << "Quanity \n";
			cin >> temp.m_quanity;
			cout << "Front \n";
			cin >> temp.m_front;
			cout << "Back \n";
			cin >> temp.m_back;
			cout << "Flash y/n \n";
			cin >> temp.m_flash;
			cout << "Type \n";
			cin >> temp.m_type;
			cout << "Enter day number (Mon(1), Tue(2), Wed(3), Thu(4), Fri(5), Sat(6), Sun(7)) \n";
			cin >> temp.m_day;
			cin.ignore ();
			jobs.push_back (move (temp));
		}
		break;
		case 2:
		{
			if (jobs.size () == 0) {
				cout << "No jobs to display \n";
			}
			else {
				for (const auto& elem : jobs) {
					cout << elem << "\n";
				}
			}
		}
		break;
		case 3:
		{
			if (jobs.size () == 0) {
				cout << "No jobs to search \n";
			}
			else {
				cout << "Enter day number to search: (Mon(1), Tue(2), Wed(3), Thu(4), Fri(5), Sat(6), Sun(7)) \n";
				int daySearch{};
				cin >> daySearch;
				cin.ignore ();
				cout << "Searching ... \n";
				bool match = false;
				for (const auto& elem : jobs) {
					if (elem.m_day == daySearch) {
						cout << elem << "\n";
						match = true;
					}
				}
				if (match == false) {
					cout << "No matches were found \n";
				}
			}
		}
		break;
		case 4:
			fQuit = true;
			cout << "Goodbye \n";
			break;
		default:
			cout << "Incorrect choice, try again \n";
			break;
		}
	}
}
Mar 3, 2017 at 8:47pm
I still haven't figured out why its freaking out after I added that variable.
Mar 4, 2017 at 12:07am
@gunnerfunner

Im trying to make the jobs show as a list instead of having to hit enter to view each one. This is my current code. I'm trying to make sense of it but I can't figure out how to do that.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <iomanip>
using namespace std;

struct PrintJobs {
	string m_customer = " ";
	string m_jobName = " ";
	int m_front = 0;
	int m_back = 0;
	int m_day = 1;
	int m_quanity = 0;
	//bool m_flash;
	char m_type;
};
ostream& operator << (ostream& os, const PrintJobs& pJ) {
	os.width (23);
	os << left << pJ.m_customer;
	os.width (26);
	os << left << pJ.m_jobName;
	os.width (13);
	os << left << pJ.m_quanity;
	os.width (10);
	os << left << pJ.m_front;
	os.width (10);
	os << left << pJ.m_back;
	//os.width (10);
	//os << left << pJ.m_flash;
	os.width (15);
	os << left << pJ.m_type;
	os.width (10);
	os << left << pJ.m_day;
	return os;
}

int main () {
	bool fQuit = false;
	vector<PrintJobs> jobs{};
	while (!fQuit) {
		cout << "1. Enter job \t2. Show all jobs \t3. Search jobs by day \t4. Quit";
		cout << "\nEnter your choice \n";
		int choice{};
		cin >> choice;//input validation required
		cin.ignore ();
		switch (choice) {
		case 1:
		{
			PrintJobs temp{};
			cout << "Customer name \n";
			getline (cin, temp.m_customer);
			cout << "Job name \n";
			getline (cin, temp.m_jobName);
			cout << "Quanity \n";
			cin >> temp.m_quanity;
			cout << "Front \n";
			cin >> temp.m_front;
			cout << "Back \n";
			cin >> temp.m_back;
			//cout << "Flash y/n \n";
			//cin >> temp.m_flash;
			cout << "Type \n";
			cin >> temp.m_type;
			cout << "Enter day number (Mon(1), Tue(2), Wed(3), Thu(4), Fri(5), Sat(6), Sun(7)) \n";
			cin >> temp.m_day;
			cin.ignore ();
			jobs.push_back (move (temp));
			system ("PAUSE");
			system ("CLS");
		}
		break;
		case 2:
		{
			if (jobs.size () == 0) {
				cout << "No jobs to display \n";
				system ("PAUSE");
				system ("CLS");
			}
			else {
				for (const auto& elem : jobs) {
					system ("CLS");
					cout << "Customer Name     " << "     Job Name" << endl;
					cout << elem << endl << endl;
				    
					system ("PAUSE");
					system ("CLS");
				}
			}
		}
		break;
		case 3:
		{
			if (jobs.size () == 0) {
				cout << "No jobs to search \n";
				system ("PAUSE");
				system ("CLS");
			}
			else {
				cout << "Enter day number to search: (Mon(1), Tue(2), Wed(3), Thu(4), Fri(5), Sat(6), Sun(7)) \n";
				int daySearch{};
				cin >> daySearch;
				cin.ignore ();
				cout << "Searching ... \n";
				bool match = false;
				for (const auto& elem : jobs) {
					if (elem.m_day == daySearch) {
						cout << elem << "\n";
						match = true;
					}
				}
				if (match == false) {
					cout << "No matches were found \n";
					system ("PAUSE");
					system ("CLS");
				}
			}
		}
		break;
		case 4:
			fQuit = true;
			cout << "Goodbye \n";
			break;
		default:
			cout << "Incorrect choice, try again \n";
			break;
		}
	}
}
Mar 4, 2017 at 2:35am
This from previous post would show all jobs at once:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
case 2:
            {
                if(jobs.size() == 0)//make sure we do have some jobs to display, etc
                {
                    std::cout << "No jobs to display \n";
                }
                else
                {
                    for (const auto& elem : jobs)//C++11 range loop feature, google if unsure
                    {
                        std::cout << elem << "\n";
                    }
                }
            }
Mar 4, 2017 at 2:41am
That code showed one, then you have to click enter, then it shows the next.

This code sorta works but is it the right way to do it?


1
2
3
4
5
6
7
8
9

for (const auto & elem : jobs) {
			system ("CLS");	
			cout << "Customer Name     " << "     Job Name" << endl;

			for (int x = 0; x != jobs.size (); ++x) {
			cout << jobs[x] << endl;
			}
Mar 4, 2017 at 3:27am
you're mixing up various ways of reading a vector's data:
(a) range-loop (C++11) which is of the style for (const auto& elem : jobs) etc
(b) the traditional int/size_t based loop such as for (size_t x = 0; x < jobs.size(); ++x) etc
(c) there's also the iterator based style such as for (auto itr = jobs.cbegin(); itr != jobs.cend(); ++itr) etc. Here you also need to dereference so the element of the vector is *(itr), not itr itself

Pick one style and apply it throughout
Also you have cout << "Customer Name " << " Job Name" << endl; within the loop so this line will be printed for each element, place it just before the function call to print the vector

make sure there are more than 1 jobs in the vector and try removing the system ("CLS")

Topic archived. No new replies allowed.