# Debug error, help needed

• 08-09-2006
wsy
Debug error, help needed
Seems to have this Debug error when i try to run it, not sure how to solve.

Code:

```#include <iostream> using namespace std; double everything; float a; float b; inline void balance(void)     {     cout<<"You have a total of: Lm"<<everything; } inline void intro(void)     {     cout<<"~~~~BANK ACCOUNT ORGANIZER~~~~\n";     cout<<"\nBank Account Organizer will organize your bank account. You can withdraw money from your bank account or you may also choose to deposit money into your bank account. \nYou can check your account balance if you need to do so!\n"; } inline void balancenote(void)     {     int money;     int five2;     int one;     int five;     int ten;     double cent;     money=everything;     ten = money/10;     five = money%10;     five2 = five/5;     one = five%5;     cout << "\nYou have:\n";     cout << "Lm10: " << ten;     cout << "\nLm5: " << five2;     cout << "\nLm1: " << one;     cout << "\nLm0.50: " << (everything - money)*2; } int main()     {     intro();     cout<<"\nPlease enter your full name: ";     char name[20];     cin.getline(name,20,'\n');     cout<<"\n"<<name<<" Bank Account:";     cout<<"\n";     int n;     while (n!=0)         {         cout<<"\n\nPlease choose: \n1)Withdraw money from your bank account\n2)Deposit money into your bank account\n3)Check your account balance\n4)Empty your account\nPress 0 to quit!";         cin>>n;         switch (n)             {             case 1:             cout<<"\nPlease choose the amount you would like to withdraw:";             cout<<"\n1)Lm5\n2)Lm10\n3)Lm20\n4)Lm50\n5)Lm100\n6)Other...\n";             cin>>b;             if (b==1)                 {                 if (5>everything)                     {                     cout<<"\nThe transaction cannot be done. There isn't enought money in your account";                 }                 else                     {                     everything = everything - 5;                     cout<<"\nThe transaction is complete! - You have successully withdrawn Lm5!";                 }             }             else if (b==2)                 {                 if (10>everything)                     {                     cout<<"\nThe transaction cannot be done. There isn't enought money in your account";                 }                 else                     {                     everything = everything - 10;                     cout<<"\nThe transaction is complete! - You have successully withdrawn Lm10!";                 }             }             else if (b==3)                 {                 if (20>everything)                     {                     cout<<"\nThe transaction cannot be done. There isn't enought money in your account";                 }                 else                     {                     everything = everything - 20;                     cout<<"\nThe transaction is complete! - You have successully withdrawn Lm20!";                 }             }             else if (b==4)                 {                 if (50>everything)                     {                     cout<<"\nThe transaction cannot be done. There isn't enought money in your account";                 }                 else                     {                     everything = everything - 50;                     cout<<"\nThe transaction is complete! - You have successully withdrawn Lm50!";                 }             }             else if (b==5)                 {                 if (100>everything)                     {                     cout<<"\nThe transaction cannot be done. There isn't enought money in your account";                 }                 else                     {                     everything = everything - 100;                     cout<<"\nThe transaction is complete! - You have successully withdrawn Lm100!";                 }             }             else if (b==6)                 {                 cout<<"\nPlease enter the amount:";                 float g;                 cin>>g;                 if (g>everything)                     {                     cout<<"\nThe transaction cannot be done. There isn't enought money in your account";                 }                 else                     {                     everything = everything - g;                     cout<<"\nThe transaction is complete! - You have successully withdrawn Lm"<<g<<"!";                 }             }             break;             case 2:             cout<<"\nPlease enter the amount you would like to put into your bank account.";             cin>>a;             everything = everything + a;             cout<<"\nThe transaction is complete! - You have successully put Lm"<<a<<" into your bank account!";             cout<<"You now have Lm"<<everything<<". Check your account balance for more info.";             break;             case 3:             cout<<"\nHere's your balance:";             balance();             balancenote();             break;             case 4:             everything = 0;             cout<<"\nYour account has been emptied";             break;             case 0:             break;             default:             cout<<"\nYou have pressed the wrong number!";         }     }     char q;     do         {         cout<<"\nThanks for using BANK ACCOUNT ORGANIZER! Press Q to quit!";         cin>>q;         if (q=='q' || q=='Q')         return 0;     }while (q!='q' && q!='Q'); }```
• 08-10-2006
siavoshkc
I don't know what do you mean by Debug Error but your program has a little problem that is if you enter a character instead of a number it fails. It is because you should place this code after each input:
Code:

```if(!cin.good()){       cin.clear();       cin.ignore(300,'\n');       //.....Ask user to enter a number.... }```
• 08-10-2006
Viorel
It seems you are using an un-initialized n here:

Code:

```int n; while (n!=0) {     . . . }```
Probably it should be

Code:

```int n; do {     . . . } while(n != 0);```
I hope this helps.
• 08-11-2006
siavoshkc
Quote:

It seems you are using an un-initialized n here:...
I was lucky that program worked without problem for me.
• 08-11-2006
Mario F.
You cannot do this if (5>everything).

5 is a literal. It is also a rvalue (right-value). It should go on the other side of the logical expression. if(everything <= 5)
• 08-11-2006
siavoshkc
Quote:

5 is a literal. It is also a rvalue (right-value). It should go on the other side of the logical expression. if(everything <= 5)
Why?
(everything <= 5) ==(5>=everything) no?
• 08-11-2006
Mario F.
Actually it's a total silliness what I said. I'm obviously wrong. Besides the rvalue or lvalue meanings are only to be seen in the context of assignment.
• 08-11-2006
jafet
EDIT: already mentioned by siavoshkc.

But seriously, using double to represent 1/100 values are a VERY BAD IDEA. Why. For small amounts, you could use an int or long divided by 100, for larger transactions you could use a 64-bit integer (even Gates isn't that rich :)).
• 08-11-2006
Bench82
Quote:

Originally Posted by Mario F.
You cannot do this if (5>everything).

5 is a literal. It is also a rvalue (right-value). It should go on the other side of the logical expression. if(everything <= 5)

I disagree.. I always put constant or literal values on the left-hand side of logical expressions whenever possible. for one simple reason, that i'm merely human, and sometimes, i make this stupid mistake..
Code:

`if ( somevar = 5 ) // graaaarghhhhh!`
then at least the compiler will whack me around the head if i type
Code:

`if ( 5 = somevar ) // Compiler error.. phew!!`
;)
• 08-11-2006
siavoshkc
Quote:

Originally Posted by Mario F
Actually it's a total silliness what I said. I'm obviously wrong. Besides the rvalue or lvalue meanings are only to be seen in the context of assignment.

Anyway this mistake should not happen.

I mean using = instead of ==.

[edit2]
Quote:

I always put constant or literal values on the left-hand side of logical expressions whenever possible.
Intresting point.
• 08-11-2006
Bench82
Quote:

Originally Posted by siavoshkc
Anyway this mistake should not happen.

I mean using = instead of ==.

[edit2]

Intresting point.

Heh, it's one of those "d'uh! I can't believe I made that mistake" bugs :)