c++ looping statement on package pricing - c++

I'm trying to make a loop statement to determine the prices according to their respective packages but I can't seem to get a grasp of the do and while statements.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const double W_adult=47.90, W_child=41.50, W_senior=35.40, A_adult=25.90, A_child=20.50, A_senior=15.40, P_adult=15.90, P_child=12.50, P_senior=10.40;
double totaladult,totalchild,totalsenior, total;
int noadult,nochild,nosenior;
char option;
//char choice = 'y'; looping choice
cout<<"WELCOME TO WAS LOST WORLD THEME PARK\n\n"<<endl;
cout<<setw(80)<<setfill('*')<<"*";
cout<<"\nPackage name \t | ADULT\t | CHILD\t | SENIOR CITIZEN"<<endl;
cout<<"|A| Water Park\t | 47.90\t | 41.50\t | 35.40"<<endl;
cout<<"|B| Am. Park\t | 25.90 \t | 20.50 \t | 15.40"<<endl;
cout<<"|C| Pet. Zoo\t | 15.90\t | 12.50\t | 10.40"<<endl<<endl;
cout<<setw(80)<<setfill('*')<<"*";
cout<<"\nChoose your package A/B/C: "<<endl;
cin>>option;
cout<<"Enter the no of adult: "<<endl;
cin>>noadult;
cout<<"Enter the no of child: "<<endl;
cin>>nochild;
cout<<"Enter the no of senior citizen: "<<endl;
cin>>nosenior;
switch(option)
{
case 'A':
case 'a':
totaladult=W_adult*noadult;
totalchild=W_child*nochild;
totalsenior=W_senior*nosenior;
break;
case 'B':
case 'b':
totaladult=A_adult*noadult;
totalchild=A_child*nochild;
totalsenior=A_senior*nosenior;
break;
case 'C':
case 'c':
totaladult=P_adult*noadult;
totalchild=P_child*nochild;
totalsenior=P_senior*nosenior;
break;
}
cout<<"Total no of adult is: "<<noadult<<endl;
cout<<"Total no of child is: "<<nochild<<endl;
cout<<"Total no of senior citizen is: "<<nosenior<<endl;
cout<<"Total price of adult is: "<<totaladult<<endl;
cout<<"Total price of child is: "<<totalchild<<endl;
cout<<"Total price of senior citizen is: "<<totalsenior<<endl;
cout<<"The total price is: "<<totaladult+totalchild+totalsenior<<endl;
return 0;
}
Is there something else I'm missing here? Currently it only calculates the price of one category. I would like know where should I include the loop statements.

you should add an option to quit, and while loop as long as the user keep selecting packages:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const double W_adult=47.90, W_child=41.50, W_senior=35.40, A_adult=25.90, A_child=20.50, A_senior=15.40, P_adult=15.90, P_child=12.50, P_senior=10.40;
double totaladult = 0,totalchild = 0,totalsenior = 0, total = 0;
int noadult = 0,nochild = 0,nosenior = 0;
char option;
bool run = true;
//char choice = 'y'; looping choice
cout<<"WELCOME TO WAS LOST WORLD THEME PARK\n\n"<<endl;
cout<<setw(80)<<setfill('*')<<"*";
cout<<"\nPackage name \t | ADULT\t | CHILD\t | SENIOR CITIZEN"<<endl;
cout<<"|A| Water Park\t | 47.90\t | 41.50\t | 35.40"<<endl;
cout<<"|B| Am. Park\t | 25.90 \t | 20.50 \t | 15.40"<<endl;
cout<<"|C| Pet. Zoo\t | 15.90\t | 12.50\t | 10.40"<<endl<<endl;
cout<<setw(80)<<setfill('*')<<"*";
while(run)
{
cout<<"\nChoose your package A/B/C --- or Q to quit: "<<endl;
cin>>option;
cout<<"Enter the no of adult: "<<endl;
cin>>noadult;
cout<<"Enter the no of child: "<<endl;
cin>>nochild;
cout<<"Enter the no of senior citizen: "<<endl;
cin>>nosenior;
switch(option)
{
case 'A':
case 'a':
totaladult+=W_adult*noadult;
totalchild+=W_child*nochild;
totalsenior+=W_senior*nosenior;
break;
case 'B':
case 'b':
totaladult+=A_adult*noadult;
totalchild+=A_child*nochild;
totalsenior+=A_senior*nosenior;
break;
case 'C':
case 'c':
totaladult+=P_adult*noadult;
totalchild+=P_child*nochild;
totalsenior+=P_senior*nosenior;
break;
case 'q':
case 'Q':
run = false;
break;
default:
// do nothing
break;
break;
}
cout<<"Total no of adult is: "<<noadult<<endl;
cout<<"Total no of child is: "<<nochild<<endl;
cout<<"Total no of senior citizen is: "<<nosenior<<endl;
cout<<"Total price of adult is: "<<totaladult<<endl;
cout<<"Total price of child is: "<<totalchild<<endl;
cout<<"Total price of senior citizen is: "<<totalsenior<<endl;
cout<<"The total price is: "<<totaladult+totalchild+totalsenior<<endl;
}
return 0;
}

Related

discount value cannot calculate after case commad

I have some problem with my homework. So this is how it looks like
#include<stdio.h>
int main()
{
char code;
int price,discount;
float total;
printf("Please input price: ");
scanf("%d",&price);
printf("Please input discount code: ");
scanf(" %c",&code);
switch(code)
{
case 'a': printf("Your discount code is 25 percent\n");
discount = 25;
break;
case 'b': printf("Your discount code is 15 percent\n");
discount = 15;
break;
case 'c': printf("Your discount code is 5 percent\n");
discount = 5;
break;
default: printf("Wrong code,Your discount is 0 percent\n");
discount = 0;
break;
}
total = (price*((100-discount)/100));
printf("Your price is = %.2f\n",total);
}
I have 2 questions to ask
My task is I have to input both of uppercase and lowercase letter for discount code (there are only three codes: a, b, c) so how can I put both of them in case command? (in this I only do the lowercase letter)
I have run this. But it seems like the discount value is 0 when I try to used it for calculate in the end. When I print the discount only, it works normally. How can I fix that?
Sorry for my poor English and Thank you for your help!
There would be different possibilities.
scanf(" %c",&code);
switch(code)
{
case 'a':
case 'A':
printf("Your discount code is 25 percent\n");
discount = 25;
break;
or you change the input to lower case before or in the switch!
switch( code | 0x60 ) // make it to lower case
with this you don't have to change the following code.

C++ switch statement error (variable not declared in this scope)

I'm currently learning about switches in C++, so I created a grading program that will output a specific message based on the grade entered. Below is the code:
#include <iostream>
using namespace std;
int main()
{
char student_grade;
cout << "Enter student's grade:" << "\n";
cin >> student_grade;
// switch statement for a simple grading system
switch(student_grade)
{
case A:
cout << "You have passed with a distinction!";
break;
case B:
cout << "You have a grade B,congrats.";
break;
case C:
cout << "Average grade,you can do better.";
break;
default:
cout<<"NIL grade";
break;
}
return 0;
}
I get this error once I run it, what did I do wrong?
/root/Desktop/practise.cpp: In function ‘int main()’:
/root/Desktop/practise.cpp:14:6: error: ‘A’ was not declared in this scope
14 | case A:
| ^
/root/Desktop/practise.cpp:17:6: error: ‘B’ was not declared in this scope
17 | case B:
| ^
/root/Desktop/practise.cpp:20:6: error: ‘C’ was not declared in this scope
20 | case C:
| ^
In the switch case, the character cases should be written in ' '. E.g. 'A', 'B'. There is a difference between A and 'A', 'A' is a character literal. It's of char type with value 97 on most of the systems.
The corrected code will be:-
#include<iostream>
using namespace std;
int main()
{
char student_grade;
cout<<"Enter student's grade:"<<"\n";
cin>>student_grade;
// switch statement for a simple grading system
switch(student_grade)
{
case 'A':
cout<<"You have passed with a distinction!";
break;
case 'B':
cout<<"You have a grade B,congrats.";
break;
case 'C':
cout<<"Average grade,you can do better.";
break;
default:
cout<<"NIL grade";
break;
}
return 0;
}
Simpe put it in ' ' because thats necessary for chars than it will compile.
Without that its just a variable.
#include<iostream>
using namespace std;
int main()
{
char student_grade;
cout<<"Enter student's grade:"<<"\n";
cin>>student_grade;
// switch statement for a simple grading system
switch(student_grade)
{
case 'A':
cout<<"You have passed with a distinction!";
break;
case 'B':
cout<<"You have a grade B,congrats.";
break;
case 'C':
cout<<"Average grade,you can do better.";
break;
default:
cout<<"NIL grade";
break;
}
return 0;
}

May i know how to solve the errors i have in my code?

Question :May i know how to solve the errors i have in my code ,view all and delete the waffle that the customer want and doesn't want without using any linked list?
C:\Users\Arianna\Documents\LABC++\Lab2\ASSIGNMENT DSA SEM2 2.cpp In function 'void orderForm()':
321 17 C:\Users\Arianna\Documents\LABC++\Lab2\ASSIGNMENT DSA SEM2 2.cpp [Error] 'extraFillings' was not declared in this scope
332 41 C:\Users\Arianna\Documents\LABC++\Lab2\ASSIGNMENT DSA SEM2 2.cpp [Error] invalid operands of types 'const char [29]' and '' to binary 'operator/'
335 19 C:\Users\Arianna\Documents\LABC++\Lab2\ASSIGNMENT DSA SEM2 2.cpp [Error] 'extraToppings' was not declared in this scope
341 15 C:\Users\Arianna\Documents\LABC++\Lab2\ASSIGNMENT DSA SEM2 2.cpp [Error] 'taxInvoice' was not declared in this scope
355 1 C:\Users\Arianna\Documents\LABC++\Lab2\ASSIGNMENT DSA SEM2 2.cpp [Error] a function-definition is not allowed here before '{' token
569 11 C:\Users\Arianna\Documents\LABC++\Lab2\ASSIGNMENT DSA SEM2 2.cpp [Error] expected '}' at end of input
#include<iostream>
#include<string>
using namespace std;
int main()
{
char choice,options;
char *tmp,*tmp2;
int sentinel=0;
double priceSoft1=10.00,priceCrispy1=12.00,priceSoft2=14.00,priceCrispy2=16.00,priceSoft3=12.00,priceCrispy3=14.00;
double payment,total;
double TotalAmt1,TotalAmt2,TotalAmt3,TotalAmt4,TotalAmt5,TotalAmt6;
double Fillings=1.20,Toppings=1.50;
double TotalAmount1,TotalAmount2,TotalAmount3,TotalAmount4,TotalAmount5,TotalAmount6;
char wMenu[100];
int listOption,orderForm;
int num1,num2,num3,num4,num5,num6;
}
class waffle
{
private:
struct cashier
{
char name [30]; //Name up to 30 letters
int idNum; //Id Number in Integer
cashier *next;//Pointer to the next node
};
cashier *start_ptr;//Start pointer (root)
public:
waffleShop()
{
start_ptr=NULL;
}//Add constructor and initialize start pointer to NULL
int cashierList();
void add_new_acc();
void displayList();
void delete_start_ptr();
void delete_end_node();
};
int waffle::cashierList()
{
char choice,options;
int listOption=0;
cout<<"Please choose the option from the list";
cout<<"<<1>>Add node at the end of the list"<<endl;
cout<<"<<2>>Display List"<<endl;
cout<<"<<3>>Delete first"<<endl;
cout<<"<<4>>Delete last"<<endl;
cout<<"<<5>>Sign Out"<<endl;
cin>>listOption;
switch(listOption)
{
case 1:
add_new_acc();
break;
case 2:
displayList();
break;
case 3:
delete_start_ptr();
break;
case 4:
delete_end_node();
break;
case 5:
cout<<"Would you like to sign out?";
cout<<" \n Press [Y] for Yes, and [N] for No.\n\n\tEnter Choice : ";
cin >>choice;
if ((choice !='N')&&(choice !='n'))
{
return cashierList();
}
else
{
cout<<"You have already sign out from the system";
system("cls");
}
break;
}
return 0;
}
void waffle::add_new_acc()
{
cashier *tmp,*tmp2;//temporary pointers
tmp=new cashier;
//Reserve space for the new node to fill in data
cout<<"Create New Account"<<endl;
cout<<"Please enter your name:"<<endl;
cin>>tmp->name;
cout<<"To create your Cashier ID"<<endl;
cout<<"Please enter last 4 digit of your Id: "<<endl;
cin>>tmp->idNum;
tmp->next=NULL;
if(start_ptr==NULL)
start_ptr=tmp;
else
{
tmp2=start_ptr;//The list is not empty since it is not NULL
while(tmp2->next!=NULL)
tmp2=tmp2->next;//move to next link
tmp2->next=tmp;
}
cashierList();
}
void waffle::displayList()
{
char wMenu[100];
cashier *tmp;
tmp=start_ptr;
while(tmp)
{
cout<<"Cashier Name:"<<tmp->name<<endl;
cout<<"Cashier Id:"<<tmp->idNum<<endl;
tmp=tmp->next;
}
main();
}
void waffle::delete_start_ptr()
{
cashier *tmp;
tmp=start_ptr;
start_ptr=start_ptr->next;
delete tmp;
cashierList();
}
void waffle::delete_end_node()
{
cashier *tmp1,*tmp2;
if(start_ptr==NULL)
cout<<"You're not in the list"<<endl;
else
{
tmp1=start_ptr;
if(tmp1->next==NULL)
{
cout<<"You are a New User!!"<<endl;
delete tmp1;
start_ptr=NULL;
}
else
{
while(tmp1->next !=NULL)
{
tmp2=tmp1;
tmp1=tmp1->next;
}
delete tmp1;
tmp2->next=NULL;
}
}
cashierList();
}
void wMenu()
{
char options;
cout<<"----------------------------------------------------------------------";
cout<<"WAFFLE TYPE ! CRISPY(RM) ! SOFT(RM) ! EXTRA CHARGE(RM) !";
cout<<"----------------------------------------------------------------------";
cout<<"1)American ! 12.00 ! 10.00 ! - RM1.20 for additional filling";
cout<<"2)Belgium ! 16.00 ! 14.00 ! - RM1.50 additional topping";
cout<<"3)Rolled ! 14.00 ! 12.00 !/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t";
cout<<"----------------------------------------------------------------------";
cout<<"Options:";
cout<<"\nFilling-choc,butter,kaya,ice cream,strawberry,blueberry";
cout<<"\nTopping(except for Rolled Waffle)";
cout<<"a)ice cream-vanila,chocolate or mix flavour";
cout<<"b)fruits-banana or strawberry";
cout<<"----------------------------------------------------------------------";
cout<<"Would you like proceed your order?"<<endl;
cin>>options;
cout<<" \n Press [Y] for Yes, and [N] for No.\n\n\tEnter Options : ";
if ((options !='N')&&(options !='n'))
{
main();
}
else
{
cout<<"Next Customer please ";
return wMenu();
}
}
void orderForm()
{
int order, choiceA,choiceB;
int num1,num2,num3,num4,num5,num6;
double AmountSale1,AmountSale2,AmountSale3,AmountSale4,AmountSale5,AmountSale6;
double priceSoft1=10.00,priceCrispy1=12.00,priceSoft2=14.00,priceCrispy2=16.00,priceSoft3=12.00,priceCrispy3=14.00;
cout<<"----------------------------------";
cout<<"(1)Soft American Waffle RM10.00 ";
cout<<"----------------------------------";
cout<<"(2)Crispy American Waffle RM12.00 ";
cout<<"----------------------------------";
cout<<"(3)Soft Belgium Waffle RM14.00 ";
cout<<"----------------------------------";
cout<<"(4)Crispy Belgium Waffle RM16.00 ";
cout<<"----------------------------------";
cout<<"(5)Soft Rolled Waffle RM12.00 ";
cout<<"----------------------------------";
cout<<"(6)Crispy Rolled Waffle RM14.00 ";
cout<<"----------------------------------";
while(order!=0)
{
do
{
cout<<"\nFrom the waffle menu , what would you like : ";
cin>>order;
}
while(1<order<5);
switch(order)
{
case 1:
cout<<"How many soft American waffle would you like:";
cin>>num1;
AmountSale1=priceSoft1*num1;
break;
case 2:
cout<<"How many crispy American waffle would you like:";
cin>>num2;
AmountSale2=priceCrispy1*num2;
break;
case 3:
cout<<"How many soft Belgium waffle would you like: ";
cin>>num3;
AmountSale3=priceSoft2*num3;
break;
case 4:
cout<<"How many crispy Belgium waffle would you like: ";
cin>>num4;
AmountSale4=priceCrispy2*num4;
break;
case 5:
cout<<"How many soft Rolled waffle would you like :";
cin>>num5;
AmountSale5=priceSoft3*num5;
break;
case 6:
cout<<"How many crispy Rolled waffle would you like :";
cin>>num6;
AmountSale6=priceCrispy3*num6;
break;
default:cout<<"Please choose a valid order from the menu\n";
}
cout<<" Do you want to add fillings? ";
cout<<" \n Press [Y] for Yes, and [N] for No.\n\n\tEnter Choice : ";
cin >> choiceA;
if ((choiceA ='Y')&&(choiceA !='N')&&(choiceA ='y')&&(choiceA !='n'))
{
extraFillings();
}
else
{
cout<<"Would you like to add Topings?";
cout<<" \n Press [Y] for Yes, and [N] for No.\n\n\tEnter Choice : ";
cin>>choiceB;
if((choiceB ='Y')&&(choiceB !='N')&&(choiceB ='y')&&(choiceB !='n'))
{
cout<<"Topping(except ROLLED WAFFLE"/endl;
if(order!=5&&order!=6)
{
extraToppings();
}
else
{
taxInvoice();
}
}
}
}
void extraFillings()
{
int typeofFillings,
cout<<"----------------------------------";
cout<<"(1)Soft American Waffle RM10.00 ";
cout<<"----------------------------------";
cout<<"(2)Crispy American Waffle RM12.00 ";
cout<<"----------------------------------";
cout<<"(3)Soft Belgium Waffle RM14.00 ";
cout<<"----------------------------------";
cout<<"(4)Crispy Belgium Waffle RM16.00 ";
cout<<"----------------------------------";
cout<<"(5)Soft Rolled Waffle RM12.00 ";
cout<<"----------------------------------";
cout<<"(6)Crispy Rolled Waffle RM14.00 ";
cout<<"----------------------------------";
cout<<"Waffle Added with Filings RM1.20 will be charged"
cout<<"Remark: ";
cin>>typeofFillings;
switch(order)
{
case 1:
TotalAmt1=AmountSale1+Fillings;
break;
case 2:
TotalAmt2=AmountSale2+Fillings;
break;
case 3:
TotalAmt3=AmountSale3+Fillings;
break;
case 4:
TotalAmt4=AmountSale4+Fillings;
break;
case 5:
TotalAmt5=AmountSale5+Fillings;
break;
case 6:
TotalAmt6=AmountSale6+Fillings;
break;
default:cout<<"Please choose a valid order from the menu\n";
}
extraTopings(void);
}
void extraTopings()
{
cout<<"----------------------------------";
cout<<"(1)Soft American Waffle RM10.00 ";
cout<<"----------------------------------";
cout<<"(2)Crispy American Waffle RM12.00 ";
cout<<"----------------------------------";
cout<<"(3)Soft Belgium Waffle RM14.00 ";
cout<<"----------------------------------";
cout<<"(4)Crispy Belgium Waffle RM16.00 ";
cout<<"----------------------------------";
cout<<"*****TOPPINGS OF ROLLED WAFFLE IS INVALID*****";
cout<<"Remark: ";
cin>>typeofTopings;
switch(order)
{
case 1:
TotalAmount1=AmountSale1+Toppings;
break;
case 2:
TotalAmount2=AmountSale2+Toppings;
break;
case 3:
TotalAmount3=AmountSale3+Toppings;
break;
case 4:
TotalAmount4=AmountSale4+Toppings;
break;
default:cout<<"Please choose a valid order from the menu\n";
}
taxInvoice();
}
void taxInvoice();
{
int ans,select;
double payment;
cout<<"----------------------------------";
cout<<"(1) Normal Waffle ";
cout<<"----------------------------------";
cout<<"(2) Waffle with Fillings ";
cout<<"----------------------------------";
cout<<"(3) Waffle with Toppings ";
cout<<"----------------------------------";
cout<<" You have ordered:\n\n";
cout<<"Select the selection ";
cin>>select;
if(select==1)
{
cout<<"NORMAL"/endl;
cout<<" ITEM\t\t\t\tQUANTITY\tUNIT PRICE\tAMOUNT OF SALE\n";
cout<<" Soft American Waffle:\t\t\t"<<num1<<"\t\tRM "<<priceSoft1<<"\tRM "<<AmountofSale1<<"\n";
cout<<" Crispy American Waffle:\t\t"<<num2<<"\t\tRM "<<priceCrispy1<<"\tRM "<<AmountofSale2<<"\n";
cout<<" Soft Belgium Waffle:\t" <<num3<<"\t\tRM "<<priceSoft2<<"\tRM "<<AmountofSale3<<"\n";
cout<<" Crispy Belgium Waffle:\t\t" <<num4<<"\t\tRM "<<priceCrispy2<<"\tRM "<<AmountofSale4<<"\n";
cout<<" Soft Rolled Waffle:\t\t\t" <<num5<<"\t\tRM "<<priceSoft3<<"\tRM "<<AmountofSale5<<"\n";
cout<<" Crispy Rolled Waffle:\t\t\t"<<num6<<"\t\tRM "<<priceCrispy3<<"\tRM "<<AmountofSale6<<"\n";
total=0;
total=total+AmountofSale1+AmountofSale2+AmountofSale3+AmountofSale4+AmountofSale5+AmountofSale6;
}
else
{
if(select==2)
{
cout<<"With Fillings"/endl;
cout<<" ITEM\t\t\t\tQUANTITY\tUNIT PRICE\tAMOUNT OF SALE\n";
cout<<" Soft American Waffle:\t\t\t"<<num1<<"\t\tRM "<<priceSoft1<<"\tRM "<<TotalAmt1<<"\n";
cout<<" Crispy American Waffle:\t\t"<<num2<<"\t\tRM "<<priceCrispy1<<"\tRM "<<TotalAmt2<<"\n";
cout<<" Soft Belgium Waffle:\t" <<num3<<"\t\tRM "<<priceSoft2<<"\tRM "<<TotalAmt3<<"\n";
cout<<" Crispy Belgium Waffle:\t\t" <<num4<<"\t\tRM "<<priceCrispy2<<"\tRM "<<TotalAmt4<<"\n";
cout<<" Soft Rolled Waffle:\t\t\t" <<num5<<"\t\tRM "<<priceSoft3<<"\tRM "<<TotalAmt5<<"\n";
cout<<" Crispy Rolled Waffle:\t\t\t"<<num6<<"\t\tRM "<<priceCrispy3<<"\tRM "<<TotalAmt6<<"\n";
total=0;
total=total+TotalAmt1+TotalAmt2+TotalAmt3+TotalAmt4+TotalAmt5+TotalAmt6;
}
else
{
if(select==3)
{
cout<<"With Toppings"/endl;
cout<<" Soft American Waffle:\t\t\t"<<num1<<"\t\tRM "<<priceSoft1<<"\tRM "<<TotalAmount1<<"\n";
cout<<" Crispy American Waffle:\t\t"<<num2<<"\t\tRM "<<priceCrispy1<<"\tRM "<<TotalAmount2<<"\n";
cout<<" Soft Belgium Waffle:\t "<<num3<<"\t\tRM "<<priceSoft2<<"\tRM "<<TotalAmount3<<"\n";
cout<<" Crispy Belgium Waffle:\t\t "<<num4<<"\t\tRM "<<priceCrispy2<<"\tRM "<<TotalAmount4<<"\n";
cout<<" Soft Rolled Waffle:\t\t\t "<<num5<<"\t\tRM "<<priceSoft3<<"\tRM "<<TotalAmount5<<"\n";
cout<<" Crispy Rolled Waffle:\t\t\t"<<num6<<"\t\tRM "<<priceCrispy3<<"\tRM "<<TotalAmount6<<"\n";
total=0;
total=total+TotalAmount1+TotalAmount2+TotalAmount3+TotalAmount4+TotalAmount5+TotalAmount6;
}
}
}
cout<<"\t\t\t\t\t\tThat would be: RM "<<total<<"\n";
cout<<"Order Again[0]no[1]yes"
cin<<ans;
if(ans==0)
{
if(payment<total)
{
cout<<"Insufficient Money";
}
else
{
cout<<"\n\tEnter Received Cash : ";
cin >> payment;
if(payment>total)
{
change=payment-total;
cout<<"\tYour change is:"<<change<<"\n\n\n\n\t\t"
}
}
do
{
cout<<" New transaction [0] no [1] yes : ";
cin >> ans;
} while ((ans != 0)&&(ans != 1));
}
system("cls\n\n\n\t\t");
system("PAUSE");
return 0;
}
The error "function X was not declared in this scope" is because you call a function that is declared after the one calling it in the file. At compile time there is no way for the compiler to know what it is.
For example : orderForm calls extraFillings it is not defined yet as it is defined later in the file.
You need to create a header file containing the prototypes of your functions and linked it to your project or you need to make sure that the function you are calling are declared before the caller.

Error : expected primary-expression before '.' token

Ok so I'm having trouble with this C++ program I am supposed to do for school and I need some help with an error I keep getting. I have the basics but I need help with classes and objects.
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
class BankAccount {
private:
double accountBalance;
string name;
public:
BankAccount();
BankAccount(string,double);
double deposit(double);
double withdraw(double);
void checkBalance();
};
BankAccount::BankAccount()
{
accountBalance=0;
name="";
}
BankAccount::BankAccount(string name,double money)
{
name=name;
accountBalance=accountBalance;
}
double BankAccount::deposit(double money)
{
accountBalance+=money;
return accountBalance;
}
double BankAccount::withdraw(double money)
{
accountBalance-=money;
return accountBalance;
};
void BankAccount::checkBalance()
{
cout<<"The balance on the account is $"<<accountBalance<<"!!"<<endl;
};
int main(int argc, char *argv[])
{
int c;
double m;
string n;
cout<<"==================Bank======="<<endl;
cout<<"[1] Open a new Bank Account |"<<endl;
cout<<"[2] Deposit money |"<<endl;
cout<<"[3] Withdraw money |"<<endl;
cout<<"[4] Check balance |"<<endl;
cout<<"============================="<<endl;
cout<<endl;
cout<<"What would you like to do :";
cin>>c;
switch (c){
case 1:
cout<<"Ok I see you want to open a new Bank Account"<<endl;
cout<<"But first answer a few questions:"<<endl;
cout<<"What is your name? ";
cin>>n;
cout<<"Next tell me the amount of money you wish to open your account with: ";
cin>>m;
BankAccount::BankAccount(n,m);
cout<<"OK all set, "<<n<<"!!"<<endl;
break;
case 2:
cout<<"How much money would you like to deposit? : ";
cin>>m;
BankAccount.deposit(m);
break;
case 3:
cout<<"How much money would you like to withdraw? : ";
cin>>m;
BankAccount.withdraw(m);
break;
case 4:
cout<<"OK I'll check your balance"<<endl;
BankAccount.checkBalance();
break;
}
system("PAUSE");
return EXIT_SUCCESS;
}
So if you could help me that would be very much appreciated.
BankAccount is a type name, not a variable name. You cannot invoke instance methods on a type.
Create a variable of type BankAccount, assign it an instance, and then call methods on the instance using the same notation that you already have:
BankAccount acct;
switch (c){
case 1:
cout<<"Ok I see you want to open a new Bank Account"<<endl;
cout<<"But first answer a few questions:"<<endl;
cout<<"What is your name? ";
cin>>n;
cout<<"Next tell me the amount of money you wish to open your account with: ";
cin>>m;
acct = BankAccount(n,m);
cout<<"OK all set, "<<n<<"!!"<<endl;
break;
case 2:
cout<<"How much money would you like to deposit? : ";
cin>>m;
acct.deposit(m);
break;
case 3:
cout<<"How much money would you like to withdraw? : ";
cin>>m;
acct.withdraw(m);
break;
case 4:
cout<<"OK I'll check your balance"<<endl;
acct.checkBalance();
break;
}
BankAccount is a class you defined, you cannot use :: operator to access non-static members of that class. In this case, you need to first create an object of BankAccount, then using dot operator to access the deposit and other member functions since those non-static member functions are associated with instance of the class.
If deposit and other relevant functions are static member function, you can do that.

c++:how to get balance (in banking system)

i'm doing this project Banking System
This system tracks customers’ accounts in a bank. Each account has a number, name, and balance. The system provides the following functionalities: create new account, withdraw, deposit, and close account.
The system has the following interface:
Choose:
1- Add new account
2- Withdraw
3- Deposit
4- Get Balance
5- Exit
 When the user chooses 1, the system generates a new ID, and then asks the user to enter a name for that account. The initial balance is set to zero.
 When the user chooses 2, the system asks the user to enter account ID and amount to be withdrawn. If this amount is greater than the balance, a message is displayed that this transaction failed because insufficient balance. If balance is enough, it decreases by amount to be withdrawn.
 When the user chooses 3. The system asks the user to enter account ID and amount to be deposited. System increases balance by this amount.
 When the user chooses 4, the system asks the user to enter account ID then prints account’s name and balance.
 Each time a task is completed the system gets back to the main menu above until the user chooses 5.
# include <iostream>
#include <string>
using namespace std;
# include<iomanip>
class Bank
{
private:
char name;
int acno;
float balance;
public:
void newAccount();
void withdraw();
void deposit();
void getbalance();
void disp_det();
};
//member functions of bank class
void Bank::newAccount()
{
cout<<"New Account";
cout<<"Enter the Name of the depositor : ";
cin>>name;
cout<<"Enter the Account Number : ";
cin>>acno;
cout<<"Enter the Amount to Deposit : ";
cin >>balance;
}
void Bank::deposit()
{
float more;
cout <<"Depositing";
cout<<"Enter the amount to deposit : ";
cin>>more;
balance+=more;
}
void Bank::withdraw()
{
float amt;
cout<<"Withdrwal";
cout<<"Enter the amount to withdraw : ";
cin>>amt;
balance-=amt;
}
void Bank::disp_det()
{
cout<<"Account Details";
cout<<"Name of the depositor : "<<name<<endl;
cout<<"Account Number : "<<acno<<endl;
cout<<"Balance : $"<<balance<<endl;
}
// main function , exectution starts here
void main(void)
{
Bank obj;
int choice =1;
while (choice != 5 )
{
cout<<"Enter \n 1- to create new account \n 2- Withdraw\n 3- Deposit \n 4- get balance\n 5 Exit"<<endl;
cin>>choice;
switch(choice)
{
case '1' :obj.newAccount();
break;
case '2' :obj.withdraw();
break;
case 3: obj.deposit();
break;
case 4: getbalance();
break;
case 5:
break;
default: cout<<"Illegal Option"<<endl;
}
}
}
Problem 1:
You have made a typo in method which gets you the balance & the one which you are calling, rename Bank::disp_det() to Bank::getbalance()
void Bank::getbalance()
{
cout<<"Account Details";
cout<<"Name of the depositor : "<<name<<endl;
cout<<"Account Number : "<<acno<<endl;
cout<<"Balance : $"<<balance<<endl;
}
Problem 2:
You are not calling Bank::getbalance through an object of Bank, Since it is a member function you should call it as follows:
case 4:
obj.getbalance();
break;
In case 4, you should call obj.getbalance(). And it's not written yet: it seems you have written a disp_det() instead that shows the balance. Try renaming.
This isn't doing exactly what you want, as the case labels have different types:
switch(choice)
{
case '1' :obj.newAccount();
break;
case '2' :obj.withdraw();
break;
case 3: obj.deposit();
break;
case 4: getbalance();
break;
case 5:
break;
default: cout<<"Illegal Option"<<endl;
}
To select options '1' or '2', the user will have to type 31 and 32 when choice is an int.