'c++ program for atm menu driven program
#include <iostream>
#include <conio.h>
using namespace std;
void showmenu()
{
cout << "***menu***" << endl;
cout << " enter 1 to checkbalance" << endl;
cout << " enter 2 to deposit" << endl;
cout << " enter 3 to withdraw" << endl;
cout << ":::::::::::::::::::::::::::::::::::::";
}
int main()
{
int option;
double balance = 500;
double deposit, withdraw;
do {
showmenu();
cout << "option";
cin >> option;
switch (option)
{
case1: cout << "balance is" << balance << endl;
break;
case2: cout << " enter deposit amount";
cin >> deposit;
balance += deposit;
break;
case3: cout << "withdraw amount";
cin >> withdraw;
if (withdraw <= balance)
{
balance -= withdraw;
}
else
{
cout << "insufficient amount";
break;
}
}
} while (option != 4);
return 0;
getch();
}
when i run this code what im getting is an iteration of the options available here.it seems to be a simple code but im fed up with these simple errors and struggles to correct it.help me
Solution 1:[1]
I think you forgot to put spaces between the keyword case and the case number. I made some minor changes. The corrected code:
#include <conio.h>
#include <iostream>
using namespace std;
void showmenu() {
cout<<"menu"<<endl;
cout<<" enter 1 to checkbalance"<<endl;
cout<<" enter 2 to deposit"<<endl;
cout<<" enter 3 to withdraw"<<endl;
cout<<":::::::::::::::::::::::::::::::::::::"<<endl;
}
int main() {
int option;
double balance=500;
double deposit,withdraw;
do{
showmenu();
cout<<"option";
cin>>option;
switch(option) {
case 1:
cout<<"balance is"<<balance<<endl;
break;
case 2:
cout<<" enter deposit amount";
cin>>deposit;
balance+=deposit;
break;
case 3:
cout<<"withdraw amount";
cin>>withdraw;
if(withdraw<=balance) {
balance-=withdraw;
} else {
cout<<"insufficient amount";
}
break;
}
}while(option!=4);
getch();
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zohaib Hamdule |
