4 Replies - 495 Views - Last Post: 03 October 2009 - 12:08 PM Rate Topic: -----

#1 thepugilist  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-October 09

Stuck trying to solve a problem, need some suggestions.

Posted 03 October 2009 - 10:51 AM

*Problem Solved*
I am supposed to write a program for a food delivery business that allows a cashier to add up the total receipts for deliveries. The program then displays the deliverer's name, total amount of the receipts, number of deliveries, and average delivery price. I have two deliverers to enter information for and was doing alright util I had to loop back and get the input for the second deliverer. When i do that, the name of the first deliverer is still output and the totals and average for the second deliverer is added to that of the first deliverer.

I'm not asking you to do my work for me,I've done most of it and now I am just stuck. I just need some ideas on how I can get past this. Any suggestions? I'm using Dev C, my code is below.

#include <iomanip>
#include "string"
using namespace std;

int main()
{
	float delivery_charge, total_receipts, average;
	int num_deliveries = 0;
	string deliverer_name;
	cout << "Enter deliverer's name: ";
	cin >> deliverer_name;
	while (deliverer_name != "0")
	   {
		cout << endl << "Delivery charge: ";
		cin >> delivery_charge;
		while (delivery_charge > 0)
		   {
			total_receipts = total_receipts + delivery_charge;
			num_deliveries++;  
			cout << endl << "Delivery charge: ";
			cin >> delivery_charge;
		   }
	   average = total_receipts / num_deliveries;
	   cout <<endl << "Deliverer = " << deliverer_name << endl;
	   cout <<endl << "Number of deliveries = " << num_deliveries << endl;
	   cout.setf(ios::fixed);
	   cout <<endl <<"Total receipts = " << setprecision(2) << total_receipts << endl;
	   cout <<endl << "Average charge per delivery = " << average << endl<<endl;
	   }
	system("PAUSE");
	return 0;
}


This post has been edited by thepugilist: 03 October 2009 - 11:31 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Stuck trying to solve a problem, need some suggestions.

#2 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Stuck trying to solve a problem, need some suggestions.

Posted 03 October 2009 - 11:12 AM

Quote

When i do that, the name of the first deliverer is still output


That is because you never ask for the second deliverer's name. You only have the user input the first one, before your while loop.

cin >> deliverer_name;
while (deliverer_name != "0")
{


So you'll have to put this code somewhere within one of your loops:
cout << "Enter deliverer's name: ";
cin >> deliverer_name;


edit--oops, I did not notice that you have two problems here.

Quote

and the totals and average for the second deliverer is added to that of the first deliverer.


You'll need to reset the values to 0 before adding & averaging the 2nd deliverer's amounts. //that is, if you don't need to save the values, just print them

This post has been edited by OliveOyl3471: 03 October 2009 - 11:16 AM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: Stuck trying to solve a problem, need some suggestions.

Posted 03 October 2009 - 11:12 AM

Something to read (also)
Was This Post Helpful? 0
  • +
  • -

#4 thepugilist  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-October 09

Re: Stuck trying to solve a problem, need some suggestions.

Posted 03 October 2009 - 11:20 AM

Quote

That is because you never ask for the second deliverer's name. You only have the user input the first one, before your while loop.

So you'll have to put this code somewhere within one of your loops:
cout << "Enter deliverer's name: ";
cin >> deliverer_name;


Right, thank you so much. It seems like I got a habit of overlooking things like that.


View PostJackOfAllTrades, on 3 Oct, 2009 - 10:12 AM, said:




Thanks. Looks like helpful stuff, I added it to my C++ folder in my bookmarks.
Was This Post Helpful? 0
  • +
  • -

#5 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Stuck trying to solve a problem, need some suggestions.

Posted 03 October 2009 - 12:08 PM

View Postthepugilist, on 3 Oct, 2009 - 01:20 PM, said:

Right, thank you so much. It seems like I got a habit of overlooking things like that.


You're welcome. Glad I could help. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1