March 25th, 2003, 03:38 AM
-
Problems printing float to screen using cout
This is an annoyance to me!
I am learning C++ in uni, the program i am creating uses functions and things, i call the show bill function. an my error occurs when i try to print the totLoc variable to screen. I am getting and invalid floating point stack or something like that!
Can anyone have a look at my code to see where my problem lies.
FYI the program is a telephon bill generator that has inputs of telephone number, local units, national units, then prints the phone bill to screen and then offers to save it to a file.
My Code---------------
//Author - Shane Jones
//Program Date - 21st March 2003
//Telephone bill program
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
int getPrice(int unitLoc, int unitNat, float& totCost, float& totLoc, float& totNat);
float vatCalc(float total, float& vat, float& newCost);
float showBill(char telNo[22], int unitLoc, int unitNat, float totLoc, float totNat, float totCost, float vat, float newCost);
int saveToFile(char telNo[22], int unitLoc, int unitNat, float totLoc, float totNat, float totCost, float vat, float newCost);
/******************************************************************************/
void main(void)
{
int count, cust;
int unitLoc, unitNat;
char telNo[22];
float totCost, totLoc, vat, totNat, newCost;
cout << "Enter the number of customers - ";
cin >> cust;
for (count = 1; count <= cust; count++) //loop, number of customers, times
{
cout << "Enter customer number " << count;
cout << "'s Telephone number with no spaces\nTel No - ";
cin >> telNo;
cout << "Enter local rate units - ";
cin >> unitLoc;
cout << "Enter local rate units - ";
cin >> unitNat;
getPrice(unitLoc, //call to getPrice...
unitNat,
totCost,
totLoc,
totNat);
vatCalc(totCost, //call to vatCalc...
vat,
newCost);
showBill(telNo, //call to showBill...
unitLoc,
unitNat,
totLoc,
totNat,
totCost,
vat,
newCost);
}//end for...
getch();
}//end void...
/******************************************************************************/
int getPrice(int unitLoc, //local rate units...
int unitNat, //national rate units...
float& totCost, //total cost returned...
float& totLoc, //total cost of local rate...
float& totNat) //total cost of national rate...
{
const float locRate = 0.05;
const float natRate = 0.10;
totLoc = unitLoc + locRate;
totNat = unitNat + natRate;
totCost = totLoc + totNat;
}//end getPrice...
/******************************************************************************/
float vatCalc(float total, //total before vat...
float& vat, //cost of vat...
float& newCost) //total after vat...
{
vat = total / 100 * 17.5;
newCost = total - vat;
}//end vatCalc...
/******************************************************************************/
float showBill(char telNo[22],
int unitLoc,
int unitNat,
float totLoc,
float totNat,
float totCost,
float vat,
float newCost)
{
int i, n; //for the loops...
char choice;
clrscr(); //clear screen...
for (i=1;i<=60;i++) cout << "-";
cout << endl << "| ";
cout << "Phone bill for : " << telNo << "\t\t\t\t |";
cout << endl << "| ";
cout << "Local units : " << unitLoc << "units @ " << "0.05p per unit = ";
cout << totLoc << "\t|";
cout << "National units : " << unitNat << "units @ " << "0.10p per unit = ";
cout << totNat << "\t|";
cout << "|";
for (i=1;i<=58;i++) cout << "-";
cout << "|" << endl << "| ";
cout << "Total cost of bill(excl. vat) = " << totCost << "\t|";
cout << endl << "| ";
cout << "Cost of V.A.T. = " << vat << "\t\t|";
cout << endl << "| ";
cout << "Total cost of bill(incl. vat) = " << newCost << "\t|";
cout << endl;
for (i=1;i<=60;i++) cout << "-";
cout << endl << endl;
cout << "Do you want to save this phone bill as " << telNo << ".txt? Y/N";
cin >> choice;
n = 0;
while (n = 0)
{
switch (choice)
{
case 'y':
case 'Y':{//call saveToFile...
n = 1; //to end loop...
}
case 'n':
case 'N':{//end this function...
n = 1; //to end loop...
}
default :{
cout << "Please use Y or N? Do you want to save ";
cout << "this phone bill as " << telNo << ".txt? Y or N";
cin >> choice;
n = 0; //to carry on loop...
}
}//end switch...
}//end while...
}//end showBill...
/******************************************************************************/
int saveToFile(char telNo[22],
int unitLoc,
int unitNat,
float totLoc,
float totNat,
float totCost,
float vat,
float newCost)
{
char filename[27];
name = telno + ".txt";
ifstream toFile(filename, ios::in) //Opens file for input...
tofile << "Telephone number - " << telNo << "\n\n";
tofile << "National units : " << unitNat << "units @ " << "0.10p per unit = ";
tofile << totNat << "\n";
tofile << "Local units : " << unitLoc << "units @ " << "0.05p per unit = ";
tofile << totLoc << "\n\n";
tofile << "Total cost of bill(excl. vat) = " << totCost << "\n";
tofile << "Cost of V.A.T. = " << vat << "\n\n|";
tofile << "Total cost of bill(incl. vat) = " << newCost;
tofile.close(); //Close file after input...
-----------------------
thanks in advance