×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Whoa

## Whoa

(OP)
/* Programmer: Bill Korn
Computer#:26
Period:1
Program Title:Check.CPP
Date:11/3/99

Purpose:This program will allow the user to enter a starting
deposit. It will then process a series of deposits,withdrawals
. Then it will print the # of withdrawals, deposits, transactions
and the total of the withdrawals and deposits.\n";
*/

#include "trinity.h"

void neo();
void morpheus();
double get_deposit();
char get_choice();
double get_amount();
void setup();
void print_results(double num_w, double num_d, double total,
double total_w, double total_d, double balance,
double initial);

int main()
{
neo();
morpheus();

// Declaring Variables
double initial, change, total_d, total_w, num_d, num_w, balance, count,total;
char choice, cheryl;

// Rerun Loop
do
{

// Initializing Variables
total_d = total_w = num_d = num_w = count = 0;

// Get initial balance
initial = get_deposit();
balance = initial;

// Setting up the screen
setup();

// Do while loop regulating transactions
do
{
// Getting the choice
choice = get_choice();

// getting amount
if(choice != 'Q' && choice != 'q')
change = get_amount();

// Switch regulating options

switch (choice)
{
case 'D', 'd': total_d = total_d + change;
num_d++;
balance = balance + change;
gotoxy(29, (4 + count));
cout << setw(12) << change;
gotoxy(48, (4 + count));
cout << setw(11) << balance;
count++;
break;

case 'W', 'w': if(change > balance)
{
gotoxy(1, 23);
cout << " \t\tYou do not have enough. Press Enter";
getch();
cin.ignore();
gotoxy(1, 23);
cout << " ";
cout << " ";
break;
}

total_w = total_w + change;
num_w++;
balance = balance - change;
gotoxy(9, (4 + count));
cout << setw(15) << change;
gotoxy(48, (4 + count));
cout << setw(11) << balance;
count++;
break;
case 'Q', 'q': break;
}
}
while(choice != 'Q' && choice != 'q');

// Calculating Total
total = num_d + num_w;

// Printing the final screen
print_results(num_w, num_d, total, total_w, total_d, balance, initial);

do
{
cout << "\n\n";
cout << "\t\t Do you want to run this program again(y/n): ";
cin >> cheryl;
}
while(cheryl != 'Y' && cheryl != 'y' && cheryl != 'N' && cheryl!= 'n');

// End of Rerun Loop

}
while(cheryl != 'N' && cheryl != 'n');

return 0;
}

double get_deposit()
{
double hoody_hoo;
do
{
clrscr();
matrix(11);
cout << "\t\t Enter the initial balance: ";
cin >> hoody_hoo;
}
while(hoody_hoo <= 0);
return hoody_hoo;
}

char get_choice()
{
char blingbling;
do
{
gotoxy(61, 23);
cout << " ";
gotoxy(1, 23);
cout << "\t\t Deposit (D), Withdrawal (W), or Quit (Q): ";
cin >> blingbling;
}
while(blingbling != 'W' && blingbling != 'w' && blingbling != 'D' &&
blingbling != 'd' && blingbling != 'Q' && blingbling != 'q');
gotoxy(1, 23);
cout << " ";
cout << " ";
gotoxy(1, 24);
cout << " ";
cout << " ";
return blingbling;
}

double get_amount()
{
double btau;
do
{
gotoxy(55, 23);
cout << " ";
gotoxy(1, 23);
cout << "\t\t Enter the amount for the transaction: ";
cin >> btau;
gotoxy(1, 23);
cout << " ";
cout << " ";
gotoxy(1, 24);
cout << " ";
cout << " ";
}
while(btau < 0);
return btau;
}

void setup()
{
clrscr();
matrix(1);
cout << " Withdrawals Deposits Balance\n";
cout << " ----------- -------- -------\n";
cout.setf(ios::showpoint ¦ ios::fixed);
cout << setprecision(2);
}

void print_results(double num_w, double num_d, double total,
double total_w, double total_d, double balance,
double initial)
{
clrscr();
matrix(9);
cout << " \t\t Initial Balance:" << setw(18) << initial << '\n';
cout << " \t\t Number of Withdrawals:" << setw(12) << int(num_w) << '\n';
cout << " \t\t Number of Deposits:" << setw(15) << int(num_d) << '\n';
cout << " \t\t Number of Transactions:" << setw(11) << int(total) << '\n';
cout << " \t\t Total of Withdrawals:" << setw(13) << total_w << '\n';
cout << " \t\t Total of Deposits:" << setw(16) << total_d << '\n';
cout << " \t\t Final Balance:" << setw(20) << balance << '\n';
matrix(8);
cout << "\t\t\t Press Enter To Continue: ";
getch();
cin.ignore();
}

Korn
airdevil23@hotmail.com

"procrastination is the key to happiness"

### RE: Whoa

(OP)
// Trinity.h

#ifndef trinity

// Function : neo
// Purpose: to display the name, date, and program title
// Input: a date and a program name
// Value(s) Returned: None

void neo(apstring date, apstring program);

// Function : morpheus
// Purpose: to display the purpose of the program
// Input: none
// Value(s) Returned: None

void morpheus();

// Function : tank_ten
// Purpose: to round a double to the tenth place
// Input: a double
// Value(s) Returned: a double

double tank_ten(double tank);

// Function : tank_hund
// Purpose: to round a double to the hundredths place
// Input: a double
// Value(s) Returned: a double

double tank_hund(double tank);

// Function : tank_thou
// Purpose: to round a double to the thousandths place
// Input: a double
// Value(s) Returned: a double

double tank_thou(double tank);

// Function : matrix
// Purpose: skip a number of lines
// Input: an integer
// Value(s) Returned: none

void matrix(int mat);

// Function : space
// Purpose: to produce a number fo spaces
// Input: an integer
// Value(s) Returned: none

void space(int spam);

#define trinity

#include "trinity.cpp"

#endif

// trinity.cpp

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#include <string.h>
#include <math.h>

void neo(apstring date, apstring program)
{

// First Screen

clrscr();
cout << "\n\n\n\n\n\n\n\n\n\n";
cout << "\t\t\t\t Bill Korn\n\n";
cout << "\t\t\t\t " << date << "\n\n";
cout << "\t\t\t\t " << program << ".CPP\n";
cout << "\n\n\n\n\n\n\n\n\n\n";
cout << "\t\t\t Press Enter To Continue:";
getch();
cin.ignore();
}

void morpheus()
{

// Second Screen

clrscr();
cout << "\n\n\n\n\n\n\n\n\n\n";
cout << " This program will ask the user for an initial inventory.\n";
cout << " Then it will get orders from the user and and tell wether\n";
cout << " they are filled or not. Then it will print the final inven\n";
cout << " -tory, widgets shipped and new widgets needed.\n";
cout << "\n\n\n\n\n\n\n\n\n\n";
cout << "\t\t\t Press Enter To Continue:";
getch();
cin.ignore();
}

double tank_ten(double tank)
{
tank = floor(tank * 10 + .5001) / 10;
return tank;
}

double tank_hund(double tank)
{
tank = floor(tank * 100 + .5001) / 100;
return tank;
}

double tank_thou(double tank)
{
tank = floor(tank * 1000 + .5001) / 1000;
return tank;
}

void matrix(int mat)
{
int i;
for(i = 1; i <= mat; i++)
cout << '\n';
}

{
int line;
int i;
if(spam < 10000)
line = 2;
if(spam < 1000)
line = 3;
if(spam < 100)
line = 4;
if(spam < 10)
line = 5;
for(i = 1;i <= line; i++)
cout << " ";
}

// apstring---------------------

#ifndef _APSTRING_H
#define _APSTRING_H

#include <iostream.h>
// uncomment line below if bool not built-in type
#if __BORLANDC__ < 0x500
#include "bool.h"
#endif

// *******************************************************************
// Last Revised: January 13,1998
//
// APCS string class
//
// string class consistent with a subset of the standard C++ string class
// as defined in the draft ANSI standard
// *******************************************************************

extern const int npos; // used to indicate not a position in the string

class apstring
{
public:

// constructors/destructor

apstring( ); // construct empty string ""
apstring( const char * s ); // construct from string literal
apstring( const apstring & str ); // copy constructor
~apstring( ); // destructor

// assignment

const apstring & operator = ( const apstring & str ); // assign str
const apstring & operator = ( const char * s ); // assign s
const apstring & operator = ( char ch ); // assign ch

// accessors

int length( ) const; // number of chars
int find( const apstring & str ) const; // index of first occurrence of str
int find( char ch ) const; // index of first occurrence of ch
apstring substr( int pos, int len ) const; // substring of len chars
// starting at pos
const char * c_str( ) const; // explicit conversion to char *

// indexing

char operator[ ]( int k ) const; // range-checked indexing
char & operator[ ]( int k ); // range-checked indexing

// modifiers

const apstring & operator += ( const apstring & str );// append str
const apstring & operator += ( char ch ); // append char

private:
int myLength; // length of string (# of characters)
int myCapacity; // capacity of string
char * myCstring; // storage for characters
};

// The following free (non-member) functions operate on strings
//
// I/O functions

ostream & operator << ( ostream & os, const apstring & str );
istream & operator >> ( istream & is, apstring & str );
istream & getline( istream & is, apstring & str );

// comparison operators:

bool operator == ( const apstring & lhs, const apstring & rhs );
bool operator != ( const apstring & lhs, const apstring & rhs );
bool operator < ( const apstring & lhs, const apstring & rhs );
bool operator <= ( const apstring & lhs, const apstring & rhs );
bool operator > ( const apstring & lhs, const apstring & rhs );
bool operator >= ( const apstring & lhs, const apstring & rhs );

// concatenation operator +

apstring operator + ( const apstring & lhs, const apstring & rhs );
apstring operator + ( char ch, const apstring & str );
apstring operator + ( const apstring & str, char ch );

// *******************************************************************
// Specifications for string functions
//
// Any violation of a function's precondition will result in an error
// message followed by a call to abort.
//
// The apstring class assumes that '\0' is not a valid
// character in an apstring. Any attempts to place '\0'
// in an apstring will result in undefined behavior. Generally
// this means that characters that follow the '\0' will not
// be considered part of the apstring for purposes of
// comparison, output, and subsequent copying.
//
// constructors / destructor
//
// string( )
// postcondition: string is empty
//
// string( const char * s )
// description: constructs a string object from a literal string
// such as "abcd"
// precondition: s is '\0'-terminated string as used in C
// postcondition: copy of s has been constructed
//
// string( const string & str )
// description: copy constructor
// postcondition: copy of str has been constructed
//
// ~string( );
// description: destructor
// postcondition: string is destroyed
//
// assignment
//
// string & operator = ( const string & rhs )
// postcondition: normal assignment via copying has been performed
//
// string & operator = ( const char * s )
// description: assignment from literal string such as "abcd"
// precondition: s is '\0'-terminated string as used in C
// postcondition: assignment via copying of s has been performed
//
// string & operator = ( char ch )
// description: assignment from character as though single char string
// postcondition: assignment of one-character string has been performed
//
// accessors
//
// int length( ) const;
// postcondition: returns # of chars in string
//
// int find( const string & str) const;
// description: find the first occurrence of the string str within this
// string and return the index of the first character. If
// str does not occur in this string, then return npos.
// precondition: this string represents c0, c1, ..., c(n-1)
// str represents s0, s1, ...,s(m-1)
// postcondition: if s0 == ck0, s1 == ck1, ..., s(m-1) == ck(m-1) and
// there is no j < k0 such that s0 = cj, ...., sm == c(j+m-1),
// then returns k0;
// otherwise returns npos
//
// int find( char ch ) const;
// description: finds the first occurrence of the character ch within this
// string and returns the index. If ch does not occur in this
// string, then returns npos.
// precondition: this string represents c0, c1, ..., c(n-1)
// postcondition: if ch == ck, and there is no j < k such that ch == cj
// then returns k;
// otherwise returns npos
//
// string substr( int pos, int len ) const;
// description: extract and return the substring of length len starting
// at index pos
// precondition: this string represents c0, c1, ..., c(n-1)
// 0 <= pos <= pos + len - 1 < n.
// postcondition: returns the string that represents
// c(pos), c(pos+1), ..., c(pos+len-1)
//
// const char * c_str( ) const;
// description: convert string into a '\0'-terminated string as
// used in C for use with functions
// that have '\0'-terminated string parameters.
// postcondition: returns the equivalent '\0'-terminated string
//
// indexing
//
// char operator [ ]( int k ) const;
// precondition: 0 <= k < length()
// postcondition: returns copy of the kth character
//
// char & operator [ ]( int k )
// precondition: 0 <= k < length()
// postcondition: returns reference to the kth character
// note: if this reference is used to write a '\0'
// subsequent results are undefined
//
// modifiers
//
// const string & operator += ( const string & str )
// postcondition: concatenates a copy of str onto this string
//
// const string & operator += ( char ch )
// postcondition: concatenates a copy of ch onto this string
//
//
// non-member functions
//
// ostream & operator << ( ostream & os, const string & str)
// postcondition: str is written to output stream os
//
// istream & operator >> ( istream & is, string & str )
// precondition: input stream is open for reading
// postcondition: the next string from input stream is has been read
// and stored in str
//
// istream & getline( istream & is, string & str )
// description: reads a line from input stream is into the string str
// precondition: input stream is open for reading
// postcondition: chars from input stream is up to '\n' have been read
// and stored in str; the '\n' has been read but not stored
//
// string operator + ( const string & lhs, const string & rhs )
// postcondition: returns concatenation of lhs with rhs
//
// string operator + ( char ch, const string & str )
// postcondition: returns concatenation of ch with str
//
// string operator + ( const string & str, char ch )
// postcondition: returns concatenation of str with ch
//
//***************************************************************

#include "apstring.cpp"

#endif

// *******************************************************************
// Last Revised: January 13, 1998, <= and >= redefined using ! and <
// operator += now takes constant
// amortized time for adding one char
//
// APCS string class IMPLEMENTATION
//
// see apstring.h for complete documentation of functions
//
// string class consistent with a subset of the standard C++ string class
// as defined in the draft ANSI standard
// *******************************************************************

#include <string.h>
#include <assert.h>
// #include "apstring.h"

const int npos = -1;
const int MAX_LENGTH = 1024; // largest size string for input

apstring::apstring()
// postcondition: string is empty
{
myLength = 0;
myCapacity = 1;
myCstring = new char[myCapacity];
myCstring = '\0'; // make c-style string zero length
}

apstring::apstring(const char * s)
//description: constructs a string object from a literal string
// such as "abcd"
//precondition: s is '\0'-terminated string as used in C
//postcondition: copy of s has been constructed
{
assert (s != 0); // C-string not NULL?

myLength = strlen(s);
myCapacity = myLength + 1; // make room for '\0'
myCstring = new char[myCapacity];
strcpy(myCstring,s);
}

apstring::apstring(const apstring & str)
//description: copy constructor
//postcondition: copy of str has been constructed
{
myLength = str.length();
myCapacity = myLength + 1;
myCstring = new char[myCapacity];
strcpy(myCstring,str.myCstring);
}

apstring::~apstring()
//description: destructor
//postcondition: string is destroyed
{
delete[] myCstring; // free memory
}

const apstring& apstring::operator =(const apstring & rhs)
//postcondition: normal assignment via copying has been performed
{
if (this != &rhs) // check aliasing
{
if (myCapacity < rhs.length() + 1) // more memory needed?
{
delete[] myCstring; // delete old string
myCapacity = rhs.length() + 1; // add 1 for '\0'
myCstring = new char[myCapacity];
}
myLength = rhs.length();
strcpy(myCstring,rhs.myCstring);
}
return *this;
}

const apstring& apstring::operator = (const char * s)
//description: assignment from literal string such as "abcd"
//precondition: s is '\0'-terminated string as used in C
//postcondition: assignment via copying of s has been performed
{

int len = 0; // length of newly constructed string
assert(s != 0); // make sure s non-NULL
len = strlen(s); // # of characters in string

// free old string if necessary

if (myCapacity < len + 1)
{
delete[] myCstring; // delete old string
myCapacity = len + 1; // add 1 for '\0'
myCstring = new char[myCapacity];
}
myLength = len;
strcpy(myCstring,s);
return *this;
}

const apstring& apstring::operator = (char ch)
//description: assignment from character as though single char string
//postcondition: assignment of one-character string has been performed
{
if (myCapacity < 2)
{
delete [] myCstring;
myCapacity = 2;
myCstring = new char[myCapacity];
}
myLength = 1;
myCstring = ch; // make string one character long
myCstring = '\0';
return *this;
}

int apstring::length( ) const
//postcondition: returns # of chars in string
{
return myLength;
}

const char * apstring::c_str() const
//description: convert string into a '\0'-terminated string as
// used in C for use with functions
// that have '\0'-terminated string parameters.
//postcondition: returns the equivalent '\0'-terminated string
{
return myCstring;
}

char& apstring::operator[](int k)
// precondition: 0 <= k < length()
// postcondition: returns copy of the kth character
// note: if this reference is used to write a '\0'
// subsequent results are undefined
{
if (k < 0 ¦¦ myLength <= k)
{
cerr << "index out of range: " << k << " string: " << myCstring
<< endl;
assert(0 <= k && k < myLength);
}
return myCstring[k];
}

char apstring::operator[](int k) const
// precondition: 0 <= k < length()
// postcondition: returns copy of the kth character
{
if (k < 0 ¦¦ myLength <= k)
{
cerr << "index out of range: " << k << " string: " << myCstring
<< endl;
assert(0 <= k && k < myLength);
}
return myCstring[k];
}

ostream& operator <<(ostream & os, const apstring & str)
//postcondition: str is written to output stream os
{
return os << str.c_str();
}

istream& operator >>(istream & is, apstring & str)
//precondition: input stream is open for reading
//postcondition: the next string from input stream is has been read
// and stored in str
{
char buf[MAX_LENGTH];
is >> buf;
str = buf;
return is;
}

istream & getline(istream & is, apstring & str)
//description: reads a line from input stream is into the string str
//precondition: input stream is open for reading
//postcondition: chars from input stream is up to '\n' have been read
{
char buf[MAX_LENGTH];
is.getline(buf,MAX_LENGTH);
str = buf;
return is;
}

const apstring& apstring::operator +=(const apstring & str)
//postcondition: concatenates a copy of str onto this string
{

apstring copystring(str); // copy to avoid aliasing problems

int newLength = length() + str.length(); // self + added string
int lastLocation = length(); // index of '\0'

// check to see if local buffer not big enough
if (newLength >= myCapacity)
{
myCapacity = newLength + 1;
if (str.length() == 1) // special case for catenating one char
{ // make room for future catenations
myCapacity *= 2;
}
char * newBuffer = new char[myCapacity];
strcpy(newBuffer,myCstring); // copy into new buffer
delete [] myCstring; // delete old string
myCstring = newBuffer;
}

// now catenate str (copystring) to end of myCstring
strcpy(myCstring+lastLocation,copystring.c_str() );
myLength = newLength; // update information

return *this;
}

const apstring & apstring::operator += ( char ch )
// postcondition: concatenates a copy of ch onto this string
{
apstring temp; // make string equivalent of ch
temp = ch;
*this += temp;
return *this;
}

apstring operator +(const apstring & lhs, const apstring & rhs)
// postcondition: returns concatenation of lhs with rhs
{
apstring result(lhs); // copies lhs to result
result += rhs; // catenate rhs
return result; // returns a copy of result
}

apstring operator + ( char ch, const apstring & str )
// postcondition: returns concatenation of ch with str
{
apstring result; // make string equivalent of ch
result = ch;
result += str;
return result;
}

apstring operator + ( const apstring & str, char ch )
// postcondition: returns concatenation of str with ch
{
apstring result(str);
result += ch;
return result;
}

apstring apstring::substr(int pos, int len) const
//description: extract and return the substring of length len starting
// at index pos
//precondition: this string represents c0, c1, ..., c(n-1)
// 0 <= pos <= pos + len - 1 < n.
//postcondition: returns the string that represents
// c(pos), c(pos+1), ..., c(pos+len-1)
//
{
if (pos < 0) // start at front when pos < 0
{
pos = 0;
}

if (pos >= myLength) return ""; // empty string

int lastIndex = pos + len - 1; // last char's index (to copy)
if (lastIndex >= myLength) // off end of string?
{
lastIndex = myLength-1;
}

apstring result(*this); // make sure enough space allocated

int j,k;
for(j=0,k=pos; k <= lastIndex; j++,k++)
{
result.myCstring[j] = myCstring[k];
}
result.myCstring[j] = '\0'; // properly terminate C-string
result.myLength = j; // record length properly
return result;
}

int apstring::find(const apstring & str) const
//description: find the first occurrence of the string str within this
// string and return the index of the first character. If
// str does not occur in this string, then return npos.
//precondition: this string represents c0, c1, ..., c(n-1)
// str represents s0, s1, ...,s(m-1)
//postcondition: if s0 == ck0, s1 == ck1, ..., s(m-1) == ck(m-1) and
// there is no j < k0 such that s0 = cj, ...., sm == c(j+m-1),
// then returns k0;
// otherwise returns npos
{
int len = str.length();
int lastIndex = length() - len;
int k;
for(k=0; k <= lastIndex; k++)
{
if (strncmp(myCstring + k,str.c_str(),len) == 0) return k;
}
return npos;
}

int apstring::find( char ch ) const
// description: finds the first occurrence of the character ch within this
// string and returns the index. If ch does not occur in this
// string, then returns npos.
// precondition: this string represents c0, c1, ..., c(n-1)
// postcondition: if ch == ck, and there is no j < k such that ch == cj
// then returns k;
// otherwise returns npos
{
int k;
for(k=0; k < myLength; k++)
{
if (myCstring[k] == ch)
{
return k;
}
}
return npos;
}

bool operator == ( const apstring & lhs, const apstring & rhs )
{
return strcmp(lhs.c_str(), rhs.c_str()) == 0;
}

bool operator != ( const apstring & lhs, const apstring & rhs )
{
return ! (lhs == rhs);
}

bool operator < ( const apstring & lhs, const apstring & rhs )
{
return strcmp(lhs.c_str(), rhs.c_str()) < 0;
}

bool operator <= ( const apstring & lhs, const apstring & rhs )
{
return !( rhs < lhs );
}
bool operator > ( const apstring & lhs, const apstring & rhs )
{
return rhs < lhs;
}

bool operator >= ( const apstring & lhs, const apstring & rhs )
{
return ! ( lhs < rhs );
}

Korn
airdevil23@hotmail.com

"procrastination is the key to happiness"

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!