Quite new at C++ and needs help

Ok so I've been trying to make this simple bank program but I cant figure out one bit the problem is that it'll always end up $20.

#include <iostream>
#include <string>

using namespace std;

int main()
{
char we;
float with;
float answer;
int w;

answer = 20 - with;
w = 1;

cout<<"You have $20 in your bank press W to withdraw or press D to deposit ";
cin>> we;
cin.get();
if ( we = w ) {
cout<<"How much would you like to withdraw? ";
cin>> with;
cin.ignore();
if ( with < 20 ) {
cout<<"You now have "<<answer;

}
}
cin.ignore();


}

Many Thanks in Advance :]
Last edited on
Look at the following line:
if ( we = w ) {
And remember that the operator= is assignment not the comparison operator==.

Also please use code tags when posting code.
With is uninitialized
You're assigning we to w, not comparing them. Also, to compare to a char you have to put it between single quotes
When you print the remaining money you don't subtract with from the initial 20$
wait uhh can you help me out I'm not quite following. Like I said quite new like really new actually XD.
Last edited on
This program will accomplish what you're looking for.

If you want to compare a letter instead of a number to a variable, you have to use single quotes (such as 'w')

Also you determined the variable "answer" before the user input was calculated. This made it so answer was always equal to 20, no matter what. If you put it after the input, it will update "answer" accordingly.

Another thing, the cin.get() and cin.ignore() is pointless. I don't understand why youre using these.

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    char we;
    float with;
    float answer;





    cout<<"You have $20 in your bank press W to withdraw or press D to deposit ";
    cin>> we;
    if (we =='w' || we == 'W')
    {
        cout<<"How much would you like to withdraw? ";
        cin>> with;
        answer = 20 - with;
        if ( with < 20 )
        {
            cout<<"You now have "<<answer;

        }
    }

}

Last edited on
Ok so

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
  char we;
  float with;
  float answer;
  int w;

  answer = 20 - with;
  w = 1;

  cout<<"You have $20 in your bank press W to withdraw or press D to deposit ";
  cin>> we;
  cin.get();
  if ( we = w ) {
    cout<<"How much would you like to withdraw? ";
    cin>> with;
    cin.ignore();
    if ( with < 20 ) {
      cout<<"You now have "<<answer;

    }
  }
  cin.ignore();


}


Line 11: you're defining a useless variable that you're not using (for the reason in line 19). This makes line 14 useless too
Line 13: you're subtracting an uninitialized variable from a number. Uninitialized means that you defined it (line 9) but didn't assign a value to it before using it. Using uninitialized variables results in undefined behavior (read: never do it)
IIRC I did it once, the result was that the number I added it to remained exactly the same, so this might be why you always see 20 as answer
Line 18: cin.get() is redundant, the >> operator already waits for the user to press enter before continuing the execution
Line 19: a common error for beginners, using = to compare. When you assign use =, when you compare use ==
You're also trying to compare we with the variable w (which you set equal to 1). To compare a char to a letter of the keyboard enclose that letters within single quotes like fovv showed you
Line 22 not sure what it's used for
Line 22b here you should write "answer = 20 - with;" to calculate the amout left after the withdrawal. Now with was initialized by cin

I think that's all
Sorry if I sound rude, I'm not a master with words
Last edited on
Oh Thanks Alot Guys :)
Topic archived. No new replies allowed.