I am trying to run below program which i am developing to calculate electricity bill to reduce workload at office as this is a repetitive task so why not use software to do that (i will use this code to do whole calculation process automatically using a database and then write in a text file which will be exported in excel) i am looking to complete this program before end of this month.
It runs perfectly without any error but only for 1 time, when the loop start again then it don't take any input ( but do show all cout statements) and go straight to end of loop where i can press 0 to re run the loop
also, if i use
cout<<"Enter Name: ";
cin.get(name,100);
this code ( to enter name ) after getting rank input then this program skips the input of name and do to next step. It do same thing if i use cin.getline instead of cin.get.
I am using visual studio 2008 to compile this c++ code as win32 application. ( name contains space to thus i need to use method other than cin to get full name and if i use cin.get for every string input then i get runtime error of "stack got corrupted around name")
Any one any idea what is wrong happening with this code?
It runs perfectly without any error but only for 1 time, when the loop start again then it don't take any input ( but do show all cout statements) and go straight to end of loop where i can press 0 to re run the loop
also, if i use
cout<<"Enter Name: ";
cin.get(name,100);
this code ( to enter name ) after getting rank input then this program skips the input of name and do to next step. It do same thing if i use cin.getline instead of cin.get.
I am using visual studio 2008 to compile this c++ code as win32 application. ( name contains space to thus i need to use method other than cin to get full name and if i use cin.get for every string input then i get runtime error of "stack got corrupted around name")
C++:
#include<iostream>
#include<conio.h>
#include<fstream>
#include<string>
#include<stdio.h>
using namespace std;
void main()
{
char choice='0';
float units=0, months=0, days=0, upm=0, total=0, fc=0, free=0, wc=0, date1[3], date2[3],wu,sc=0,et=0;
char sn[5], rank[5], name[10], qtr[5];
do
{
ofstream file;
file.open("bill.txt",ios::app);
units=0, months=0, days=0, upm=0, total=0, fc=0, free=0, wc=0, date1[3], date2[3],wu,sc=0,et=0;
//sn[], rank[], name[], qtr[5];
string str;
//clrscr();
cout<<"Enter Name: ";
cin.get(name,100);
cout<<"Enter QTR No: ";
cin>>qtr;
cout<<"Enter service Number: ";
cin>>sn;
cout<<"Enter Rank: ";
cin>>rank;
cout<<"Enter electricity units: ";
cin>>units;
cout<<"Enter monthly water units: " ;
cin>>wu;
cout<<"\nEnter starting date \nDay: ";
cin>>date1[0];
cout<<"Month: ";
cin>>date1[1];
cout<<"Year: ";
cin>>date1[2];
cout<<"\nEnter ending date\nDay: ";
cin>>date2[0];
cout<<"Month: ";
cin>>date2[1];
cout<<"Year: ";
cin>>date2[2];
months=date2[1]-date1[1];
days=1+(date2[0]-date1[0]);
months+=(date2[2]-date1[2])*12;
if (days>=30)
{days=0;
months++;}
upm=units/(months+(days/30));
if (upm>400)
total+=(((upm-400)*7.3)+1190+800)*(months+(days/30));
else if (upm>200)
total+=(((upm-200)*5.95)+800)*(months+(days/30));
else
total+=(upm*4)*(months+(days/30));
//cout<<total;
//getch();
fc=(40*(months+(days/30)));
free=(400*(months+(days/30)));
wc=(209.62*months)+(6.9873*days);
wu=(wu*months)+(wu*days/30);
cout<<"\nUnits per months =: "<<upm;
cout<<"\nUnits consumed ===: "<<units;
if (units-(free/4)<=0)
cout<<"\nUnits charged ====: "<<0;
else
cout<<"\nUnits charged ====: "<<units-(free/4);
cout<<"\nTotal free units =: "<<free/4;
cout<<"\nFC ===============: Rs"<<fc;
cout<<"\nWater ============: "<<wu;
cout<<"\nTime =============: "<<months<<" months & "<<days<<" days";
cout<<"\nwater charge =====: RS"<<wc;
cout<<"\nTotal ============: Rs";
if ((total-free)>0)
{total-=free;
cout<<total;}
else
{total=0;
cout<<total;}
sc=(total+fc)*0.08;
et=((total)+total*0.08)*0.05;
file<<"\t"<<qtr<<"\t"<<sn<<"\t"<<rank<<"\t\t"<<units<<"\t";
if ((total-free)>0)
file<<free/4<<"\t"<<units-(free/4)<<"\t"<<total<<"\t"<<fc<<"\t"<<sc<<"\t"<<et<<"\t"<<wu*1000<<"\t"<<wc<<"\t"<<total+wc+fc+sc+et<<"\t"<<date1[0]<<"/"<<date1[1]<<"/"<<date1[2]<<"\t"<<date2[0]<<"/"<<date2[1]<<"/"<<date2[2]<<endl;
else
file<<free<<"\t"<<0<<"\t"<<total<<"\t"<<fc<<"\t"<<sc<<"\t"<<et<<"\t"<<wu*1000<<"\t"<<wc<<"\t"<<total+wc+fc+sc+et<<"\t"<<date1[0]<<"/"<<date1[1]<<"/"<<date1[2]<<"\t"<<date2[0]<<"/"<<date2[1]<<"/"<<date2[2]<<endl;
cout<<"\n\nPress [0] to run again else any other key to exit : ";
choice=getch();
file.close();
}while (choice=='0');
}
Any one any idea what is wrong happening with this code?