Hello!

I have written the code for a function that takes an object DailySale and compares its values with an array of objects fromInventory. If the id of the DailySale object matches with the id of an object in the array, it takes the price from there and calculates the total cost. I am getting garbage numbers in result. Here is the code
of the function:

int computeTotalPrice(Medicine FromInventory[], Sale DailySale){
                      int TotalPrice;
                      int Price;
                      
                       for (int l=0;l<3;l++){  
                      
                      if(DailySale.getId()==FromInventory[l].getId()){
                      
                      Price= FromInventory[l].getUnitPrice();
                      
                      }

And in the main i am calling this function in a loop

for(int j=0;j<3;j++){
      cout<<endl<<endl;       
      DailySale[j].dailySaleReport();
      cout<<endl;
      computeTotalPrice(MedicineList,DailySale[j]) ;

I want to to know what is wrong with the logic in the loop.
Another problem i am facing is that i take name as input from the user in an char InpuName[10], and i set this name as my sale's object's name by using setName function. When i display the name, it does not diplay anything. The code is as follows

char InputName[10];
cin>>InputName;
setName(InputName);

But when i display the name, nothing gets printed

cout<<"Name of the item sold"<<endl<<Name<<endl;

Help me solve these two problems. I tried to take user input in char *InputName, but then the program stops in the middle of execution.

Recommended Answers

All 3 Replies

Is there anything in FromInventory once you get inside of your method? I would verify it contains what you think it does and is as long as you expect.

Second thing with the name is straightforward. Change your cin to a cin.getline(InputName,10); that way it will get the spaces and you won't overrun your char array.

In main i make an array of Medicine objects like

Medicine MedicineList[3]=
{Medicine(123,"Panadol", 10),Medicine(456,"antox",10),Medicine(456,"antox",10)}

In the getline function you mentioned, is char InputName[] or char *?

So if you call your getUnitPrice() within your computeTotalPrice (so just say looping through your inventory without the if statement making the comparison) what does it say the prices are?

You can still use your char array in the getline() if that's what you are asking. Just pass it in without brackets.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.