Solved

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

Posted on 2004-03-23
4
221 Views
Last Modified: 2011-09-20
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.
0
Comment
Question by:Broken_Arrow
  • 2
4 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10664779
Hi  Broken_Arrow,

I have posted the answer to this i your previous post
The same is pasted below

 The problem lies in your constructor

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

The above constructor has arguments with the same variable name as the class data members
Thus
     Description = Description;
would effectively do nothing to your class data member,
Thus the assignment happens to local variables
Thus the names of parameters should not same as the class data members

Example

SalesRegister::SalesRegister(string Description1, int TotalCostPrice1, int TotalSellPrice1, int ItemCount1)
{
     Description = Description1;
     TotalCostPrice = TotalCostPrice1;
     TotalSellPrice = TotalSellPrice1;
     ItemCount = ItemCount1;
}



Amit
0
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 250 total points
ID: 10665451
The following program demostrates this

Consider the following class

class A {
      int i ;
      public :
            A ( int i ) {
                  i = i ;              // i here refers the local variable (i.e. parameter) and not the 'i' of the class object
            }
            void getA ( ) {
                  cout << i ;
            }
} ;

int main()
{
      A a ( 1 ) ;
      a.getA () ;
       system ( "PAUSE" ) ;
       return 0;
}

Solutions
1) as posted above, change the parameter name
2) Use this -> i to refer to the class data member
Thus the code in this case would be

class A {
      int i ;
      public :
            A ( int i ) {
                  this -> i = i ;              
            }
            void getA ( ) {
                  cout << i ;
            }
} ;

int main()
{
      A a ( 1 ) ;
      a.getA () ;
       system ( "PAUSE" ) ;
       return 0;
}


HTH

Amit
0
 
LVL 4

Expert Comment

by:booki
ID: 10666897
Broken_Arrow,

The problem lies in the two constructors:

StockItem::StockItem(string Author, string Title, string Publisher, int Year, int CostPrice, int SellPrice)
SalesRegister::SalesRegister(string Description, int TotalCostPrice, int TotalSellPrice, int ItemCount)

Since your member function (constructors in this case) parameters have the same name as data members of the classes.  For example Author is a data member of StockItem but it is also a parameter to your constructor.  Now when you refer to Author within the function (constructor) which Author do you get, the data member or the parameter?  Well the answer is that you get the parameter.  So when you make the statement:

Author = Author

the class data member is unaffected as both references to Author resolve to the parameter.

You have 3 options.

1. Sys_Prog has already proposed this solution.  Change the parameter names so they do not confict with the data members.

StockItem::StockItem(string Author1, string Title1, string Publisher1, int Year1, int CostPrice1, int SellPrice1)
{
    Author = Author1;    
    Title = Title1;
    Publisher = Publisher1;
    Year = Year1;
    CostPrice = CostPrice1;
    SellPrice = SellPrice1;
}


2. Use the resolution operator to remove the ambiguity.

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


3. Use the this pointer.  Also already suggested by Sys_Prog.

StockItem::StockItem(string Author, string Title, string Publisher, int Year, int CostPrice, int SellPrice)
{
    this->Author = Author;    
    this->Title = Title;
    this->Publisher = Publisher;
    this->Year = Year;
    this->CostPrice = CostPrice;
    this->SellPrice = SellPrice;
}

*Note: The three solutions options only fix StockItem.  Similar changes need to occur for SalesRegister constructor.

Sys_Prog,
sorry... i tried posting this earlier (before your most recent post) but it seems that oldlook is not allowing some comments to be posted.. anyway i just posted my reply in its entirety even though you seem to have covered most of the options.

b.
0
 

Author Comment

by:Broken_Arrow
ID: 10671559
Thanks EveryBody for your help, I now just have a logical problem, but I will work that out my Self, Thanks Again for all you help. Campbell
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now