Need help with program!


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.

Please help as i am new to programming. Thanks.
Last edited on
cout << "Your current balance is"<< double balance <<endl;
should be
cout << "Your current balance is " << balance << '\n';

( use '\n' instead of endl, it's faster )
Here is the fixed code with the code without bugs. I did some changes in the if statemrnts so I could hurry more and help you.

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <stdlib.h>
using namespace 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.
Last edited on
Thanks! it helped! How would i make the program continue repeating the program so that the only way it ends is if the user enters Q or q?
Last edited on
You can use a while loop to repeat until the user indicates they want to quit. e.g.

1
2
3
4
5
while (user has not entered quit)
{
//do stuff
//ask user if they want to repeat
}


@SirChristian123 - goto statements are not best practice and should not be recommended.
What do you suggest to use instead of goto statements? I did not want to use them either. @AbstractionAnon
closed account (j3Rz8vqX)
A possible suggestion:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <stdlib.h>
using namespace 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.
Last edited on
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.

I can use a while loop but nothing more than that. Is there a way to construct this program using a while loop? @AbstractionAnon
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.
closed account (j3Rz8vqX)
Simply Informing:

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.

PreTest and PosTest.
Maybe this will help to show what i am trying to do and what statements and conditions i want to use.

I am only including the main
How would i make the program work using this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
int main()
{
    char choice;
    cout << "Welcome!" << endl;
  	
        print_balance( balance);  
        print_menu();  
        cin >> choice;
        
		while (choice == 'Q' || 'q')
   {
       
        if (choice == 'd' || 'D')
        {
            
                cout << "Enter the amount to deposit ";
                cin >> amount;
                if ( amount <= 0)   
                {
                    cout << "Enter a proper amount" << endl;
                }
                else
                {
                    balance = amount + balance;
                    void print_menu();
                }
            cout << "Thank you, your transaction is completed" << endl;
        }
        // Condition for withdraw
        if else (choice == 'w' || 'W')
        {
            cout << "Choose the amount to withdraw ";   
            cin >> withdraw;
            if (withdraw <= 0 || withdraw >= balance)   
            {
                cout << "Enter a proper amount" << endl;
            }
            balance = balance-withdraw;    

            cout << "Thank you, your transaction is completed" << endl;
        }
		
		if else (choice == 'B' || 'b')
		{
			print_balance( balance)
		}
		
		if else (choice == 'Q' || 'q')
		{
			cout << "Thank you, your transaction is completed" << endl;
		}
		else 
		{
			print_balance( balance);  
       		print_menu(); 
        	cin >> choice;
		}
}
    system("PAUSE");
    return 0;
}
I noticed that my while condition is wrong
Your if..else..if statements are wrong too. You've written if else, but it should be else if. And line 38 needs an else statement before it.
Last edited on
i understand. How would I make the program continue to ask the user for an input unless the user enters Q or q, at which point it would terminate?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#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";
			}
		}

		else if ( 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";
			}
		}

		else if ( choice == 'Q' )
		{
			cout << "\nThank you for banking with Yay295. We hope to see you again soon.\n\n";
			return 0;
		}
	}
}
Last edited on
Topic archived. No new replies allowed.