By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

need help with data types, constructors, tight deadline

P: n/a
I am a new C++ programmer. I am still having trouble with certain data
types and constructors, among other things. I'm not sure if I've
used "std::string" properly throughout this program. I need to fix
that, as well as add exception-handling, make sure everything is
documented properly, and make certain that the code compiles and runs
correctly. I'm under a very tight deadline, as this needs to run
perfectly within the next 24 hours.

Header File: computer.h #if !defined COMPUTER_H
#define COMPUTER_H

enum TComputerType { ctLaptop = 1, ctDesktop, ctGraphics, ctScientific,
ctMainframe, ctMidrange };

struct Computer
{
Computer( std::string const& aModel, operatingsystem, TComputerType
aCategory )
: model( aModel ), operatingsystem( aOperatingsystem ), category(
aCategory )
{}

std::string model;
std::string operatingsystem;
TComputerType category;
};

#endif // COMPUTER_H

Source File: computer.cpp // Computer.cpp: implementation of the
Computer class.

#include <iostream>
#include <string>
using namespace std;

#include "Computer.h"

//--------------------------------------------------------------------
Computer::Computer()
{
setMakeModel("No Computer Selected");
setOperatingsystem(Windows);
setCategory(ctLaptop);
}
//--------------------------------------------------------------------
Computer::Computer(const char *MM, std::string O, std::string Cat)
{
setMakeModel(MM);
setOperatingSystem(O);
setCategory(Cat);
}
//--------------------------------------------------------------------
Computer::~Computer()
{
// delete [] MakeModel;
}
//--------------------------------------------------------------------
void Computer::setMakeModel(const char *m)
{
MakeModel = new char[strlen(m) + 1];
strcpy(MakeModel, m);
}
//--------------------------------------------------------------------
char* Car::getMakeModel() const
{
return MakeModel;
}
//--------------------------------------------------------------------
void ComputerOperatingsystem::setComputerOperatingsyste m(std::string y)
{
ComputerOperatingsystem = y;
}
//--------------------------------------------------------------------
std::string Computer::getComputerOperatingsystem() const
{
return ComputerOperatingsystem;
}
//--------------------------------------------------------------------
void Computer::setCategory(const int c)
{
Category = c;
}
//--------------------------------------------------------------------
int Computer::getCategory() const
{
return Category;
}
//--------------------------------------------------------------------

Header File: customer.h #if !defined CUSTOMER_H
#define CUSTOMER_H

class Customer
{
private:
std::string FirstName;
std::string LastName;
std::string Address;
std::string City;
std::string State;
long ZIPCode;

public:
void setFirstName(const char *FN);
char* getFirstName() const { return FirstName; }
void setLastName(const char *LN);
char* getLastName() const { return LastName; }
char* FullName() const;
void setAddress(const char *Adr);
char* getAddress() const { return Address; }
void setCity(const char *CT);
char* getCity() const { return City; }
void setState(const char *St);
char* getState() const { return State; }
void setZIPCode(const long ZIP);
long getZIPCode() const { return ZIPCode; }
Customer();
Customer(char *FName, char *LName, char *Adr,
char *Ct, char *St, long ZIP);
Customer(const Customer &Pers);
Customer(char * FName, char * LName);
~Customer();
};

#endif // CUSTOMER_H

Source File: customer.cpp
//---------------------------------------------------------------------------
#include <iostream>
using namespace std;
#pragma hdrstop

#include "Customer.h"

//---------------------------------------------------------------------------
member objects:

std::string FirstName, LastName;

function:
std::string Customer::FullName() const
{
std::string sName = FirstName + " " + LastName;

return sName;
}
//---------------------------------------------------------------------------
Customer::Customer()
: ZIPCode(0)
{
FirstName = new char[20];
strcpy(FirstName, "John");
LastName = new char[20];
strcpy(LastName, "Doe");
Address = new char[40];
strcpy(Address, "123 Main Street Apt A");
City = new char[32];
strcpy(City, "Great City");
State = new char[30];
strcpy(State, "Our State");
}
//---------------------------------------------------------------------------
Customer::Customer(char * FName, char * LName)
: ZIPCode(0)
{
FirstName = new char[strlen(FName) + 1];
strcpy(FirstName, FName);
LastName = new char[strlen(LName) + 1];
strcpy(LastName, LName);
Address = new char[40];
strcpy(Address, "123 Main Street Apt A");
City = new char[32];
strcpy(City, "Great City");
State = new char[30];
strcpy(State, "Our State");
}
//---------------------------------------------------------------------------
Customer::Customer(char *FName, char *LName, char *Adr,
char *Ct, char *St, long ZIP)
: ZIPCode(ZIP)
{
FirstName = new char[strlen(FName) + 1];
strcpy(FirstName, FName);
LastName = new char[strlen(LName) + 1];
strcpy(LastName, LName);
Address = new char[40];
strcpy(Address, Adr);
City = new char[32];
strcpy(City, Ct);
State = new char[30];
strcpy(State, St);
}
//---------------------------------------------------------------------------
Customer::Customer(const Customer &Pers)
: ZIPCode(Pers.ZIPCode)
{
FirstName = new char[strlen(Pers.FirstName) + 1];
strcpy(FirstName, Pers.FirstName);
LastName = new char[strlen(Pers.LastName) + 1];
strcpy(LastName, Pers.LastName);
Address = new char[strlen(Pers.Address) + 1];
strcpy(Address, Pers.Address);
City = new char[strlen(Pers.City) + 1];
strcpy(City, Pers.City);
State = new char[strlen(Pers.State) + 1];
strcpy(State, Pers.State);
}
//---------------------------------------------------------------------------
void Customer::setFirstName(const char *FN)
{
strcpy(FirstName, FN);
}
//---------------------------------------------------------------------------
void Customer::setLastName(const char *LN)
{
strcpy(LastName, LN);
}
//---------------------------------------------------------------------------
void Customer::setAddress(const char *Adr)
{
strcpy(Address, Adr);
}
//---------------------------------------------------------------------------
void Customer::setCity(const char *CT)
{
strcpy(City, CT);
}
//---------------------------------------------------------------------------
void Customer::setState(const char *St)
{
strcpy(State, St);
}
//---------------------------------------------------------------------------
void Customer::setZIPCode(const long ZIP)
{
ZIPCode = ZIP;
}
//---------------------------------------------------------------------------
Customer::~Customer()
{
delete [] FirstName;
delete [] LastName;
delete [] Address;
delete [] City;
delete [] State;
}
//---------------------------------------------------------------------------

Header File: Invoice.h

#if !defined INVOICE_H
#define INVOICE_H

#include "Customer.h"
#include "Computer.h"
#include "RentDate.h"

class Invoice
{
public:
Invoice();
virtual ~Invoice();
Customer CustomerRegistration();
void CustomerInformation(const Customer& Pers);
Computer ComputerSelection();
void ComputerSelected(const Computer& Machine);
double CalculatePrice(const Computer& Machine, double& Rate, int
&Days);
void setMemory(const long g);
long getMemory() const;
void setProcessorPower(const char *v);
char* getProcessorPower() const;
void setComputerCondition(const char *c);
char* getComputerCondition() const;

void ProcessOrder();
void ComputerExamination();
void ShowOrder();

private:
std::string Memory;
std::string ProcessorPower;
std::string ComputerCondition;
};

#endif // INVOICE_H
Source File: Invoice.cpp

// Invoice.cpp: implementation of the Invoice class.
//
//////////////////////////////////////////////////////////////////////
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

#include "Invoice.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Invoice::Invoice()
{
setMemory("384MBofRAM");
setComputerCondition("Good");
}
//--------------------------------------------------------------------
Invoice::~Invoice()
{
}
//--------------------------------------------------------------------
Customer Invoice::CustomerRegistration()
{
std::string FName, LName, Addr, CT, St;
long ZC = 0;

cout << "Enter Customer Information\n";
cout << "First Name: "; cin >> FName;
cout << "Last Name: "; cin >> LName;
cout << "Address: "; cin >> ws;
cin.getline(Addr, 40);
cout << "City: ";
cin.getline(CT, 32);
cout << "State: ";
cin.getline(St, 30);
cout << "Zip Code: "; cin >> ZC;

Customer Cust(FName, LName, Addr, CT, St, ZC);
return Cust;
}
//--------------------------------------------------------------------
Computer Invoice::ComputerSelection()
{
std::string ComputerType, ModelChosen;
std::string ComputerSelected;
int ComputerSelectedOperatingsystem = Windows;

cout << "What type of computer would you like to rent?";
do {
cout << "\n1 - Laptop | 2 - Desktop | 3 - Graphics"
<< "\n4 - Scientific | 5 - Mainframe | 6 - Midrange";
cout << "\nYour Choice: ";
cin >> ComputerType;
if( ComputerType < 1 || ComputerType > 6 )
cout << "\nPlease type a number between 1 and 6";
} while(ComputerType < 1 || ComputerType > 6);

switch(ComputerType)
{
case ctLaptop:
cout << "\nFor the Laptop type, we have:"
<< "\n1 - HP Pavilion | 2 - Compaq Presario";
cout << "\nWhich one would you prefer? ";
cin >> ModelChosen;

if(ModelChosen == 1)
{
strcpy(strComputerSelected, "HP Pavillion");
ComputerSelectedOperatingsystem = Windows;
}
else
{
strcpy(strComputerSelected, "Compaq Presario");
ComputerSelectedOperatingsystem = Windows;
}
break;

case ctDesktop:
cout << "\nFor the Desktop type, we have:"
<< "\n1 - Gateway DX110S | 2 - Apple Macmini"
<< "\n3 - Dell DimensionB110 | 4 - eMachines T3120";
cout << "\nWhich one would you prefer? ";
cin >> ModelChosen;

if(ModelChosen == 1)
{
strcpy(strComputerSelected, "Gateway DX110s");
ComputerSelectedOperatingsystem = Windows;
}
else if(ModelChosen == 2)
{
strcpy(strComputerSelected, "Apple Macmini");
ComputerSelectedOperatingsystem = MacOSX;
}
else if(ModelChosen == 3)
{
strcpy(strComputerSelected, "Dell DimensionB110");
ComputerSelectedOperatingsystem = Windows;
}
else
{
strcpy(strComputerSelected, "eMachines T3120");
ComputerSelectedOperatingsystem = Windows;
}
break;

case ctGraphics:
cout << "\nFor the Graphics type, we have:"
<< "\n1 - Silicon Graphics Tezro | 2 - Apple PowerMacG5";
cout << "\nWhich one would you prefer? ";
cin >> ModelChosen;

if(ModelChosen == 1)
{
strcpy(strComputerSelected, "Silicon Graphics Tezro");
ComputerSelectedOperatingsystem = IRIX;
}
else
{
strcpy(strComputerSelected, "Apple Power Mac G5");
ComputerSelectedOperatingsystem = MacOSXTiger;
}
break;

case ctScientific:
cout << "\nFor the Scientific type, we have:"
<< "\n1 - Sun Ultra 45 | 2 - Silicon Graphics Fuel | 3 - Silicon
Graphics Prism";
cout << "\nWhich one would you prefer? ";
cin >> ModelChosen;

if(ModelChosen == 1)
{
strcpy(strComputerSelected, "Sun Ultra 45");
ComputerSelectedOperatingsystem = Solaris10;
}
else if(ModelChosen == 2)
{
strcpy(strComputerSelected, "Silicon Graphics Fuel");
ComputerSelectedOperatingsystem = IRIX;
}
else
{
strcpy(strComputerSelected, "Silicon Graphics Prism");
ComputerSelectedOperatingsystem = Linux;
}
break;

case ctMainframe:
cout << "\nFor the Mainframe type, we have:"
<< "\n1 - IBM zSeries 990 | 2 - IBM zSeries 890"
<< "\n3 - IBM zSeries 900 | 4 - IBM System z9";
cout << "\nWhich one would you prefer? ";
cin >> ModelChosen;

if(ModelChosen == 1)
{
strcpy(strComputerSelected, "IBM zSeries 990");
ComputerSelectedOperatingsystem = LinuxOnZSeries;
}
else if(ModelChosen == 2)
{
strcpy(strComputerSelected, "IBM zSeries 890");
ComputerSelectedOperatingsystem = LinuxOnZSeries;
}
else if(ModelChosen == 3)
{
strcpy(strComputerSelected, "IBM zSeries 900");
ComputerSelectedOperatingsystem = LinuxOnZSeries;
}
else
{
strcpy(strComputerSelected, "IBM System z9");
ComputerSelectedOperatingsystem = LinuxOnSystemZ;
}
break;

case ctMidrange:
cout << "\nFor the Midrange type, we have:"
<< "\n1 - IBM eServer i5 520 | 2 - IBM eServer i5 550"
<< "\n3 - IBM eServer i5 570 | 4 - IBM eServer i5 595";
cout << "\nWhich one would you prefer? ";
cin >> ModelChosen;

if(ModelChosen == 1)
{
strcpy(strComputerSelected, "IBM eServer i5 520");
ComputerSelectedOperatingsystem = IBMEServerI5OS;
}
else if(ModelChosen == 2)
{
strcpy(strComputerSelected, "IBM eServer i5 550");
ComputerSelectedOperatingsystem = IBMEServerI5OS;
}
else if(ModelChosen == 3)
{
strcpy(strComputerrSelected, "IBM eServer i5 570");
ComputerSelectedOperatingsystem = IBMEServerI5OS;
}
else
{
strcpy(strComputerSelected, "IBM eServer i5 595");
ComputerSelectedOperatingsystem = IBMEServerI5OS;
}
break;
}

Computer Selected(strComputerSelected,
ComputerSelectedOperatingsystem, ComputerType);
return Selected;
}
//--------------------------------------------------------------------
void Invoice::setMemory(std::string g)
{
Memory = g;
}
//--------------------------------------------------------------------
long Invoice::getMemory() std::string
{
return Memory;
}
//--------------------------------------------------------------------
void Invoice::setProcessorPower std::string v
{
ProcessorPower = std::string v;
strcpy(ProcessorPower, v);
}
//--------------------------------------------------------------------
std:string Invoice::getProcessorPower
{
return ProcessorPower;
}
//--------------------------------------------------------------------
void Invoice::setComputerCondition(const char *c)
{
ComputerCondition = new char[strlen(c) + 1];
strcpy(ComputerCondition, c);
}
//--------------------------------------------------------------------
char* Invoice::getComputerCondition() const
{
return ComputerCondition;
}
//--------------------------------------------------------------------
void Invoice::ProcessOrder()
{
int NbrOfDays;;
double Rate, TotalPrice;
// Enter Customer Information
Customer Person = CustomerRegistration();
cout << "\nProcess Computer Information\n";
Computer Usage = ComputerSelection();

TotalPrice = CalculatePrice(Usage, Rate, NbrOfDays);
ComputerExamination();

// This function works for both Borland C++ Builder and MSVC
system("cls");

cout << " - San Francisco Computer Rental -";
cout << "\n=============================";
CustomerInformation(Person);
cout << "\n------------------------------";
ComputerSelected(Usage);
cout << setiosflags(ios::fixed) << setprecision(2);
cout << "\n------------------------------";
cout << "\nComputer Memory: " << getMemory();
cout << "\nCondition: " << getComputerCondition();
cout << "\nProcessorPower : " << getProcessorPower();
cout << "\n# of Days: " << NbrOfDays;
cout << "\n------------------------------";
cout << "\nRate: $" << Rate;
cout << "\nTotal Price: $" << TotalPrice;
cout << "\n==============================\n";
}
//--------------------------------------------------------------------
void Invoice::CustomerInformation(const Customer& Pers)
{
cout << "\nEmployee Identification";
cout << "\nFull Name: " << Pers.FullName();
cout << "\nAddress: " << Pers.getAddress();
cout << "\nCity: " << Pers.getCity() << ", "
<< Pers.getState() << " " << Pers.getZIPCode();
}
//--------------------------------------------------------------------
void Invoice::ComputerSelected(const Computer& Machine)
{
cout << "\nModel: " << Machine.getMakeModel();
cout << "\nOperatingsystem: " <<
Machine.getComputerOperatingsystem();
}

//--------------------------------------------------------------------
double Invoice::CalculatePrice(const Computer& Machine, double&
DayRate,
int &NumberOfDays)
{
// char WeekEndResponse;// Does the customer rent the computer for the
week-end?
double OneDayRate, // If renting for less than 5 days including
week-end
WeekDay, // If renting for at least 5 days, regardless of the days
WeekEnd = 0;// If renting for more than two days from Friday to
monday
//double DayRate; // Rate applied based on the number of days
double TotalRate;

switch(Machine.getCategory())
{
case ctDesktop:
DayRate = 24.95;
OneDayRate = 29.95;
WeekDay = 24.95;
WeekEnd = 19.95;
break;

case ctLaptop:
DayRate = 34.95;
OneDayRate = 39.95;
WeekDay = 34.95;
WeekEnd = 25.95;
break;

case ctGraphics:
DayRate = 38.95;
OneDayRate = 49.95;
WeekDay = 38.95;
WeekEnd = 32.95;
break;

case ctScientific:
DayRate = 44.95;
OneDayRate = 69.95;
WeekDay = 44.95;
WeekEnd = 35.95;
break;

case ctMidrange:
DayRate = 54.95;
OneDayRate = 89.95;
WeekDay = 54.95;
WeekEnd = 42.95;
break;

case ctMainframe:
DayRate = 64.95;
OneDayRate = 89.95;
WeekDay = 64.95;
WeekEnd = 50.95;
break;
}

cout << "\nHow many days? "; cin >> NumberOfDays;
TotalRate = DayRate * NumberOfDays;
return TotalRate;
}
//--------------------------------------------------------------------
void Invoice::ComputerExamination()
{
char Cond;
std::string ProcessorPower;

cout << "Rate the computer's condition (e=Excellent/g=Good/d=Useable):
";
cin >> Cond;
if( Cond == 'e' || Cond == 'E' )
strcpy(ComputerCondition, "Excellent");
else if( Cond == 'g' || Cond == 'G' )
strcpy(ComputerCondition, "Good");
else if( Cond == 'd' || Cond == 'D' )
strcpy(ComputerCondition, "Useable");
else
strcpy(ComputerCondition, "Can't Decide");

cout << "Enter the computer memory: ";
cin >> Memory;

do {
cout << "ProcessorPower"
<< "\n1 - 1.6 GHz"
<< "\n2 - 1.9 GHz"
<< "\n3 - 2.5 GHz"
<< "\n4 - 2.7 GHz"
<< "\n5 - 3.7 GHz";
cout << "\nEnter the ProcessorPower: ";
cin >> ProcessorPower;
}while(ProcessorPower < 3.7GHz || ProcessorPower > 1.6GHz);

switch(ProcessorPower)
{
case 1:
setProcessorPower("1.6 GHz");
break;
case 2:
setProcessorPower("1.9 GHz");
break;
case 3:
setProcessorPower("2.5 GHz");
break;
case 4:
setProcessorPower("2.7 GHz");
break;
case 5:
setProcessorPower("3.7 GHz");
break;
}
}
//--------------------------------------------------------------------
Main File: Main.cpp

#include "Invoice.h"

void main()
{
CInvoice Order;

Order.ProcessOrder();
Order.ShowOrder();
}

dte

Jun 25 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
ro*******@gmail.com schrieb:
I am a new C++ programmer. I am still having trouble with certain data
types and constructors, among other things. I'm not sure if I've
used "std::string" properly throughout this program. I need to fix
that, as well as add exception-handling, make sure everything is
documented properly, and make certain that the code compiles and runs
correctly. I'm under a very tight deadline, as this needs to run
perfectly within the next 24 hours.
Generally, we don't do homework for you, see the FAQ, but...

This: class Customer
{
private:
std::string FirstName;
std::string LastName;
std::string Address;
std::string City;
std::string State; [snip]

This: Customer::Customer()
: ZIPCode(0)
{
FirstName = new char[20];
strcpy(FirstName, "John");
LastName = new char[20];
strcpy(LastName, "Doe");
Address = new char[40];
strcpy(Address, "123 Main Street Apt A");
City = new char[32];
strcpy(City, "Great City");
State = new char[30];
strcpy(State, "Our State");
} [snip]

And this: Customer::~Customer()
{
delete [] FirstName;
delete [] LastName;
delete [] Address;
delete [] City;
delete [] State;
}


Doesn't make really sense. You shouldn't new[], delete[] and strcpy with
std::string, just do: FirstName = "John" or the like.
std::string does handle the memory stuff.

Thomas
Jun 25 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.