Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Incorrect Output, Could somebody point me in the right direction of why this is occuring ?

Avatar of Broken_Arrow
Broken_Arrow asked on
C++
4 Comments1 Solution254 ViewsLast Modified:
These are the files that I am using

Here is the StockItem.h
-------------------------------------------------------------------------------------------------------------------

#include <string>
using namespace std;

class StockItem
{
      private:
            string Author;
            string Title;
            string Publisher;
            int Year;
            int CostPrice;
            int SellPrice;
      public:
            StockItem(string Author = "", string Title = "", string Publisher = "", int Year = 0, int CostPrice = 0, int SellPrice = 0);
            string GetAuthor();
            string GetTitle();
            string GetPublisher();
            int GetYear();
            int GetCostPrice();
            int GetSellPrice();
            void SetAuthor(string NewAuthor);
            void SetTitle(string NewTitle);
            void SetPublisher(string NewPublisher);
            void SetYear(int NewYear);
            void SetCostPrice(int NewCostPrice);
            void SetSellPrice(int NewSellPrice);
};


Here is the StockItem.cpp file
---------------------------------------------------------------------------------------------------------------------
#include "StockItem.h"

StockItem::StockItem(string Author, string Title, string Publisher, int Year, int CostPrice, int SellPrice)
{
      Author = Author;      
      Title = Title;
      Publisher = Publisher;
      Year = Year;
      CostPrice = CostPrice;
      SellPrice = SellPrice;
}
string StockItem::GetAuthor()
{
      return (Author);
}

string StockItem::GetTitle()
{
      return (Title);
}

string StockItem::GetPublisher()
{
      return (Publisher);
}

int StockItem::GetYear()
{
      return (Year);
}

int StockItem::GetCostPrice()
{
      return (CostPrice);
}

int StockItem::GetSellPrice()
{
      return (SellPrice);
}

void StockItem::SetAuthor(string NewAuthor)
{
      Author = NewAuthor;
}

void StockItem::SetTitle(string NewTitle)
{
      Title = NewTitle;
}

void StockItem::SetPublisher(string NewPublisher)
{
      Publisher = NewPublisher;
}

void StockItem::SetYear(int NewYear)
{
      Year = NewYear;
}

void StockItem::SetCostPrice(int NewCostPrice)
{
      CostPrice = NewCostPrice;
}

void StockItem::SetSellPrice(int NewSellPrice)
{
      SellPrice = NewSellPrice;
}



Here are the contents of the SalesRegister.h
--------------------------------------------------------------------------------------------

#include <string>
using namespace std;
#include "StockItem.h"

class SalesRegister
{
     private:
          string Description;
          int TotalCostPrice;
          int TotalSellPrice;
          int ItemCount;

     public:
          SalesRegister(string Description, int TotalCostPrice, int TotalSellPrice, int ItemCount);
          SalesRegister(SalesRegister &Source);
          SalesRegister operator+(SalesRegister &Parameter);
          void RecordSale(StockItem &Item);
          void PrintReport();
          string GetDescription();
          int GetTotalCostPrice();
          int GetTotalSellPrice();
          int GetItemCount();
          void SetDescription(string NewDescription);
          void SetTotalCostPrice(int NewTotalCostPrice);
          void SetTotalSellPrice(int NewTotalSellPrice);
          void SetItemCount(int NewItemCount);
};
-------------------------------------------------------------------------------------------------------------

Here is the SalesRegister.cpp file
---------------------------------------------------------------------------------------------------------------
#include "SalesRegister.h"
#include <iomanip>
#include <iostream>
using namespace std;

SalesRegister::SalesRegister(string Description, int TotalCostPrice, int TotalSellPrice, int ItemCount)
{
     Description = Description;
     TotalCostPrice = TotalCostPrice;
     TotalSellPrice = TotalSellPrice;
     ItemCount = ItemCount;
}

SalesRegister::SalesRegister(SalesRegister &Source)
{
     Description = Source.GetDescription();
     TotalCostPrice = Source.GetTotalCostPrice();
     TotalSellPrice = Source.GetTotalSellPrice();
     ItemCount = Source.GetItemCount();
}

SalesRegister SalesRegister::operator +(SalesRegister &Parameter)
{
     SalesRegister temp(Parameter);
     temp.TotalCostPrice = this->TotalCostPrice + Parameter.TotalCostPrice;
     temp.TotalSellPrice = this->TotalSellPrice + Parameter.TotalSellPrice;
     return (temp);
}

void SalesRegister::RecordSale(StockItem &Item)
{
     cout << "$" << Item.GetSellPrice() << Item.GetAuthor() << Item.GetTitle() << Item.GetPublisher() << Item.GetYear() << endl;
     
     SetTotalCostPrice(GetTotalCostPrice() + Item.GetCostPrice());
     SetTotalSellPrice(GetTotalSellPrice() + Item.GetSellPrice());
     ItemCount = ItemCount + 1;
}

void SalesRegister::PrintReport()
{
     float Profit = 0;

     Profit = ((TotalSellPrice - TotalCostPrice) / ItemCount);
     
     cout << "Report for " << Description << endl;
     cout << "\t Total Sales:\t\t$" << TotalSellPrice << endl;
     cout << "\t Total Cost:\t\t$" << TotalSellPrice << endl;
     cout.setf(ios::fixed);
     cout.setf(ios::showpoint);
     cout << "\t Average Profit:\t$" << setprecision(2) << Profit << endl;

}

string SalesRegister::GetDescription()
{
     return (Description);
}

int SalesRegister::GetTotalCostPrice()
{
     return (TotalCostPrice);
}

int SalesRegister::GetTotalSellPrice()
{
     return (TotalSellPrice);
}

int SalesRegister::GetItemCount()
{
     return (ItemCount);
}

void SalesRegister::SetDescription(string NewDescription)
{
     Description = NewDescription;
}

void SalesRegister::SetTotalCostPrice(int NewTotalCostPrice)
{
     TotalCostPrice = NewTotalCostPrice;
}

void SalesRegister::SetTotalSellPrice(int NewTotalSellPrice)
{
     TotalSellPrice = NewTotalSellPrice;
}

void SalesRegister::SetItemCount(int NewItemCount)
{
     ItemCount = NewItemCount;
}
-----------------------------------------------------------------------------------------------------

Here is the ass1.cpp file
-----------------------------------------------------------------------------------------------------------
#include <iostream>
#include <string>

using namespace std;

#include "SalesRegister.h"

int main(int argc, char *argv[])
{
     StockItem text1("W. Savitch", "Absolute C++ 1/e", "Addison/Wesley", 2002, 75, 90);
     StockItem text2("L. A. Robertson", "Simple Program Design 4/e", "Thomson", 2003, 70, 80);
     StockItem text3("A. Silberschatz, P. B. Galvin, and G. Gagne", "Operating System Concepts 6/e", "John Wiley & Sons", 2003, 85, 120);

     SalesRegister monday("Monday March 1, 2004", 0, 0, 0);
     cout << monday.GetDescription() << endl;
     monday.RecordSale(text1);
     monday.RecordSale(text1);

     SalesRegister tuesday("Tuesday March 2, 2004", 0, 0, 0);
     cout << endl << tuesday.GetDescription() << endl;
     tuesday.RecordSale(text3);
     tuesday.RecordSale(text2);
     tuesday.RecordSale(text1);

     SalesRegister wednesday("Wednesday March 2, 2004", 0, 0, 0);
     cout << endl << wednesday.GetDescription() << endl;
     wednesday.RecordSale(text1);
     wednesday.RecordSale(text3);

     SalesRegister thursday("Thursday March 2, 2004", 0, 0, 0);
     cout << endl << thursday.GetDescription() << endl;
     thursday.RecordSale(text3);
     thursday.RecordSale(text2);

     SalesRegister friday("Friday March 2, 2004", 0, 0, 0);
     cout << endl << friday.GetDescription() << endl;
     friday.RecordSale(text1);
     friday.RecordSale(text2);

     SalesRegister total(monday + tuesday + wednesday + thursday + friday);
     total.SetDescription("Total Sales");

     monday.PrintReport();
     tuesday.PrintReport();
     wednesday.PrintReport();
     thursday.PrintReport();
     friday.PrintReport();
     total.PrintReport();

     return(0);
}
----------------------------------------------------------------------------------------------------------------


My program does not output the correct results in fact none of the correct results are outputted, this is something like what is suppose to be outputted.

Monday March 1, 2004
     $90     W. Savitch, Absolute C++ 1/e, Addison/Wesley, 2002
     $90     W. Savitch, Absolute C++ 1/e, Addison/Wesley, 2002

Tuesday March 2, 2004
     $120     A. Silberschatz, P. B. Galvin, and G. Gagne, Operating System Concepts 6/e, John Wiley & ons, 2003
     $80     L. A. Robertson, Simple Program Design 4/e, Thomson, 2003
     $90     W. Savitch, Absolute C++ 1/e, Addison/Wesley, 2002

Wednesday March 2, 2004
     $90     W. Savitch, Absolute C++ 1/e, Addison/Wesley, 2002
     $120     A. Silberschatz, P. B. Galvin, and G. Gagne, Operating System Concepts 6/e, John Wiley & Sons, 2003

Thursday March 2, 2004
     $120     A. Silberschatz, P. B. Galvin, and G. Gagne, Operating System Concepts 6/e, John Wiley & Sons, 2003
     $80     L. A. Robertson, Simple Program Design 4/e, Thomson, 2003

Friday March 2, 2004
     $90     W. Savitch, Absolute C++ 1/e, Addison/Wesley, 2002
     $80     L. A. Robertson, Simple Program Design 4/e, Thomson, 2003

Report for Monday March 1, 2004
     Total Sales:     $180
     Total Cost:     $150
     Average Profit:     $15.00

Report for Tuesday March 2, 2004
     Total Sales:     $290
     Total Cost:     $230
     Average Profit:     $20.00

Report for Wednesday March 2, 2004
     Total Sales:     $210
     Total Cost:     $160
     Average Profit:     $25.00

Report for Thursday March 2, 2004
     Total Sales:     $200
     Total Cost:     $155
     Average Profit:     $22.50

Report for Friday March 2, 2004
     Total Sales:     $170
     Total Cost:     $145
     Average Profit:     $12.50

Report for Total Sales
     Total Sales:     $1050
     Total Cost:     $840
     Average Profit:     $19.09

And my output is as follows

Campbell Star@campbell ~
$ ./ass1

$2011670320, , , , 1627947726
$2011670320, , , , 1627947726


$2147348480, , , , 6877184
$2011676203, , , , 16
$2011670320, , , , 1627947726


$2011670320, , , , 1627947726
$2147348480, , , , 6877184


$2147348480, , , , 6877184
$2011676203, , , , 16


$2011670320, , , , 1627947726
$2011676203, , , , 16
Report for
         Total Sales:    $-269333280
         Total Cost:     $-269333280
         Average Profit: $0.00
Report for
         Total Sales:    $1875729639
         Total Cost:     $1875729639
         Average Profit: $-236006064.00
Report for
         Total Sales:    $1875721602
         Total Cost:     $1875721602
         Average Profit: $0.00
Report for
         Total Sales:    $-133649157
         Total Cost:     $-133649157
         Average Profit: $0.00
Report for
         Total Sales:    $-271620727
         Total Cost:     $-271620727
         Average Profit: $0.00
Report for Total Sales
         Total Sales:    $-1218119219
         Total Cost:     $-1218119219
         Average Profit: $-1.00

I don't know why this is happening, I havn't used C++ before so any help would be appreciated.
ASKER CERTIFIED SOLUTION
Avatar of Sys_Prog
Sys_ProgFlag of India image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answers