Hi,

My problem here is that, I want to add up the total of the few different items that are entered. I have no idea how to add them up, please offer some suggestions, thanks

#include<iostream>
#include <windows.h>

using namespace std;
float weightcount(float w);
float distancecount(float d);

int main(void)
{
	int items,count=0;
	float weight;
	float distance;
	float total;
	
	cout<<"********Courier Service**********\n \n";

	cout<<"Number of items: ";
	cin>>items;
	while (count<items){
		count= count+1;
		cout<<"Item "<<count<<endl;
		cout<<"Net weight in kg: ";
		cin>>weight;
		while (weight>60){
			Beep(623.2511,200);
			cout<<"Too heavy, only below 60kg. Re-enter weight: ";
			cin>>weight;
		}
		cout<<"Distance travel in km: ";
		cin>>distance;
		while (distance>500){
			Beep(623.2511,200);
			cout<<"Too far, only under 500km. Re-enter distance: ";
			cin>>distance;
		}

	cout<<"\n";
	
	total = weightcount(weight) + distancecount(distance);
	cout<<"The cost is RM "<<total<<endl;
	}
	
	return 0;
}

float weightcount(float w)
{
	if (w<=10){
		w = w*2;
	}
	else if (w>10 && w<=30){
		w = 20 + ( (w-10)*3 );
	}
	else if (w>30){
		w = 80 + ( (w-30)*4 );
	}
	return(w);
}

float distancecount(float d)
{
	if (d<=50){
		d = d*0.5;
	}
	else if (d>50 && d<=150){
		d = 25 + ( (d-50)*1 );
	}
	else if (d>150 && d<=300){
		d = 125 + ( (d-150)*2 );
	}
	else if (d>300){
		d = 425 + ( (d-300)*3 );
	}
	return(d);
}

eg:
If I entered 4 different items with weights and distance, the display would show the total for each 4 individual items. I want to add up all 4 total together. How should I do it?

It appears as if

total += weightcount(weight) + distancecount(distance);

would do it. And also initialize total to zero before entering the loop.

It appears as if

total += weightcount(weight) + distancecount(distance);

would do it. And also initialize total to zero before entering the loop.

i tried it but couldn't get the total of different items together. By the way, the code you gave should be place in the loop or outside the loop?i tried both. The first one gives answers that have e and the other one produces an answer that is total*2

Perhaps try a simplified version first ...

int main(void)
{
  float total = 0.0f;

  // loop twice ...
  int count = 2;

  while (count --)
  {
    float weight = 0.0f,
          distance = 0.0f,

    cout  << "Item " << count << "Net weight in kg: ";

    cin >> weight;

    cout << "Distance travel in km: ";

    cin >> distance;

    // double the values and sum up ...
    total += 2* weight + 2 * distance;

    cout << "The total cost is now " << total << endl;
  }
  return 0;
}

PS. If you don't have any specific reason to operate with float (such as saving memory), then rather switch to double s altogether.

Perhaps try a simplified version first ...

int main(void)
{
  float total = 0.0f;

  // loop twice ...
  int count = 2;

  while (count --)
  {
    float weight = 0.0f,
          distance = 0.0f,

    cout  << "Item " << count << "Net weight in kg: ";

    cin >> weight;

    cout << "Distance travel in km: ";

    cin >> distance;

    // double the values and sum up ...
    total += 2* weight + 2 * distance;

    cout << "The total cost is now " << total << endl;
  }
  return 0;
}

PS. If you don't have any specific reason to operate with float (such as saving memory), then rather switch to double s altogether.

sorry but I really couldn't understand the += operation. Are there any alternate methods?

sorry but I really couldn't understand the += operation. Are there any alternate methods?

Well, one way, perhaps more clear to you, would be

float total = 0.0f;
while(looping)
{
  float calculated_value = some_func();
  // add to the 'total' ...
  total = total + calculated_value;
}

So, += is another form of value = value + some_other_value , they both do exactly the same thing;

Well, one way, perhaps more clear to you, would be

float total = 0.0f;
while(looping)
{
  float calculated_value = some_func();
  // add to the 'total' ...
  total = total + calculated_value;
}

So, += is another form of value = value + some_other_value , they both do exactly the same thing;

Hey I finally got it! i left out initializing total = 0. Why is it important to initialize it to 0 before entering the loop? is it that it would take some other values as total if not initialize? if so, would the value be known?

i left out initializing total = 0. Why is it important to initialize it to 0 before entering the loop?

Basically, if you don't initialize it, then the initial value may be pretty much anything. The compiler doesn't care, it expects you to do the initialization. Maybe, try it out, put a cout << total << endl; prior to entering the loop and see what the initial value is.

Basically, if you don't initialize it, then the initial value may be pretty much anything. The compiler doesn't care, it expects you to do the initialization. Maybe, try it out, put a cout << total << endl; prior to entering the loop and see what the initial value is.

oh ok. thank you very much for your help :)

This question has already been answered. Start a new discussion instead.