December2000
asked on
C++ Structs and Passing to functions : Code Review
Ok Experts!
How can I optimize my code and make it more sophisticated and efficient? Any advice or suggestions?
How can I optimize my code and make it more sophisticated and efficient? Any advice or suggestions?
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// Declaration of the Customer structure
struct Customer
{
string name; // Name
string address; // Address
string city; // City
string state; // State
string zip; // ZIP code
string phone; // Phone number
double balance; // Account balance
string lastPay; // Date of last payment
};
// Function prototypes
void getInfo(Customer &);
void showInfo(Customer);
int main()
{
// Constant for the number of accounts
const int NUM_ACCOUNTS = 20;
// Array of Customer structures
Customer account[NUM_ACCOUNTS];
int maxCust = 0; // Subscript of next account to enter
int choice; // User's choice from a menu
int cust;
do
{
// Display a menu.
cout << "1. Enter new account information\n";
cout << "2. Change account information\n";
cout << "3. Display all account information\n";
cout << "4. Exit the program\n\n";
// Get the user's choice.
cout << "Enter your choice: ";
cin >> choice;
// Validate the choice.
while (choice < 1 || choice > 5)
{
cout << "Please enter 1, 2, 3, 4 or 5.\n";
cout << "Enter your choice: ";
cin >> choice;
}
// Perform the selected operation.
switch (choice)
{
// Enter new account information.
case 1:
cin.get();
getInfo(account[maxCust]);
cout << "You have entered information"
<< "for customer number ";
cout << maxCust << endl;
maxCust++;
break;
// Change an existing account.
case 2:
// Get the customer number.
cout << "Customer number: ";
cin >> cust;
// Validate the customer number.
while (cust < 0 || cust >= maxCust)
{
cout << "ERROR: Invalid Customer Number\n";
cout << "Enter a valid customer number: ";
cin >> cust;
}
// Show the selected account.
showInfo(account[cust]);
// Get the new account information.
cin.get();
getInfo(account[cust]);
break;
// Display all account information.
case 3:
cin.get();
for (int count = 0; count < maxCust; count++)
{
showInfo(account[count]);
cout << "Press enter to continue...";
cin.get();
}
break;
}
} while (choice != 4);
return 0;
}
//********************************************************
// Function getInfo *
// This function gets account information from the user *
// and stores it in the parameter c, which is a Customer *
// structure reference variable. *
//********************************************************
void getInfo(Customer &c)
{
// Get the customer name
cout << "\nCustomer name: ";
getline(cin, c.name);
while (c.name.empty())
{
cout << "You must enter a name.\n";
getline(cin, c.name);
}
// Get the customer's address.
cout << "Customer address: ";
getline(cin, c.address);
while (c.address.empty())
{
cout << "You must enter an address.\n";
getline(cin, c.address);
}
// Get the customer's city.
cout << "City: ";
getline(cin, c.city);
while (c.city.empty())
{
cout << "You must enter a city.\n";
getline(cin, c.city);
}
// Get the customer's state.
cout << "State: ";
getline(cin, c.state);
while (c.state.empty())
{
cout << "You must enter a state.\n";
getline(cin, c.state);
}
// Get the customer's ZIP code.
cout << "ZIP code: ";
getline(cin, c.zip);
while (c.zip.empty())
{
cout << "You must enter a ZIP code.\n";
getline(cin, c.zip);
}
// Get the customer's phone number.
cout << "Telephone: ";
getline(cin, c.phone);
while (c.phone.empty())
{
cout << "You must enter a "
<< "telephone number.\n";
getline(cin, c.phone);
}
// Get the account balance.
cout << "Account balance: ";
cin >> c.balance;
while (c.balance < 0)
{
cout << "Please enter 0 or geater "
<< "for account balance.\n";
cin >> c.balance;
}
// Get the date of the last payment.
cin.get();
cout << "Date of last payment: ";
getline(cin, c.lastPay);
while (c.lastPay.empty())
{
cout << "You must enter the date "
<< "of the last payment.\n";
getline(cin, c.lastPay);
}
}
//************************************************************
// Function showInfo *
// This function displays the customer's name, address, *
// account balance, and date of last payment. The information*
// is passed into the c structure. *
//************************************************************
void showInfo(const Customer c)
{
cout << fixed << showpoint << setprecision(2);
cout << "Customer name: " << c.name << endl;
cout << "Customer address: " << c.address
<< endl;
cout << "City: " << c.city << endl;
cout << "State: " << c.state << endl;
cout << "Zip: " << c.zip << endl;
cout << "Telephone: " << c.phone << endl;
cout << "Account balance: $" << c.balance
<< endl;
cout << "Date of last payment: " << c.lastPay
<< endl << endl;
}
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Minor errors in previous post
// Declaration of the Customer structure
struct Customer
{
string name; // Name
string address; // Address
string city; // City
string state; // State
string zip; // ZIP code
string phone; // Phone number
double balance; // Account balance
string lastPay; // Date of last payment
Customer(){};
Customer(Customer&& other)
: name(std::move(other.name) ),
address(std::move(other.ad dress)),
city(std::move(other.city) ),
state(std::move(other.stat e)),
zip(std::move(other.zip)),
phone(std::move(other.phon e)),
lastPay(std::move(other.la stPay)),
balance(other.balance)
{
};
// Also implement default move operator
const Customer& operator=(Customer&& other)
{
if(&(*this) != &other)
{
name = (std::move(name)),
address = (std::move(address)),
city = (std::move(city)),
state = (std::move(state)),
zip = (std::move(zip)),
phone = (std::move(phone)),
lastPay = (std::move(lastPay)),
balance = other.balance;
}
return *this;
}
};
ostream& operator<<(ostream& out, const Customer& c)
{
out << fixed << showpoint << setprecision(2);
out << "Customer name: " << c.name << endl;
out << "Customer address: " << c.address
<< endl;
out << "City: " << c.city << endl;
out << "State: " << c.state << endl;
out << "Zip: " << c.zip << endl;
out << "Telephone: " << c.phone << endl;
out << "Account balance: $" << c.balance
<< endl;
out << "Date of last payment: " << c.lastPay
<< endl << endl;
return out;
}
// Declaration of the Customer structure
struct Customer
{
string name; // Name
string address; // Address
string city; // City
string state; // State
string zip; // ZIP code
string phone; // Phone number
double balance; // Account balance
string lastPay; // Date of last payment
Customer(){};
Customer(Customer&& other)
: name(std::move(other.name)
address(std::move(other.ad
city(std::move(other.city)
state(std::move(other.stat
zip(std::move(other.zip)),
phone(std::move(other.phon
lastPay(std::move(other.la
balance(other.balance)
{
};
// Also implement default move operator
const Customer& operator=(Customer&& other)
{
if(&(*this) != &other)
{
name = (std::move(name)),
address = (std::move(address)),
city = (std::move(city)),
state = (std::move(state)),
zip = (std::move(zip)),
phone = (std::move(phone)),
lastPay = (std::move(lastPay)),
balance = other.balance;
}
return *this;
}
};
ostream& operator<<(ostream& out, const Customer& c)
{
out << fixed << showpoint << setprecision(2);
out << "Customer name: " << c.name << endl;
out << "Customer address: " << c.address
<< endl;
out << "City: " << c.city << endl;
out << "State: " << c.state << endl;
out << "Zip: " << c.zip << endl;
out << "Telephone: " << c.phone << endl;
out << "Account balance: $" << c.balance
<< endl;
out << "Date of last payment: " << c.lastPay
<< endl << endl;
return out;
}
I don't understand what exactly is your goal. But when you use C++ not C the goal is to use class instead structure. With class you could use your code not only for current task but in other task like this. The other oportunity that a class gives is that you can change your implementation and thata but methods to stay the same and to be used by other part of program like they were used before the changes. Tamplates gives you the oportunty to use change the type of the parameters when you call methods. This is what I see as oportunity to make your code more efficient. But that dipendes from situation where you use your code
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Open in new window