Can you check my program and make a correction ? #include <iostream> using names
ID: 3641452 • Letter: C
Question
Can you check my program and make a correction ?#include <iostream>
using namespace std;
void main()
{
int ret, negcount, input;
while (true)
{
ret = 1;
input = 1;
for (negcount=0; negcount < 3; negcount++)
{
cout << "Enter an integer :";
cin >> input;
while (input >= 0)
{
while (input > 0)
{
ret *= input;
input=0;
}
negcount = 0;
cout << "Enter an integer :";
cin >> input;;
}
}
cout << endl << "Product"<< ret << endl << endl;
}
}
Enter an integer :-6
Enter an integer :-2
Enter an integer :0
Enter an integer :1
Enter an integer :2
Enter an integer :3
Enter an integer :-6
Enter an integer :-5
Enter an integer :-1
Product = 6
Enter an integer :5
Enter an integer :4
Enter an integer :3
Enter an integer :-1
Enter an integer :-2
Enter an integer :-5
Product = 60
Enter an integer :5
Enter an integer :0
Enter an integer :5
Enter an integer :-5
Enter an integer :0
Enter an integer :-2
Enter an integer :-5
Enter an integer :-8
Product = 25
Enter an integer :6
Enter an integer :5
Enter an integer :4
Enter an integer :0
Enter an integer :0
Enter an integer :-5
Enter an integer :-4
Enter an integer :0
Enter an integer :-4
Enter an integer :-5
Enter an integer :-6
Product = 120
Enter an integer
// C Patel
// Page 325 Prob. 6
#include<iostream>
using namespace std;
void main ()
{
char W, D, Z, I, Q, L;
double number, balance, amount;
cout << "SAVING ACCOUNT TRANSCATION PROGRAM." << endl;
cout << "When prompted, enter an 'I' for the initial account" << endl;
cout << "information or 'Q' to terminate program execution." << endl;
cout << "Then enter the account number follwed by the initial"<< endl;
cout << "account balance. All subsequent transactions are "<< endl;
cout << "denotedas withdrawal:"<< endl;
cout << "(W) for withdrawal" << endl;
cout << "(D) for deposit" << endl;
cout << "(Z) to end account transcations for this account" << endl << endl;
cout << "Enter (I)nitial account or (Q)uit : ";
cin >> L;
while (L != 'Q' || L != 'q')
{
cout << "Account number : ";
cin >> number;
cout << endl << "Enter Initial Balance : ";
cin >> balance;
cout << "SAVINGS ACCOUNT TRANSACTION" << endl;
cout << "(W)ithdrawal" << endl;
cout << "(D)eposit" << endl;
cout << "(Z) to end transaction" << endl;
cout << "Enter first letter of transaciton type (W, D, or Z):";
cin >> L;
while (L !='Z' && L != 'z')
{
if (L=='D' || L == 'd')
{
cout << endl << "Amount: $";
cin >> amount;
balance += amount;
L=I;
}
else if (L =='W' || L == 'w')
{
cout << endl << "Amount: $";
cin >> amount;
balance -= amount;
L=I;
}
cout << endl << "Balance for this account is now: " << balance << endl;
}
cout << endl << " No more Changes." << endl;
cout << endl << " Balance for this account is now: " << balance << endl;
}
}
SAVING ACCOUNT TRANSCATION PROGRAM.
When prompted, enter an 'I' for the initial account
information or 'Q' to terminate program execution.
Then enter the account number follwed by the initial
account balance. All subsequent transactions are
denotedas withdrawal:
(W) for withdrawal
(D) for deposit
(Z) to end account transcations for this account
Enter (I)nitial account or (Q)uit : i
Account number : 1234
Enter Initial Balance : 1054.07
SAVINGS ACCOUNT TRANSACTION
(W)ithdrawal
(D)eposit
(Z) to end transaction
Enter first letter of transaciton type (W, D, or Z):w
Amount: $25
Balance for this account is now: 1029.07
SAVINGS ACCOUNT TRANSACTION
(W)ithdrawal
(D)eposit
(Z) to end transaction
Enter first letter of transaciton type (W, D, or Z):d
Amount: $243.35
Balance for this account is now: 1272.42
SAVINGS ACCOUNT TRANSACTION
(W)ithdrawal
(D)eposit
(Z) to end transaction
Enter first letter of transaciton type (W, D, or Z):w
Amount: $254.55
Balance for this account is now: 1017.87
SAVINGS ACCOUNT TRANSACTION
(W)ithdrawal
(D)eposit
(Z) to end transaction
Enter first letter of transaciton type (W, D, or Z):z
No more Changes.
Balance for this account is now: 1017.87
Explanation / Answer
I can't see any problem in the first program...
for the second program:
#include <iostream>
#include <iomanip> //need for setprecision
#include <math.h> //need for fabs function (float absolute)
using namespace std;
#define EPSILON 0.01
void printHeader();
void printTransactionChoices();
bool areSame(double a, double b) { return fabs(a - b) < EPSILON; } //if amount and balance difference is less than 1 cent then we consider them as the same
void main ()
{
char response;
double number, balance, amount;
cout << fixed << setprecision(2); //print double values with format 2 decimal places
printHeader();
cout << "Enter (I)nitial account or (Q)uit : ";
cin >> response;
cout << endl;
if (toupper(response) == 'I') //Anything else (include q or Q) will be considered illegal input and terminates the process
{
cout << "Account number: ";
cin >> number;
cout << endl << "Enter Initial Balance : ";
cin >> balance;
do
{
cout << endl;
printTransactionChoices();
cout << "Enter first letter of transaciton type (W, D, or Z):";
cin >> response;
if (toupper(response) == 'D')
{
cout << endl << "Amount: $";
cin >> amount;
balance += amount;
}
else if (toupper(response) == 'W')
{
cout << endl << "Amount: $";
cin >> amount;
if (areSame(balance, amount))
balance = 0.0;
else if (amount > balance) //you should check if amount > balance (you can withdraw more than the current balace)
cout << "You cannot withdraw more than $" << balance << endl;
else
balance -= amount;
}
cout << endl << "Balance for this account is now $" << balance << endl;
}
while (toupper(response) == 'D' || toupper(response) == 'W'); //Anything else (include z or Z) will be considered illegal input and terminates the process
cout << endl << " No more Changes." << endl;
cout << endl << " Balance for this account is now $" << balance << endl;
}
}
void printHeader()
{
cout << "SAVING ACCOUNT TRANSCATION PROGRAM." << endl;
cout << "When prompted, enter an 'I' for the initial account" << endl;
cout << "information or 'Q' to terminate program execution." << endl;
cout << "Then enter the account number follwed by the initial"<< endl;
cout << "account balance. All subsequent transactions are "<< endl;
cout << "denotedas withdrawal:"<< endl;
cout << "(W) for withdrawal" << endl;
cout << "(D) for deposit" << endl;
cout << "(Z) to end account transcations for this account" << endl << endl;
}
void printTransactionChoices()
{
cout << "SAVINGS ACCOUNT TRANSACTION" << endl;
cout << "(W)ithdrawal" << endl;
cout << "(D)eposit" << endl;
cout << "(Z) to end transaction" << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.