Please Help debug, first time serializing into file

Getting alot of bugs, first off, in my output its all 1's when it should be a mix of 1's and 0's, and second off litterally nothing is happening with my file, even if I do something like file << "Im screwed becuase this is due in an hour and a half"; ---- its comepletely EMPTY

Please help
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cctype>
using namespace std;

const int SIZE = 3;

struct Customer {
    string customerID;
    int streamMovies;
    int streamingTV;
    int multipleLines;
    double monthlyCharges;
	bool validID(){
		return (customerID.size() == 4 && isdigit (customerID.at(1)) && isdigit (customerID.at(2)) &&
		isdigit (customerID.at(3)) && isupper (customerID.at(0)));
	}
	bool validStreamMovies(){
		return streamMovies == 0 || 1;
	}
	bool validStreamTV(){
		return streamingTV ==  0 || 1;
	}
	bool validMultipleLines(){
		return multipleLines == 0 || 1;
	}
	bool validMonthlyCharges(){
		return monthlyCharges > -1;
	}
};

void populateStruct(Customer&);
void writeToCustomerFile(Customer&, ostream&);

// ***************** MAIN *******************
int main()
{
	Customer cust[SIZE]; //use array of customers so you can fill information in loop
	for(int i = 0; i < SIZE; i++){
		cout << "Enter information for Customer # " << i+1 << ": " << endl;
		populateStruct(cust[i]);
		cout << endl;
	}
	
	ofstream file;
	file.open ("Customer.txt");
	if(!file.is_open()){
		cout << "could not open file";
		return 1;
	}
	
	file << "fuck this";
	
	cout << "Customer ID" << setw(20) << "Streaming Movies" << setw(20) << "Streaming TV" << 
	setw(20) << "Multiple Lines" << setw(20) << "Monthly Charges\n" << " \n" << "Writing to file 'Customer.txt'..." << endl;
	
	
	file << "Customer ID" << setw(20) << "Streaming Movies" << setw(20) << "Streaming TV" << 
	setw(20) << "Multiple Lines" << setw(20) << "Monthly Charges" << endl;
	
	for(int i = 0; i < SIZE; i++){
		writeToCustomerFile(cust[i], file);
		writeToCustomerFile(cust[i], cout);
		/*file << cust[i].customerID << setw(20) << cust[i].validStreamMovies() << setw(20) << cust[i].validStreamTV() << setw(20) 
		<< cust[i].validMultipleLines() << setw(20) << "$" << cust[i].monthlyCharges << endl;*/
	}

	file.close();
	
	
	
}

void writeToCustomerFile(Customer &a, ostream& file){ //writing the structs into the arrays
	file << a.customerID << setw(20) << a.validStreamMovies() << setw(20) << a.validStreamTV() << setw(20) 
	<< a.validMultipleLines() << setw(20) << "$" << a.monthlyCharges << endl;
}

void populateStruct (Customer  &a){
	
	bool valid = false;
	char answer;
	
	while(!valid){
		cout << "Customer ID: ";
		cin >> a.customerID;
		valid = a.validID();
		if(!valid){
			cout << "Please enter a uppercase letter followed by 3 integers" << endl;
		}
	}
	
	valid = false;
	while(!valid){
		cout << "Streaming Movies: ";
		cin >> answer;
		if (answer == 'Y' || answer =='y'){
			a.streamMovies = 0;
			valid = a.validStreamMovies();
		}
		else if (answer == 'N' || answer == 'n'){
			a.streamMovies = 1;
			valid = a.validStreamMovies();
		}
		else{
			cout << "Please enter Y or N" << endl;
		}
	}
	valid = false;
	while(!valid){
		cout << "Streaming TV: ";
		cin >> answer;
		if (answer == 'Y' || answer == 'y'){
			a.streamingTV = 0;
			valid = a.validStreamTV();
		}
		else if (answer == 'N' || answer == 'n'){
			a.streamingTV = 1;
			valid = a.validStreamTV();
		}
		else{
			cout << "Please enter Y or N" << endl;
		}
	}
	valid = false;
	while(!valid){
		cout << "Multiple Lines: ";
		cin >> answer;
		if (answer == 'Y' || answer == 'y'){
			a.multipleLines = 0;
			valid = a.validMultipleLines();
		}
		else if (answer == 'N' || answer =='n'){
			a.multipleLines = 1;
			valid = a.validMultipleLines();
		}
		else{
			cout << "Please enter Y or N" << endl;
		}
	}
	valid = false;
	while (!valid){
		cout << "Monthly Charges: ";
		cin >> a.monthlyCharges;
		valid = a.validMonthlyCharges();
		if(!valid){
			cout << "Please enter a number greater than or equal to 0" << endl;
		}
	}
}
It's all 1's because expressions like this are always true.

return streamMovies == 0 || 1;

Removing the "|| 1" from everything would be a start.
wouldnt it always be 0? I want it to change based on my input
https://en.cppreference.com/w/cpp/language/operator_precedence
Here's your code with added brackets and implied operations

return (streamMovies == 0) || (1 != 0);
It really doesn't matter what's in the variable, the right hand side of the OR is guaranteed to evaluate to true, so the whole thing is true.

Maybe you want
return (streamMovies == 0) || (streamMovies == 1);
I litterally did that and its still just couting 1
1
2
3
4
5
6
7
8
9
	bool validStreamMovies(){
		return (streamMovies == 0) || (streamMovies == 1);
	}
	bool validStreamTV(){
		return (streamingTV ==  0) || (streamingTV == 1);
	}
	bool validMultipleLines(){
		return (multipleLines == 0) || (multipleLines == 1);
	}
What can I say.

Your while(!valid) ensure that you'll only ever store a 0 or a 1, then you're complaining that it only ever stores a 0 or a 1.

You're not outputting the values of streamMovies, streamingTV and multipleLines. You're outputting whether those are valid.
Last edited on
Your streaming section codes does not make much sense. If the answer is 'y' then the member variable is set to 0 and if 'n' it's set to 1 ??? 0 usually means false and 1 true. But the member variable is an int so expected to have values other than 1 or 0???

Also, the .validstream/lines member functions checks the member variable and returns true or false if the applicable .stream member variable is either 0 or 1. But you know this already as the member variable has just been set to 0 or 1 - so .validxxx() will always return true!

The code can just be (not tried):

1
2
3
4
5
6
7
8
9
10
11
12
13
        valid = false;
	while (!valid) {
		cout << "Streaming TV: ";
		cin >> answer;
		if (answer == 'Y' || answer == 'y') {
			a.streamingTV = 1;
			valid = true;
		} else if (answer == 'N' || answer == 'n') {
			a.streamingTV = 0;
			valid = true;
		} else
			cout << "Please enter Y or N\n";
	}


Then remove the .validstream/lines() functions.

The stream write then becomes:

1
2
file << a.customerID << setw(20) << a.StreamMovies << setw(20) << a.StreamTV << setw(20) 
	<< a.MultipleLines << setw(20) << "$" << a.monthlyCharges << '\n; 

Maybe something like:

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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cctype>

using std::setw;

const size_t SIZE { 3 };

struct Customer {
	std::string customerID;
	bool streamMovies {};
	bool streamingTV {};
	bool multipleLines {};
	double monthlyCharges {};

	bool validID() {
		return (customerID.size() == 4 && std::isdigit(customerID[1]) && std::isdigit(customerID[2]) &&
			std::isdigit(customerID[3]) && std::isupper(customerID[0]));
	}

	bool validMonthlyCharges() {
		return monthlyCharges > -1;
	}
};

void populateStruct(Customer&);
void writeToCustomerFile(Customer&, std::ostream&);
void header(std::ostream&);
bool check(const std::string&);

int main() {
	Customer cust[SIZE] {};

	for (size_t i {}; i < SIZE; ++i) {
		std::cout << "Enter information for Customer # " << i + 1 << "\n";
		populateStruct(cust[i]);
		std::cout << '\n';
	}

	std::ofstream file("Customer.txt");

	if (!file.is_open())
		return (std::cout << "could not open file\n"), 1;

	header(std::cout);
	header(file);

	for (int i = 0; i < SIZE; i++) {
		writeToCustomerFile(cust[i], file);
		writeToCustomerFile(cust[i], std::cout);
	}
}

void header(std::ostream& os) {
	os << "Customer ID" << setw(20) << "Streaming Movies" << setw(20) << "Streaming TV" <<
		setw(20) << "Multiple Lines" << setw(20) << "Monthly Charges\n" << '\n';
}

void writeToCustomerFile(Customer& a, std::ostream& file) {
	file << std::boolalpha << a.customerID << setw(20) << a.streamMovies << setw(20) << a.streamingTV << setw(20)
		<< a.multipleLines << setw(20) << "$" << a.monthlyCharges << '\n';
}

bool check(const std::string& prm) {
	for (char answer {}; true; ) {
		std::cout << prm;
		std::cin >> answer;

		if (answer == 'Y' || answer == 'y')
			return true;

		if (answer == 'N' || answer == 'n')
			return false;

		std::cout << "Please enter Y or N\n";
	}
}

void populateStruct(Customer& a) {
	for (bool valid {}; !valid; ) {
		std::cout << "Customer ID: ";
		std::cin >> a.customerID;

		if (valid = a.validID(); !valid)
			std::cout << "Please enter a uppercase letter followed by 3 integers\n";
	}

	a.streamMovies = check("Streaming Movies: ");
	a.streamingTV = check("Streaming TV: ");
	a.multipleLines = check("Multiple Lines: ");

	for (bool valid {}; !valid; ) {
		std::cout << "Monthly Charges: ";
		std::cin >> a.monthlyCharges;

		if (valid = a.validMonthlyCharges(); !valid)
			std::cout << "Please enter a number greater than or equal to 0\n";
	}
}



Enter information for Customer # 1
Customer ID: A123
Streaming Movies: y
Streaming TV: y
Multiple Lines: y
Monthly Charges: 345

Enter information for Customer # 2
Customer ID: B234
Streaming Movies: y
Streaming TV: n
Multiple Lines: y
Monthly Charges: 345

Enter information for Customer # 3
Customer ID: C345
Streaming Movies: n
Streaming TV: y
Multiple Lines: n
Monthly Charges: 567

Customer ID    Streaming Movies        Streaming TV      Multiple Lines    Monthly Charges

A123                true                true                true                   $345
B234                true               false                true                   $345
C345               false                true               false                   $567

Topic archived. No new replies allowed.