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