calculation problem!sometimes does wrong and absurb calculations

i'm just a beginner here..so forgive me if i make some mistakes ><!here's the problem,sometimes during compiling,i input a correct number but the sub total output gives a absurdly huge number and sometimes it just works normally,i don't know what is wrong here.i hope u guys can help n enlighten me!thanks!








#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <windows.h>
using namespace std;

int main()
{
ofstream file;
file.open("grcr2.txt", ios::in|ios::out|ios::trunc);

if (!file.is_open())
{
cout<<"error while opening the file";

}

else
{

int z;
char ans, y,n;
const int num1=10000;
cout<<"Enter number of items: ";
cin>>z;
if(z<=0)
{
cout<<"invalid number of items.";
return 0;
}
string name[num1]={""};
string date[num1]={""};
double number[num1]={NULL},cost[num1]={NULL}, TC[num1]={NULL},cash,change1,TTC;
cin.ignore(1024,'\n');


for(int i=0;i<z;i++)
{
cout << "\nEnter name of ITEM "<< i+1 <<"(t to terminate): ";
getline(cin,name[i]);
if(name[i]=="t")
{
name[i]="";
break;
}
cout << "Enter date of purchase: ";
cin >> date[i];
cout << "Enter amount of item: " ;
cin >> number[i];
cout << "Enter the cost of item: ";
cin >> cost[i];
cin.ignore(1024,'\n');
TC[i]=number[i] * cost[i];

TTC += TC[i]; //this line is the calculation for sub total i dont know if this code will cause some glitches or not.



}
file << "\nItem" << std::setw(25) << " Date" << std::setw(15)
<< " Amount "<< std::setw(17) <<" Cost of item" <<std::setw(17)<< "Total cost\n";
file << std::setfill('-') << std::setw(80) << "-" << std::endl;
file << std::setfill(' ');
for (int i=0; i<z; i++)
{
file <<"ITEM"<< i+1 << ": ";
if(i+1==10)
{
file << std::setw(18) << std::left;
}
else
file << std::setw(19) << std::left;
file << name[i] << std::setw(12) << std::left;
file << date[i] <<"x"<< std::setw(11) << std::left;
file << fixed <<setprecision(0) << number[i] <<"$"<< std::setw (17) << std::left;
file << fixed << setprecision(2) <<cost[i]<<"$";
file <<TC[i]<<endl;
}
file << std::setfill('-') << std::setw(80) << "-" << std::endl;
file << std::setfill(' ');
cout <<fixed<< "sub total: $"<<setprecision(2)<<TTC<< endl;
file <<fixed<<"sub total : $"<<setprecision(2)<<TTC<<endl;
cout <<"payment details -- cash: $";
cin >> cash;
file <<fixed<<"payment details -- cash: $"<<setprecision(2)<<cash<<endl;

change1 = cash - TTC;

if(change1>= -0)
{
cout<<"change due: $"<<fixed<<setprecision(2)<<change1<<"\n" ;
file<<"change due: $"<<fixed<<setprecision(2)<<change1<<"\n" ;

}

else
{
cout<<" NOT ENOUGH CASH!"<<endl;
file<<" NOT ENOUGH CASH!"<<endl;
}



file.close();
cout<<"Do you want to open textfile?(input y or n): ";
cin>>ans;
if(ans=='y')
{
ShellExecute(NULL,"open","notepad.exe","grcr2.txt",NULL,SW_SHOWNORMAL);
system("PAUSE");
}
return 0;
}

}
gives a absurdly huge number and sometimes it just works normally
This is usually a sign for uninitialized variable. Indeed TTC is uninitialzed and hence this TTC += TC[i]; adds to a random number.
so i'll just have to double TTC = 0.00; will that solve the problem?
Last edited on
I'd suggest that you test it. Very likely.
thanks so much!!it is working fine now!i could'nt see the problem untill now xp
Topic archived. No new replies allowed.