ok so i got my code done but my logic in my code is off and my outputs are not coming out the way they should.

so the following is a notepad file which is being read into my program

1231 washington, george
300
3431 adams, john
600
5767 jefferson, tom
950
1515 madison, john
250
4532 wilson, woodrow
1300
2541 hovver, herbert
1450
4325 roosevelt, teddt
47
5151 roosevelt, franklin
1500
4624 truman, harry
2500
3333 eisenhower, ike
800
3451 kennedy, john
1000
7856 johnson, lyndon
1451
1111 nixon, richard
5242
2121 carter, jimmy
379
3541 reagan, ronald
4545

the following is my code

//calculate energy bill part 8
// ryan 
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
int i;
void sort(int[], int[], string[], int);
void c(int[], int[], int);
int customer[25];
int usagekwh[25];
double cost[25];
int arraysize;
string name[25];

int main()
{
	cout<<"enter the number of customers being processed";
	cin>>arraysize;

	ifstream input("K:/C++/energy bill part 3/input.txt", ios::in);
	if (!input){
		cerr << "File could not be opened\n";
		exit(1);
	}
	for (i=0; i<arraysize; i++)
	{
		//cout<<"i has top value:"<<i<<endl;
		input>>customer[i];
		//cout<<account[i]<<endl;
		//read the name
		input.ignore(1);
		getline(input,name[i]);
		
		input>>usagekwh[i];
		
		


	}

	sort(usagekwh, customer, name, arraysize);
	c(usagekwh, customer, arraysize);
}


void c(int usagekwh[], int customer[], int arraysize)

{
double rate, t;

double total_usage, total_cost, average_cost, average_usage;

	total_cost=0;
	total_usage=0;
	average_cost=0;
	average_usage=0;
	
cout<<setw(20)<<left<<"Customer Number"<<setw(25)<<"Customer Name"<<setw(10)<<"KWH Used"<<setw(10)<<"Amount Due"<<endl;

	
	//ditirmen what rate to be used

for (i=0; i<arraysize; i++)
{
	if (usagekwh[i]<=300)
	{
		rate=0.10;
		t=rate*usagekwh[i];
	}
	else if (usagekwh[i]<800)
	{
		rate=0.09;
		t=30+rate*(usagekwh[i]-300);
	}
	else if (usagekwh[i]<1300)
	{
		rate=0.07;
		t=30+(500*.09)+(rate*(usagekwh[i]-800));
	}
	else
	{
		rate=0.055;
		t=30+(500*.09)+(500*.07)+(rate*(usagekwh[i]-1300));
	}
	//output

	cout<<setw(12)<<right<<customer[i]<<setw(8)<<setw(25)<<name[i]<<setw(7)<<usagekwh<<setw(3)<<setw(8)<<fixed<<showpoint<<setprecision(2)<<cost[i]<<endl;

	cost[i]=t;

}

//compute the totals

for (i=0; i<7; i++)
{
	total_usage=total_usage+usagekwh[i];
	total_cost=total_cost+cost[i];
	
}

average_usage=total_usage/7;
average_cost=total_cost/7;

//output

cout<<"______________________________________________"<<endl;
cout<<"                                 "<<endl;
cout<<setw(9)<<"         "<<setw(10)<<right<<"Usage(kwh)"<<setw(26)<<right<<"Amount Due(dollars)"<<endl;
cout<<"______________________________________________"<<endl;
cout<<setw(9)<<right<<"Total"<<setw(10)<<setprecision(2)<<showpoint<<fixed<<total_usage<<setw(26)<<total_cost<<endl;
cout<<setw(9)<<right<<"Average"<<setw(10)<<setprecision(2)<<showpoint<<fixed<<average_usage<<setw(26)<<average_cost<<endl;
}


void sort(int usagekwh[], int customer[], string name[], int arraysize)
{
	int holdu, holdc, p, i;
	string holdn;

	//nested for loops to cycle thru array
	for(p=0; p<arraysize; p++)
	{
		for(i=0; i<(arraysize-1); i++)
		{
			//if statements
			if(customer[i]>customer[i+1])
			{
				//switch usage
				holdu=usagekwh[i];
				usagekwh[i]=usagekwh[i+1];
				usagekwh[i+1]=holdu;

				//switch customer number
				holdc=customer[i];
				customer[i]=customer[i+1];
				customer[i+1]=holdc;

				//switch names
				holdn=name[i];
				name[i]=name[i+1];
				name[i+1]=holdn;

			}
		}
	}
	return;
}

now the way i would like my output to happen is something like this but a little more spaced out

customer number customer name kwh used amount due
1231 Washington, George 300 $$$.$$

ect.

but my output is this

enter the number of customers being processed15
Customer Number Customer Name KWH Used Amount Due
1111 nixon, richard0041D150 0.00
1231 washington, george0041D150 0.00
1515 madison, john0041D150 0.00
2121 carter, jimmy0041D150 0.00
2541 hovver, herbert0041D150 0.00
3333 eisenhower, ike0041D150 0.00
3431 adams, john0041D150 0.00
3451 kennedy, john0041D150 0.00
3541 reagan, ronald0041D150 0.00
4325 roosevelt, teddt0041D150 0.00
4532 wilson, woodrow0041D150 0.00
4624 truman, harry0041D150 0.00
5151 roosevelt, franklin0041D150 0.00
5767 jefferson, tom0041D150 0.00
7856 johnson, lyndon0041D150 0.00
______________________________________________

Usage(kwh) Amount Due(dollars)
______________________________________________
Total 9021.00 669.17
Average 1288.71 95.60
Press any key to continue . . .

please help me before i shoot myself

Recommended Answers

All 3 Replies

I think that in line 89 you should put [i] after your usagekwh array in the cout statement.

Just had a first glance at your code though.

What mrboolf said is correct, you're displaying the address of the costs array.

Also, put line 91 before the output in line 89 - you are not storing the computed cost to the cost array till after display.

The <<fixed<<showpoint<<setprecision(2) are sticky settings, they don't need to be in the output statement that repeats. You probably do want to have both << left and << right manipulators in order to left align the names but right align the numbers. You've got an extra setw( ) in there as well.

And, if I may make a couple comments on style.

You have main's variables in global space (lines 8-15). This is generally frowned upon - variables that are global can be modified by all functions, and eventually you'll do that. It makes for tougher debugging of logic and reduces portability of the code to another project.

You ask the user to tell your program how many customers to read in from the file - this is looking for trouble! What if the user enters too large a number, or too small? Two approaches:
- the file's very first line contains a count of the records that follow
- better - just keep reading till you run out of data in the file or there is no more room in your data arrays to hold more.

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.