C++ code won't compile.

I 'm doing a project to simulate an ATM machine like withdraw,deposit, see current balance, and make it keep asking the user what do you want to do unless they choose not to do anything anymore and just halt the program using Q or q to quit. But I keep getting errors when I compile I really need help D: Can someone please tell me what I'm doing wrong?

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

#include <iostream>
#include <stdlib.h>


using namespace std;

void menu (void);

int main()
{


float number,result;
float numero = 5000.00;
void menu (void);
void check_balance(void);
char choice;

menu();
cin >> choice;

while(choice !='d' && choice != 'D' && choice != 'w' && choice != 'W' && choice != 'b' && choice != 'B' && choice != 'Q' && choice != 'q')
{
cout << "It's not a valid entry." << endl;
menu();
cin >> choice;


}

if (choice == 'd' || choice == 'D')
{
cout << "How much will you deposit?" << endl;
cin >> number;
}

result = (number + numero);
}
else if (choice == 'w' || choice == 'W')
{
cout << "How much will you withdraw?" << endl;
cin >> number;
result = (numero - number);
}

else if (choice == 'b' || choice == 'B')
{          
           
void print_balance (void);

cout << "Your balance is " << balance  << " dollars." << endl;
}

{
else (choice == 'q' || choice == 'Q')

return 0;
}

{
cout << "Thank you for your transaction! Your current balance is now " << result  << " dollars." << endl;
cout << "What would you like to do now?" << endl;

return 0;
}
void menu(void);

{       cout << "Hello there!" <<endl;
cout << "Enter D or d for deposit." << endl;
cout << "Enter W or w for withdrawal." << endl;
cout << "Enter B or b for balance." << endl;
cout << "Enter Q or q to quit." << endl;

} 


void print_balance(float);
{

if (balance == balance)
{
            
 return 0;
 
}

else 
{
     
return 1;

}
     
}



These are the earrors I get from compiling. I don't understand what they want me to do.

1
2
3
4
5
6
7
8
9
10
11
12
13
line 39: error: expected unqualified-id before "else"
line 39: error: expected `,' or `;' before "else"
line 46: error: expected unqualified-id before "else"
line 46: error: expected `,' or `;' before "else"
line 54: error: expected unqualified-id before '{' token
line 54: error: expected `,' or `;' before '{' token
line 60: error: expected unqualified-id before '{' token
line 68: error: expected unqualified-id before '{' token
line 68: error: expected `,' or `;' before '{' token
line 78: error: expected unqualified-id before '{' token
line 78: error: expected `,' or `;' before '{' token

closed account (3pj6b7Xj)
1
2
3
4
}

result = (number + numero);
}


Incorrect syntax...

1
2
3
4
5
6
7
else if (choice == 'b' || choice == 'B')
{          
           
void print_balance (void);

cout << "Your balance is " << balance  << " dollars." << endl;
}


You can't define a function inside an if statement!!

1
2
3
4
5
6
{
cout << "Thank you for your transaction! Your current balance is now " << result  << " dollars." << endl;
cout << "What would you like to do now?" << endl;

return 0;
}


What are you trying to do with this??

I tried to fix your program but realized I was going to end up reprogramming the entire thing and just gave up on it.

I did notice you have several syntax errors, you are trying to define a function inside an if statement and you have several and I mean several out of scope variable issues...you seem to be having a problem with the use of if elseif, etc and the use of scopy and how it affects variables.

The compiler pretty much tells you what to fix, the first line...


line 39: error: expected unqualified-id before "else"


Tells you there is a problem on line #39...when I look at line 39 I see this...

 
}


I back track upwards to find its partner "{" and realize I do not find it...

1
2
3
4
5
6
7
8
9
10
}

if (choice == 'd' || choice == 'D')
{
cout << "How much will you deposit?" << endl;
cin >> number;
}

result = (number + numero);
}


Instead i just find another "}" that is completely wrong syntax and it will cause number + numero some of the two to become out of scope.

You should indent your code for better clarity, of course, the forum post may have chewed up the tabs or spaces.
Last edited on
void menu(void); -> void menu(void)
i think there will be an error with your while statement

while(choice !='d' && choice != 'D' && choice != 'w' && choice != 'W' && choice != 'b' && choice != 'B' && choice != 'Q' && choice != 'q')

I'm getting the idea that it will get messed up is because it has to be everything in order for it to get out of the loop. so the user would have to put in a d, w, b, and a q in order to get it, because of the &&. However, when i've tried to fix something like this with OR statements it throws it into an infinite loop which isn't fun, lol. so if it works now just leave it, but i dk why it should work this way.

maybe something like (choice != 'd' || choice != 'D').... that would check to see if it's capital or lowercase at least. because right now how it's coding it's checking if it's 'd' and it's 'D'. I hope this makes sense, and doesn't stear you in a wrong direction.

EDIT:
You could also use a switch for the choice instead of several else if and if else statements, just saying.
Last edited on
Topic archived. No new replies allowed.