Because things, these days, seem a little 'slow' around this forum, please forebare this old post, (that was recently resurrected by an inquiring mind), and humour this 'update', in good faith that this update might be appreciated by some beginning students of C++ ... :)
// beginnerBadCppExampleCodeBeforeFixedUp.cpp // 2013-08-14 //
// This following 'Supposed example' of good code is ...
// re-worked ...
// to demo an acceptable coding style for current C++ students ...
#include<iostream.h>
#include<conio.h>
float no,price[50],total[50],qty[50],tt;
char item[30];
void main()
{
int i,j;
clrscr();
cout<<"\n\n\t\t\tRaje's Billing Software\n\n";
cout<<"\nEnter The Number of Products you'v Purchased:";
cin>>no;
cout<<"\n\nSL No\tITEM NAME\tQUANTITY\tPRICE/ITEM\n\n";
for(i=1;i<=no;i++)
{
cout<<i;
cout<<"\t";
cin>>item;
cout<<"\t";
cin>>qty[i];
cout<<"\t";
cin>>price[i];
total[i]=price[i]*qty[i];
cout<<"\n\n";
}
for(i=1;i<=no;i++)
{
tt=tt+total[i];
}
cout<<"\n\n\t\t\tTOTAL:"<<tt<<" Rupees";
cout<<"\n\n\t\t\tTHANK YOU VISIT AGAIN.....";
getch();
}
As DaniWeb Nick Evans rightly indicated (a long time ago) ...
I'm sorry to tell you, but this code is just horrible;
people should never ever use this snippet.
1. void main() does not exist in standard C++
2. neither does iostream.h or conio.h.
They are only used by Turbo C which is only used by people
from India and the Philippines for some reason.
The rest of the world uses IDE's from this millennium.
3. Global variables. Yuck.
4. No indention. Double yuck.
5. neither getch() or clrsrc() are valid C++ functions
6. No error control on input what-so-ever."
Ok ...
here is how it might be re-worked ...
to get a 'pass' as acceptable student code 'these days' ...
// beginnerBadCppExampleCodeFixedUp.cpp // 2013-08-14 //
#include <iostream> // use <iosteam ... (NOT <iostream.h>)
#include <iomanip> // re. setw, precision, fixed
#include <string> // use C++ string unless C strings are REALLY needed //
//using namespaces std; // better to not use here to avoid 'name clashes' //
const int MAX_ARY_SIZE = 2; // keep small while testing/debugging ... //
// can use an array of data struct's
// (or better, use a C++ STL vector, in place of a dynamic array)
struct Product
{
// 3 data members in this data 'record' / data 'struct'
std::string itemName;
int numItems;
double unitPrice; // in local units per item //
// C++ struct (or class) can have functions/methods to process data
// Note: total can be found from numItems and unitPrice ... so ...
// NO need to store total (i.e. total is NOT independant input data)
double get_total() const { return numItems * unitPrice; }
} ; // <-- note semi-colon to tell compiler 'struct is done' //
// could use here ... (to validate input) ...
// TWO functions for input, one for int and one for double ...
// but why not just use ONE 'template function' instead ...
template< typename T >
T takeIn( const std::string& msg, const T& min, const std::string& errMsg = "" )
{
T val;
while( true )
{
std::cout << msg << std::flush;
if( std::cin >> val && std::cin.get() == '\n' && val >= min )
break;
else
{
std::cout << errMsg;
std::cin.clear();
std::cin.sync();
}
}
return val;
}
// returns a non-empty string with CAPS on ALL first letters ...
// defaults to noEmpty ,,,
// if noEmpty set to false, then allows an empty string as valid input
std::string takeInString( const std::string& msg, bool noEmpty = true )
{
std::string val;
for( ; ; )
{
std::cout << msg << std::flush;
getline( std::cin, val );
size_t len = val.size();
if( len )
{
val[0] = toupper( val[0] );
size_t pos = 0;
while( (pos = val.find( ' ', pos )) != std::string::npos )
{
++pos;
if( pos < len )
val[pos] = toupper( val[pos] );
else break;
}
}
// else ...
if( !len && noEmpty )
std::cout << "\nBlank line not valid here ...\n";
else break;
}
return val;
}
char takeInChar( const std::string& msg )
{
std::cout << msg << std::flush;
std::string reply;
getline( std::cin, reply );
if( reply.size() )
return reply[0];
// else ...
return 0;
}
bool more()
{
if( tolower( takeInChar( "More (y/n) ? " )) == 'n' )
return false;
// else ...
return true;
}
int main()
{
Product cart[MAX_ARY_SIZE]; // get room to hold this many products
using std::cout;
cout << "\n\t\t\tRaje's Billing Software\n"
<< "\nBelow you will be asked to enter ...\n"
<< "\nITEM NAME,\nQUANTITY\nUNIT PRICE\n\nfor each item.\n";
int no = 0; // inital num products in cart ... //
do
{
cout << "\nFor item " << (no+1) << ", please enter ... \n";
cart[no].itemName = takeInString( "Item name : " );
cart[no].numItems =
takeIn< int >( "Quantity : ", 1, "\nValid input is a numner >= 1\n" );
cart[no].unitPrice =
takeIn< double >( "Unit price : ", 1.0,
"\nValid input is a number >= 1.0\n" );
cout << '\n';
++no;
if( no == MAX_ARY_SIZE )
{
cout << "\nYou have reached the max cart size "
<< "allowed here of " << no << " items.\n"
<< "Proceeding to 'Check-Out' right now ...\n";
break;
}
}
while( more() );
double sumTotal = 0.0;
// NOTE! In C/C++ ... arrays start with index 0 ...
using std::setw;
cout << "\nYour cart ...\n"
<< setw( 20 ) << "Item"
<< setw( 15 ) << "Quanity"
<< setw( 15 ) << "Unit Price"
<< setw( 15 ) << "Line Total"
<< '\n';
cout << std::fixed << std::setprecision( 2 );
// show 2 decimal places in monet
for( int i = 0; i < no; ++ i )
{
cout << ' ' << setw( 19 ) << cart[i].itemName
<< ' ' << setw( 14 ) << cart[i].numItems
<< ' ' << setw( 14 ) << cart[i].unitPrice
<< ' ' << setw( 14 ) << cart[i].get_total()
<< '\n';
sumTotal += cart[i].get_total();
}
cout << "\n\nTOTAL: " << sumTotal << " local units of money."
<< "\n\nTHANK YOU FOR YOUR PURCHASE TODAY."
<< "\n\nPLEASE VISIT AGAIN SOON.\n";
takeInString( "\nPress 'Enter' to continue/exit ... ", false );
}