Hello Integrated,
Based on the clarification, here's a program based on the following:
- closely matches the design of the Pascal program you provided
- adds customer deposit and withdrawl (from the clarification)
I split out the Calculate function so you can run it separately as
well. If that isn't needed, remove it from the menu and call it
directly from the Input function.
A few comments on the code posted below:
- I am including the Makefile I used to build the application on my
Unix system. The flags add debug data and turn on all compiler
warnings. The g++ compiler I used issued no warnings from this code.
- I used the more modern header file references <iostream> and not
<iostream.h>. Add the .h if needed for your system. I also fully
specified references to cout and similar items.
- You can modify the number of accounts (NOACCT), length of name
(NAMELEN), and account number length (ACCLEN) and the rest of the code
should work just fine.
- Keep BIGLEN greater than NAMELEN and ACCLEN; it is used for
temporary strings used to help input (see getStr next).
- getStr was added to handle input of a bounded string and ensure the
value is null terminated.
- deposits and withdrawls must be positive. Now that I think about
it, it would probably good to add an if statement to ensure the amount
never goes negative. Change withdrawl to read
else if (amount > customers[i].amount)
std::cout << "Amount £" << amount << " is too big. Ignored.";
before the else clause.
- the arrays start at zero (not one), so listEnd will be one less
than the Pascal version.
If this program is unclear or does not meet your needs in any way,
don't hesitate to make a clarification request.
--Maniac
==========
Makefile
CPPFLAGS=-g -Wall
ktc : ktc.cpp
==========
ktc.cpp
// ktc.cpp - written by Maniac on 2004/02/09
// A translation of Pascal source code provided by integrated-ga
// http://answers.google.com/answers/threadview?id=304408
// header files
#include <iostream>
#include <cstdlib>
// a few helper values
#define BIGNUM 99
#define NL '\n'
#define NOACC 20
#define NAMELEN 16
#define ACCLEN 12
// Account record definition, name, account number, type, amount
// and new balance (in order)
struct accRec {
char names[NAMELEN];
char accNo[ACCLEN];
char accType;
double amount;
double newBal;
};
typedef accRec xList[NOACC];
// wait on user input until a newline
void Keypress () {
std::cout << "\nTo continue press ENTER . . .";
std::cin.ignore(BIGNUM,NL);
}
// query user for menu selection
void mMenu (char &c) {
const char message[] = "KTC Banking System";
std::cout << NL << NL << message << NL << NL;
std::cout << "Customers Record\n\n";
std::cout << "==========================================\n";
std::cout << "1. Input customers details\n";
std::cout << "2. Display accounts details\n";
std::cout << "3. Customer deposit\n";
std::cout << "4. Customer withdrawl\n";
std::cout << "5. Calculate\n";
std::cout << "9. EXIT\n";
std::cout << "Select from one of the above options ";
std::cin.get(c);
std::cin.ignore(BIGNUM, NL);
}
// helper function to read a line of data and copy it
// into another string; truncates with null just in case
void getStr (char x[],
int length) {
char temp[BIGNUM];
std::cin.get(temp, BIGNUM);
std::cin.ignore(BIGNUM, NL);
strncpy(x, temp, length);
x[length-1] = '\0';
}
// Input the customer data
void Input (xList &customers,
int &listEnd) {
char choice; // Y or N
char temp[BIGNUM];
do {
if (listEnd+1 >= NOACC) {
std::cout << "Customer table full.\n";
break;
}
listEnd++;
std::cout << "Customers Details\n";
std::cout << "=================\n";
std::cout << "Account holders name =====> ";
getStr(customers[listEnd].names, NAMELEN);
std::cout << "Account number ===========> ";
getStr(customers[listEnd].accNo, ACCLEN);
do {
std::cout << "Account type (C, S) ======> ";
std::cin.get(choice);
std::cin.ignore(BIGNUM, NL);
} while ((choice != 'C') && (choice != 'S'));
customers[listEnd].accType = choice;
std::cout << "Amount ===================>£";
std::cin.get(temp, BIGNUM);
std::cin.ignore(BIGNUM, NL);
customers[listEnd].amount = strtod(temp, NULL);
// don't leave uninitialized
customers[listEnd].newBal = customers[listEnd].amount;
do {
std::cout << "Enter another customer? (Y/N) ";
std::cin.get(choice);
std::cin.ignore(BIGNUM, NL);
} while ((choice != 'Y') && (choice != 'N'));
} while (choice != 'N');
}
// format the information and generate / display a report
void Display (xList customers,
int listEnd) {
int i=0;
char temp[BIGNUM];
if (listEnd >= 0) {
std::cout << "====================\n";
std::cout << "STATEMENT OF ACCOUNT\n";
std::cout << "====================\n" << NL;
snprintf(temp, BIGNUM, "%*s%*s%10s%15s%15s\n",
NAMELEN+2,"Account name",ACCLEN+2,"Acct No.","A/C Type",
"Old Balance","New Balance");
std::cout << temp;
snprintf(temp, BIGNUM, "%*s%*s%10s%15s%15s\n",
NAMELEN+2,"------------",ACCLEN+2,"--------","--------",
"-----------","-----------");
std::cout << temp;
do {
snprintf(temp, BIGNUM, "%*s%*s%10c%15.2f%15.2f\n",
NAMELEN+2,customers[i].names,
ACCLEN+2,customers[i].accNo,
customers[i].accType,
customers[i].amount,
customers[i].newBal);
std::cout << temp;
} while (++i<=listEnd);
std::cout << NL;
}
else {
std::cout << "No Accounts in the system.\n";
}
Keypress();
}
// Process customer deposit
void Deposit(xList customers,
int listEnd) {
char accNo[ACCLEN];
char temp[BIGNUM];
int i = 0;
double amount;
if (listEnd >= 0) {
std::cout << "Account number ===========> ";
getStr(accNo, ACCLEN);
do {
if (strncmp(accNo, customers[i].accNo, ACCLEN)==0)
break;
} while (++i<=listEnd);
if (i>listEnd)
std::cout << "Account '" << accNo << "' not found.\n";
else {
std::cout << "Deposit Amount ===========>£";
std::cin.get(temp, BIGNUM);
std::cin.ignore(BIGNUM, NL);
amount = strtod(temp, NULL);
if (amount<0.0)
std::cout << "Amount £" << amount << " is negative. Ignored.";
else
customers[i].amount += amount;
}
}
else {
std::cout << "No Accounts in the system.\n";
}
}
// Process customer withdrawl
void Withdrawl(xList customers,
int listEnd) {
char accNo[ACCLEN];
char temp[BIGNUM];
int i = 0;
double amount;
if (listEnd >= 0) {
std::cout << "Account number ===========> ";
getStr(accNo, ACCLEN);
do {
if (strncmp(accNo, customers[i].accNo, ACCLEN)==0)
break;
} while (++i<=listEnd);
if (i>listEnd)
std::cout << "Account '" << accNo << "' not found.\n";
else {
std::cout << "Withdrawl Amount =========>£";
std::cin.get(temp, BIGNUM);
std::cin.ignore(BIGNUM, NL);
amount = strtod(temp, NULL);
if (amount<0.0)
std::cout << "Amount £" << amount << " is negative. Ignored.";
else
customers[i].amount -= amount;
}
}
else {
std::cout << "No Accounts in the system.\n";
}
}
// Update balances based on calculation
void Calculate(xList customers,
int listEnd) {
int i=0;
if (listEnd >= 0) {
do {
switch (customers[i].accType) {
case 'S':
if (customers[i].amount<5000.0)
customers[i].newBal = customers[i].amount*(1.0+10.0/100.0);
else
customers[i].newBal = customers[i].amount*(1.0+12.0/100.0);
case 'C':
if (customers[i].amount<100.0)
customers[i].newBal = customers[i].amount*(1.0+5.0/100.0);
else
customers[i].newBal = customers[i].amount*(1.0+7.0/100.0);
}
} while (++i<=listEnd);
}
else {
std::cout << "No Accounts in the system.\n";
}
}
// Main program, loop on menu selection and dispatch
// based on the user input.
int main () {
char xSelect;
xList customers;
int listEnd = -1;
do {
mMenu(xSelect);
switch (xSelect) {
case '1':
Input(customers, listEnd);
break;
case '2':
Display(customers, listEnd);
break;
case '3':
Deposit(customers, listEnd);
break;
case '4':
Withdrawl(customers, listEnd);
break;
case '5':
Calculate(customers, listEnd);
break;
case '9':
break;
default:
std::cout << "Don't understand " << xSelect << NL;
}
} while (xSelect != '9');
std::cout << "\n\nThanks for using KTC banking system\n";
} |
Request for Answer Clarification by
integrated-ga
on
12 Feb 2004 17:28 PST
hi maniac- i just have few suggestions, firstfull thank you.
i have a C++ borland 3.1 compiler i have tryed to compile it but it
didn't work soo i will list the errors and you annotate it with
solutions.
directory> out put directory >is set as c: drive
thank you
Compiling NONAME00.CPP:
Error NONAME00.CPP 8: Unable to open include file 'IOSTREAM'
Error NONAME00.CPP 9: Unable to open include file 'CSTDLIB'
Error NONAME00.CPP 36: Type qualifier 'std' must be a struct or class name
Error NONAME00.CPP 36: Statement missing ;
Error NONAME00.CPP 37: Type qualifier 'std' must be a struct or class name
Error NONAME00.CPP 37: Statement missing ;
Error NONAME00.CPP 45: Type qualifier 'std' must be a struct or class name
Error NONAME00.CPP 45: Statement missing ;
Error NONAME00.CPP 46: Type qualifier 'std' must be a struct or class name
Error NONAME00.CPP 46: Statement missing ;
Error NONAME00.CPP 47: Type qualifier 'std' must be a struct or class name
Error NONAME00.CPP 47: Statement missing ;
Error NONAME00.CPP 48: Type qualifier 'std' must be a struct or class name
Error NONAME00.CPP 48: Statement missing ;
Error NONAME00.CPP 49: Type qualifier 'std' must be a struct or class name
Error NONAME00.CPP 49: Statement missing ;
Error NONAME00.CPP 50: Type qualifier 'std' must be a struct or class name
Error NONAME00.CPP 50: Statement missing ;
Error NONAME00.CPP 51: Type qualifier 'std' must be a struct or class name
Error NONAME00.CPP 51: Statement missing ;
Error NONAME00.CPP 52: Type qualifier 'std' must be a struct or class name
Error NONAME00.CPP 52: Statement missing ;
Error NONAME00.CPP 53: Type qualifier 'std' must be a struct or class name
Error NONAME00.CPP 53: Statement missing ;
Error NONAME00.CPP 54: Type qualifier 'std' must be a struct or class name
Error NONAME00.CPP 54: Too many error or warning messages
|
Clarification of Answer by
maniac-ga
on
12 Feb 2004 20:31 PST
Hello Integrated,
I don't have that specific compiler (Borland), but the first problem
is that the header file references do not appear to be correct. From
your previous question
http://answers.google.com/answers/threadview?id=303912
you used:
#include<iostream.h>
and
#include <cstdlib>
in that file. That construct compiled OK on my system (and I assume it
did on yours as well).
The first two errors on this program are for the same two header
files. A few suggestions:
- add .h to #include <iostream> to match your previous program. Do
the same for cstdlib if needed. [please also review the comments I
made in the original answer]
- check the project file to ensure these system level includes can be accessed
If we can't get past these errors, the rest of the program won't work.
The remaining errors appear to be references to
std::cout
std::cin
std::cin.ignore
and so on. These may be fixed when the header files are fixed. If not,
there are two ways to make the fix:
1. Remove std:: from each place it occurs. The older C++ standard did
not have namespaces so adding std:: was not necessary. Since you are
using Borland 3.1 (I believe Borland is up to 5.x now), the compiler
you are using may not support namespaces. Removing namespace
references will then fix the program.
2. Same as #1, but add
using std::cin;
using std::cout;
and similar statements at the top. This matches the structure in the
last program you had posted. This assumes your compiler supports
namespaces but has some other problem with the constructs as I coded
them.
Let me know how this works out.
--Maniac
|
Request for Answer Clarification by
integrated-ga
on
24 Feb 2004 09:50 PST
hi maniac-ga apology on the late clarification, on the last
clarification there was 25 error now iam only down to 8 errors. simply
because some of the functions dont work in my compiler i will outline
the problems please help me find solutions to it.
the function snprintf doesn't exist in my compiler.
i have toke out all the std:: and i turned iostream > iostream.h
but cstdlib doesn't exist in my library even if i add .h it still wont
work, and could you please give me instructions on how to include file
or header file, how to make them work.
Compiling KTC.CPP:
Error KTC.CPP 8: Unable to open include file 'CSTDLIB.H'
Error KTC.CPP 67: Function 'strncpy' should have a prototype
Error KTC.CPP 100: Function 'strtod' should have a prototype
Error KTC.CPP 124: Function 'snprintf' should have a prototype
Error KTC.CPP 162: Function 'strncmp' should have a prototype
Error KTC.CPP 171: Function 'strtod' should have a prototype
Error KTC.CPP 196: Function 'strncmp' should have a prototype
Error KTC.CPP 205: Function 'strtod' should have a prototype
Warning KTC.CPP 280: Function should return a value
|
Request for Answer Clarification by
integrated-ga
on
24 Feb 2004 09:53 PST
and this is how the program is looking so far.
// ktc.cpp - written by Maniac on 2004/02/09
// A translation of Pascal source code provided by integrated-ga
// http://answers.google.com/answers/threadview?id=304408
// header files
#include <iostream.h>
#include <cstdlib.h>
// a few helper values
#define BIGNUM 99
#define NL '\n'
#define NOACC 20
#define NAMELEN 16
#define ACCLEN 12
// Account record definition, name, account number, type, amount
// and new balance (in order)
struct accRec {
char names[NAMELEN];
char accNo[ACCLEN];
char accType;
double amount;
double newBal;
};
typedef accRec xList[NOACC];
// wait on user input until a newline
void Keypress () {
cout << "\nTo continue press ENTER . . .";
cin.ignore(BIGNUM,NL);
}
// query user for menu selection
void mMenu (char &c) {
const char message[] = "KTC Banking System";
cout << NL << NL << message << NL << NL;
cout << "Customers Record\n\n";
cout << "==========================================\n";
cout << "1. Input customers details\n";
cout << "2. Display accounts details\n";
cout << "3. Customer deposit\n";
cout << "4. Customer withdrawl\n";
cout << "5. Calculate\n";
cout << "9. EXIT\n";
cout << "Select from one of the above options ";
cin.get(c);
cin.ignore(BIGNUM, NL);
}
// helper function to read a line of data and copy it
// into another string; truncates with null just in case
void getStr (char x[],
int length) {
char temp[BIGNUM];
cin.get(temp, BIGNUM);
cin.ignore(BIGNUM, NL);
strncpy(x, temp, length);
x[length-1] = '\0';
}
// Input the customer data
void Input (xList &customers,
int &listEnd) {
char choice; // Y or N
char temp[BIGNUM];
do {
if (listEnd+1 >= NOACC) {
cout << "Customer table full.\n";
break;
}
listEnd++;
cout << "Customers Details\n";
cout << "=================\n";
cout << "Account holders name =====> ";
getStr(customers[listEnd].names, NAMELEN);
cout << "Account number ===========> ";
getStr(customers[listEnd].accNo, ACCLEN);
do {
cout << "Account type (C, S) ======> ";
cin.get(choice);
cin.ignore(BIGNUM, NL);
} while ((choice != 'C') && (choice != 'S'));
customers[listEnd].accType = choice;
cout << "Amount ===================>£";
cin.get(temp, BIGNUM);
cin.ignore(BIGNUM, NL);
customers[listEnd].amount = strtod(temp, NULL);
// don't leave uninitialized
customers[listEnd].newBal = customers[listEnd].amount;
do {
cout << "Enter another customer? (Y/N) ";
cin.get(choice);
cin.ignore(BIGNUM, NL);
} while ((choice != 'Y') && (choice != 'N'));
} while (choice != 'N');
}
// format the information and generate / display a report
void Display (xList customers,
int listEnd) {
int i=0;
char temp[BIGNUM];
if (listEnd >= 0) {
cout << "====================\n";
cout << "STATEMENT OF ACCOUNT\n";
cout << "====================\n" << NL;
snprintf(temp, BIGNUM, "%*s%*s%10s%15s%15s\n",
NAMELEN+2,"Account name",ACCLEN+2,"Acct No.","A/C Type",
"Old Balance","New Balance");
cout << temp;
snprintf(temp, BIGNUM, "%*s%*s%10s%15s%15s\n",
NAMELEN+2,"------------",ACCLEN+2,"--------","--------",
"-----------","-----------");
cout << temp;
do {
snprintf(temp, BIGNUM, "%*s%*s%10c%15.2f%15.2f\n",
NAMELEN+2,customers[i].names,
ACCLEN+2,customers[i].accNo,
customers[i].accType,
customers[i].amount,
customers[i].newBal);
cout << temp;
} while (++i<=listEnd);
cout << NL;
}
else {
cout << "No Accounts in the system.\n";
}
Keypress();
}
// Process customer deposit
void Deposit(xList customers,
int listEnd) {
char accNo[ACCLEN];
char temp[BIGNUM];
int i = 0;
double amount;
if (listEnd >= 0) {
cout << "Account number ===========> ";
getStr(accNo, ACCLEN);
do {
if (strncmp(accNo, customers[i].accNo, ACCLEN)==0)
break;
} while (++i<=listEnd);
if (i>listEnd)
cout << "Account '" << accNo << "' not found.\n";
else {
cout << "Deposit Amount ===========>£";
cin.get(temp, BIGNUM);
cin.ignore(BIGNUM, NL);
amount = strtod(temp, NULL);
if (amount<0.0)
cout << "Amount £" << amount << " is negative. Ignored.";
else
customers[i].amount += amount;
}
}
else {
cout << "No Accounts in the system.\n";
}
}
// Process customer withdrawl
void Withdrawl(xList customers,
int listEnd) {
char accNo[ACCLEN];
char temp[BIGNUM];
int i = 0;
double amount;
if (listEnd >= 0) {
cout << "Account number ===========> ";
getStr(accNo, ACCLEN);
do {
if (strncmp(accNo, customers[i].accNo, ACCLEN)==0)
break;
} while (++i<=listEnd);
if (i>listEnd)
cout << "Account '" << accNo << "' not found.\n";
else {
cout << "Withdrawl Amount =========>£";
cin.get(temp, BIGNUM);
cin.ignore(BIGNUM, NL);
amount = strtod(temp, NULL);
if (amount<0.0)
cout << "Amount £" << amount << " is negative. Ignored.";
else
customers[i].amount -= amount;
}
}
else {
cout << "No Accounts in the system.\n";
}
}
// Update balances based on calculation
void Calculate(xList customers,
int listEnd) {
int i=0;
if (listEnd >= 0) {
do {
switch (customers[i].accType) {
case 'S':
if (customers[i].amount<5000.0)
customers[i].newBal = customers[i].amount*(1.0+10.0/100.0);
else
customers[i].newBal = customers[i].amount*(1.0+12.0/100.0);
case 'C':
if (customers[i].amount<100.0)
customers[i].newBal = customers[i].amount*(1.0+5.0/100.0);
else
customers[i].newBal = customers[i].amount*(1.0+7.0/100.0);
}
} while (++i<=listEnd);
}
else {
cout << "No Accounts in the system.\n";
}
}
// Main program, loop on menu selection and dispatch
// based on the user input.
int main () {
char xSelect;
xList customers;
int listEnd = -1;
do {
mMenu(xSelect);
switch (xSelect) {
case '1':
Input(customers, listEnd);
break;
case '2':
Display(customers, listEnd);
break;
case '3':
Deposit(customers, listEnd);
break;
case '4':
Withdrawl(customers, listEnd);
break;
case '5':
Calculate(customers, listEnd);
break;
case '9':
break;
default:
cout << "Don't understand " << xSelect << NL;
}
} while (xSelect != '9');
cout << "\n\nThanks for using KTC banking system\n";
}
|
Request for Answer Clarification by
integrated-ga
on
24 Feb 2004 10:13 PST
hi maniac-ga i turned cstdlib > to stdlib.h and iam down to four errors now.
Compiling KTC.CPP:
Error KTC.CPP 67: Function 'strncpy' should have a prototype
Error KTC.CPP 124: Function 'snprintf' should have a prototype
Error KTC.CPP 162: Function 'strncmp' should have a prototype
Error KTC.CPP 196: Function 'strncmp' should have a prototype
Warning KTC.CPP 280: Function should return a value
|
Clarification of Answer by
maniac-ga
on
24 Feb 2004 17:01 PST
Hello Integrated,
I am glad to help.
I did a quick search using phrases like:
borland strncpy
borland strncmp
borland snprintf
and found
http://www.clipx.net/ng/borcpp/ng365.php
which appears to have references to some of the functions you are
having problems with. Scroll down to find strncpy and strncmp - click
on those names to get a description of the functions / how they are
used. Using strncpy and strncmp as an example, I suggest adding
#include <string.h>
to handle these references.
It appears that snprintf is a little more difficult to address. According to
http://www.drbob42.com/cbuilder/bcb4intr.htm
snprintf was added in Borland C++ 4. However, I found
http://archives.postgresql.org/pgsql-patches/2002-12/msg00186.php
which includes the following statement
#define snprintf _snprintf
which MAY fix the problem. The other alternative is to change the
calls to snprintf to sprintf and remove the BIGNUM parameter to match
the calling sequence for sprintf. To explain, snprintf is a more
"safe" version of sprintf - it only copies up to BIGNUM (or whatever
the length is) into the string.
Let me know if you need additional help - I'd be glad to provide it.
--Maniac
|
Request for Answer Clarification by
integrated-ga
on
26 Feb 2004 07:40 PST
Question ID: 310995
HI MANIAC I HAVE POSTED A NEW TOPIC BASED ON THE ACCOUNT PROGRAM. ALL
THIS PROGRAM NEEDS IS A BIT OF MODIFYING ADD NEW FUNCTIONS TO IT. IT
WILL BE ALOT MORE EASIER TO COMPILE. CAUSE THIS PROGRAM I HAVE SEND
COMPILES PERFECTLY IN MY 3.1 BORLAND COMPILER.
PLEASE WRITE PROGRAMS SO THAT A BEGINNER CAN UNDERSTAND IT WELL. WRITE
THIS PROGRAM WITHOUT USING ADVANCED FUNCTIONS AND CODES LIKE : ACCLEN.
NAMELEN AND THINGS LIKE 'XLIST'snprintf(temp, BIGNUM,
"%*s%*s%10s%15s%15s\n",THINGS LIKE THIS IS HARD FOR ME TO GRASP CAUSE
IAM A BEGINNER.
|
Clarification of Answer by
maniac-ga
on
26 Feb 2004 10:20 PST
Hello Integrated,
I don't mind clarifying some of the code already submitted (without
fee) and have done so below. I will look into the new program later
today.
About NAMELEN, ACCLEN, and so on. Both C (and C++) have a preprocessor
that allows you to substitute values or simulate functions with code.
The lines:
#define BIGNUM 99
#define NL '\n'
#define NOACC 20
#define NAMELEN 16
#define ACCLEN 12
are examples of this where NAMELEN will be replaced by 16 whenever it
appears. So the statement
char names[NAMELEN];
is interpreted by the compiler as
char names[16];
and
snprintf(temp, BIGNUM, "%*s%*s%10s%15s%15s\n",
NAMELEN+2,"Account name",ACCLEN+2,"Acct No.","A/C Type",
"Old Balance","New Balance");
is interpreted by the compiler as
snprintf(temp, BIGNUM, "%*s%*s%10s%15s%15s\n",
16+2,"Account name",12+2,"Acct No.","A/C Type",
"Old Balance","New Balance");
In this way, you can make the change in one place (the definition of
NAMELEN) and affect the code in a consistent manner in several places.
The term xList is defined as:
typedef accRec xList[NOACC];
which defines a new type, xList which is an array of NOACC (20) values
of type accRec. This simplifies the code such as:
void Input (xList &customers,
which can be coded as:
void Input (accRec &customers[NOACC],
The use of snprintf was based on the use of formatted output in your
original Pascal program. Using this as an example:
WriteLn(Names:16,AcctNo:11,AccType:8,Amount:15:2,NewBal:15:2);
the equivalent with snprintf is:
snprintf(temp, BIGNUM, "%*s%*s%10c%15.2f%15.2f\n",
NAMELEN+2,customers[i].names,
ACCLEN+2,customers[i].accNo,
customers[i].accType,
customers[i].amount,
customers[i].newBal);
std::cout << temp;
Let's decode the meaning of the format string and the parameters.
[1] The code "%*%s" will take two arguments, the first is the length
of the field and the second is the string to print. In the first
example:
NAMELEN+2,customers[i].names,
the field length is 18 (NAMELEN+2) and the string printed is the
customer name. Since the customer name is less than 18 letters, you
will have spaces added to fill the field length (just like your 16
character example in Pascal).
[2] The same code "%*%s" is used for the account name, with a field
length of 14 (ACCLEN+2).
[3] The code "10c" will print a single character in a 10 letter field.
In this case, the account type is printed with enough spaces to fill
10 characters.
[4] The code "15.2f" will print a floating point number. The total
field width is 15 letters, with two digits after the decimal point. In
this case, the amount in the account is printed.
[5] The code "15.2f" is used again for the "new balance".
[6] The code "\n" is represents a newline.
Finally the assembled string in temp is sent to cout (the standard
output). I made the field lengths slightly longer in the version I
wrote to ensure there were spaces between each value.
There are a few reasons why I wrote the code this way. The main ones are:
- formatting in C++ natively is cumbersome and requires many more
lines of code than the example I used.
- I could have used printf to write directly to the standard output,
but some C++ implementations do not coordinate printf properly with
cout. Doing this in two steps adds a line of code for each output, but
is safer to code.
- I used snprintf instead of sprintf because snprintf is "safe", it
will never write more than BIGNUM letters into the string. You may
have heard of security problems due to buffer overflows - sprintf is
one function that can cause those.
You did not mention this one, but let me comment on another coding
style I used. The sequence of steps such as:
std::cin.get(temp, BIGNUM);
std::cin.ignore(BIGNUM, NL);
amount = strtod(temp, NULL);
was used to prevent the user from confusing the application. In this
case, we are trying to read in the amount of money in the account. If
you used
std::cin >> amount;
instead, the user could enter a nonsense value (e.g., abc) and confuse
the program. The three step process reads in a reasonable amount of
characters, skips past the end of line, and then converts the numeric
value. If a nonsense value is entered, amount will be zero.
--Maniac
|
Request for Answer Clarification by
integrated-ga
on
01 Mar 2004 07:11 PST
hi maniac, hows it going with the other question let me know please.
|
Clarification of Answer by
maniac-ga
on
01 Mar 2004 19:47 PST
Hello Integrated,
I have reviewed the code you provided in the other question and made a
request for question clarification. I suggest we put further exchange
of information on that question instead of this one.
--Maniac
|