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
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.
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!
#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')
returntrue;
if (answer == 'N' || answer == 'n')
returnfalse;
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