I am getting an error every time i check for syntax errors; it states that "expected primary expression before 'double'."
which is this line:
cout << "Your current balance is"<< double balance <<endl;
Also, any help completing the program would be much appreciated.
#include <iostream>
#include <stdlib.h>
usingnamespace std;
//global variables called
double balance = 5000.00;
double amount;
double withdraw;
// functions definitions
void print_menu()
{
cout << "Here are your options: Enter (D) or (d) to make a deposit, (W) or (w) to make a withdrawal, (B) or (b) to view the current balance and (Q) or (q) to quit" << endl;
}
void print_balance(double balance)
{
cout << "Your current balance is " << balance << endl;
}
int main()
{
char choice;
cout << "Welcome!" << endl;
startagain:
print_balance( balance); // call print_balance function to show the balance
print_menu(); // call the print_menu() to show the menu
cin >> choice;
// deposit if statement
if (choice == 'd')
{
start:
cout << "Enter the amount to deposit ";
cin >> amount;
if ( amount <= 0) // execute if amount is negative
{
cout << "Enter a proper amount" << endl;
goto start; // makes it comeback to start: statement
}
else
{
void print_menu();
}
balance = amount + balance;
cout << "Thank you, your transaction is completed" << endl;
goto startagain;
}
// withdraw if statement
if (choice == 'w')
{
cout << "Choose the amount to withdraw ";
cin >> withdraw;
if (withdraw < 0) // execute if withdraw is negative
{
cout << "Enter a proper withdraw amount" << endl;
goto startagain;
}
balance = withdraw + balance;
cout << "Thank you, your transaction is completed" << endl;
goto startagain;
}
system("PAUSE");
return 0;
}
Hope this helps you!
Have a good day!.
Tell me if it helped you.
#include <iostream>
#include <stdlib.h>
usingnamespace std;
//global variables called
double balance = 5000.00;
double amount;
double withdraw;
// functions definitions
void print_menu()
{
cout << "Here are your options: Enter (D) or (d) to make a deposit, (W) or (w) to make a withdrawal, (B) or (b) to view the current balance and (Q) or (q) to quit" << endl;
}
void print_balance(double balance)
{
cout << "Your current balance is " << balance << endl;
}
int main()
{
char choice;
cout << "Welcome!" << endl;
do
{
print_balance( balance); // call print_balance function to show the balance
print_menu(); // call the print_menu() to show the menu
cin >> choice;
// deposit if statement
if (choice == 'd')
{
do
{
//bool stay=true;//<- Optionally, you could break out with this variable
cout << "Enter the amount to deposit ";
cin >> amount;
if ( amount <= 0) // execute if amount is negative
{
cout << "Enter a proper amount" << endl;
}
else
{
void print_menu();
balance = amount + balance;
break;//<- Optionally, you could assign: stay = false;
}
}while(true);//<- Optionally, you could break out with a variable declared in this scope while(stay);
cout << "Thank you, your transaction is completed" << endl;
}
// withdraw if statement
if (choice == 'w')
{
cout << "Choose the amount to withdraw ";
cin >> withdraw;
if (withdraw < 0) // execute if withdraw is negative
{
cout << "Enter a proper withdraw amount" << endl;
}
balance = withdraw + balance;
cout << "Thank you, your transaction is completed" << endl;
}
}while(choice!='Q'&&choice!='q');
system("PAUSE");
return 0;
}
goto is not often recommended because it is capable of causing spaghetti madness.
Edit: added comments, in case you are against loops with "true" values.
I apologize, is there anyway to do this without using the do-while condition?
I can only use if, if else, and else statements in the program along with the Print_balance and Print_menu functions.
It's hard to write logic that repeats without using loops (or gotos).
If you've been told that you can't use while or do/while, but have learned goto, then I would go ahead and use the gotos. Just keep in mind that the use of gotos is discouraged because they lead to spaghetti code (code that jumps all over the place with no rhyme or reason).
A good compromise is to put each part of the code that uses a goto in a separate function and no more than one goto in a function. That way, there is only one place a goto can jump to and is very contained.
Is there a way to construct this program using a while loop?
Sure there is. Just change the do/while to a while.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
// deposit if statement
if (choice == 'd')
{ bool done = false;
while (! done) // Alternative to do/while(true)
{ cout << "Enter the amount to deposit ";
cin >> amount;
if ( amount <= 0) // execute if amount is negative
{ cout << "Enter a proper amount" << endl;
}
else
{ balance = amount + balance;
done = true; // Will cause us to exit the loop
}
} // end while
cout << "Thank you, your transaction is completed" << endl;
} // end if
Note: I removed your line 45. Unclear if you meant to print the menu here. Line 45 is a declaration, not a function call. If you meant a function call, remove the void from the line.
Be reminded that while loops will pre test, whereas do-while loops use pos test.
That means, for a while loop, you may want to "prompt" the user before the loop, if it is controlled by the user, and somewhere inside the loop you'd want to retest the conditions.
The do while loop, on the other hand, will immediately enter the loop; so you will only have to provide the prompt once inside. The catch is, that, it must enter the loop. at least, once.
#include <iostream>
using std::cout;
using std::cin;
int main ( )
{
double balance = 5000.00, amount, withdraw;
char choice;
cout << "Welcome!\n\n";
while ( true )
{
cout << "Your current balance is $" << balance << "\nEnter (D) or (d) to make a deposit.""\nEnter (W) or (w) to make a withdrawal.\nEnter or (Q) or (q) to quit.\n";
cin.clear ( );
cin >> choice;
choice = toupper ( choice );
if ( choice == 'D' )
{
cout << "Enter the amount to deposit: ";
cin >> amount;
if ( !cin || amount <= 0 )
cout << "That cannot be deposited.\n";
else
{
balance = amount + balance;
cout << "Thank you, your transaction has been completed.\n\n";
}
}
elseif ( choice == 'W' )
{
cout << "Choose the amount to withdraw: ";
cin >> withdraw;
if ( !cin || withdraw <= 0 || withdraw > balance )
cout << "That cannot be withdrawn.\n";
else
{
balance = balance - withdraw;
cout << "Thank you, your transaction has been completed.\n\n";
}
}
elseif ( choice == 'Q' )
{
cout << "\nThank you for banking with Yay295. We hope to see you again soon.\n\n";
return 0;
}
}
}