I have a problem in this code???

I am making a program that simulates an atm.
I also put the error message at the bottom.


#include "stdafx.h"
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int _tmain(int argc, _TCHAR* argv[]);

int balance;//indicates the balance
int withamount;//indicates the amount to withdrawl
int depamount;//indicates the amount to deposit
char again;
char which;
{
cout << "Please enter your beginning balance: ";
cin >> balance;
while ( again != n )
{

cout<< "/nWould you like to withdrawal, or deposit? (w for withdrawal, d for deposit)(or press b to check you blance if you wish): ";

if ( which = w )
cout << " Please state the amount you would like to withdrawal: ";
cin >> withamount;
balance - withamount;
cout << " Your balance after the withdrawal of: " << withamount << "is: " << balance;

if ( which = d )
cout << "Please state the amount you would like to deposit: ";
cin >> depamount;
balance + depamount;
cout << "Your balance after the deposit of: " << depamount << " is: " << balance;

if ( which = b )
cout << "Your current balance is: " << balance;

cout << "Would you like to make another transaction? (y for yes, n for no): "
cin >> again;
system ("pause")
}
return 0;
}



and this is the error message when I try to compile it:

1>------ Build started: Project: atm2, Configuration: Debug Win32 ------
1>Compiling...
1>atm2.cpp
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\atm2\atm2\atm2.cpp(18) : error C2447: '{' : missing function header (old-style formal list?)
1>Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\atm2\atm2\Debug\BuildLog.htm"
1>atm2 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I can't compile because the compiler cant finde the stdafx.h
But also I noticed tha the beggining of the main() is its end also... You should use the entry bracket "{" instead of ";". If you want to declare
1
2
3
4
5
int balance;//indicates the balance
int withamount;//indicates the amount to withdrawl
int depamount;//indicates the amount to deposit
char again;
char which;

as global variables you should do it before the main.

Also you use variables n,d,b,w without initializing them.
If you want to check if the "which" char is one of those then you should use:
if(which=='d')
The '=' operator assign a value to a variable. The '==' check for equality. Also because it is a char you have to use single quotes ' ' and put in there your character.

Also you have: balance - withamount; which doesn't mean anything... If I understood well you need to assign to balance the current amount of balance minus the withamount. Such a syntax would be balance = balance - withamount; or balance -= withamount;

Finally for the if statements
1
2
3
if ( which = w )
if ( which = d )
if ( which = b )

You probably want to do all the folowin actions so you have to use brackets "{}" or else it will only execute the one following command as the commands of the if statements.
Also the escape character to change line is \n and not /n...
So, if I undestood well your code you probaby ment to do something like:
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
#include <stdafx>
#include <iostream>

using namespace std;

int balance;//indicates the balance
int withamount;//indicates the amount to withdrawl
int depamount;//indicates the amount to deposit
char again;
char which;
int _tmain(int argc, _TCHAR* argv[])
{
   cout << "Please enter your beginning balance: ";
   cin >> balance;
   while ( again != 'n' )
   {
      cout<< "\nWould you like to withdrawal, or deposit? (w for withdrawal, d for deposit)(or press b to check you blance if you wish): ";
      cin >> which; // You forgot to w8 for an input...
      if ( which == 'w' ){
         cout << " Please state the amount you would like to withdrawal: ";
         cin >> withamount;
         balance -= withamount;
         cout << " Your balance after the withdrawal of: " << withamount << "is: " << balance << '\n';
      }

      if ( which == 'd' ){
         cout << "Please state the amount you would like to deposit: ";
         cin >> depamount;
         balance += depamount;
         cout << "Your balance after the deposit of: " << depamount << " is: " << balance << '\n';
      }

      if ( which == 'b' )
         cout << "Your current balance is: " << balance;//Now you only want to execute this command as part of the if

      cout << "Would you like to make another transaction? (y for yes, n for no): ";
      cin >> again;
      system ("pause");
   }
   return 0;
}

One technique, that works for me, is to write your program in parts. What i mean is to make sure something works before going on. I do it even for the declaration of the main() so i can be sure that i don't have anything wrong because of mistype.
Hope that helps.


[edit] When you paste code it is better to use the # button and write your code between the code /code so it can be easier readable...
Last edited on
Topic archived. No new replies allowed.