I have written a code like bill payment. Code is working fine but there are many warnings in my code which I want to remove. One of the most frequent warning is deprecated conversion from string constant to 'char* . I have tried many things and some of the warnings are gone but not all. Please anybody point out at my mistakes??
P.S: I have already tried replacing char* to const char* , but then I am not able to exchange or swap values and it was causing error. Any other solution??
Below is the code
#include<iostream>
#include<string.h>
using namespace std;
class item
{
private:
int barcode;
char* item_name;
public:
item (int num=0, char* name="NULL") : barcode(num)
{
item_name=new char[strlen(name)+1];
strcpy(item_name,name);
}
void setbarcode(int num)
{
barcode=num;
}
int getbarcode()
{
return barcode;
}
void scanner()
{
int num;
cin>>num;
setbarcode(num);
}
void printer()
{
cout <<"\nBarcode"<<"\t\t"<<"Item Name"<<"\t\t"<<"Price"<<endl;
cout <<barcode<<"\t\t"<<item_name<<"\t\t\t";
}
~item()
{
delete[]item_name;
}
};
class packedfood : public item
{
private :
int price_per_piece;
public :
packedfood(int a=0, int num=0, char* name = "NULL") : price_per_piece(a),item(num,name)
{
}
void setprice(int num)
{
price_per_piece=num;
}
int getprice()
{
return price_per_piece;
}
void scanner()
{
item::scanner();
}
void printer()
{
item::printer();
cout<<getprice()<<" Per Piece";
}
~packedfood()
{
}
};
class freshfood: public item
{
private:
int price_per_rupee;
float weight;
public:
freshfood(float b=0, int a=0,int num=0,char* name="NULL") : weight(b),price_per_rupee(a),item(num,name)
{
}
void setweight(float b)
{
weight=b;
}
int getweight()
{
return weight*50;
}
void printer()
{
item::printer();
cout<<getweight()<<" Per Rupee"<<endl<<endl;
}
void scanner()
{
item::scanner();
}
~freshfood()
{
}
};
int main()
{
item x(389,"Anything");
item y;
packedfood m(10,118,"Chocolate");
packedfood n;
freshfood r(20.9,93,357,"Fruits");
freshfood s;
cout <<"\n\n Enter the Barcode for Packed food : ";
m.scanner();
m.printer();
cout <<"\n\n Enter the Barcode for Fresh food : ";
r.scanner();
r.printer();
system("PAUSE");
return 0;
}
Related
In my function 'func' i want to create an object Menu who returns a breakfast with lemon and coffee. 'func' return a list of menu
When I try to display the menu for a breakfast that contains only lemon or only coffee it is displayed correctly. For example:
c.push_back(make_unique<Breakfast>("eggs", 10));
but when i try to display a menu that also contains lemon and coffee it shows me error in main
this is the program:
#include <iostream>
#include <vector>
using namespace std;
class Menu {
private:
int price;
public:
Menu(int p = 0) : price{ p } {}
virtual string description() = 0;
virtual int getPrice() {
return price;
}
virtual ~Menu() {}
};
class WithLemon : public Menu {
private:
Menu* meniu;
public:
WithLemon(Menu* n) :
meniu{ n } {}
string description() override {
return meniu->description() + " with lemon ";
}
int getPrice() override {
return meniu->getPrice() + 4;
}
};
class WithCoffee : public Menu {
private:
Menu* meniu;
public:
WithCoffee(Menu* n) :
meniu{ n } {
}
string description() override {
return meniu->description() + " with coffee ";
}
int getPrice() override {
return meniu->getPrice() + 5;
}
};
class Breakfast : public Menu {
private:
string name;
public:
Breakfast(string n, int p) :
name{ n }, Menu{ p } {
}
string description() override {
return name;
}
};
std::vector<std::unique_ptr<Menu>> func(void)
{
std::vector <std::unique_ptr<Menu> > c;
Breakfast a{ "breakfast eggs", 10 };
WithCoffee breakfast_with_coffee{ &a };
Menu* breakfast_with_coffee_and_lemon = new WithLemon{ &breakfast_with_coffee };
//cout << breakfast_with_coffee_and_lemon->description() << " " << breakfast_with_coffee_and_lemon->getPrice();// print ----> breakfast eggs with coffee with lemon 19
c.push_back(make_unique<WithLemon>(&breakfast_with_coffee));
return c;
}
int main() {
std::vector < std::unique_ptr<Menu> > lista = func();
for (int i = 0; i < lista.size(); i++) {
cout << lista[i]->description() << " " << lista[i]->getPrice() << endl; //error read memory access
}
return 0;
}
You can't take a pointer to automatic memory, store it in a smart pointer and leave the function. After you leave the function the automatic memory is freed and the smart pointer contains a dangling pointer. The simplest way to avoid this problem and other problems with memory leaks is to use smart pointers for all variables:
#include <iostream>
#include <memory>
#include <string>
#include <vector>
class Menu {
private:
int price;
public:
Menu(int p = 0) : price{ p } {}
virtual std::string description() = 0;
virtual int getPrice() {
return price;
}
virtual ~Menu() = default;
};
class WithLemon : public Menu {
private:
std::unique_ptr<Menu> meniu;
public:
WithLemon(Menu* n) : meniu{ n } {}
std::string description() override {
return meniu->description() + " with lemon ";
}
int getPrice() override {
return meniu->getPrice() + 4;
}
};
class WithCoffee : public Menu {
private:
std::unique_ptr<Menu> meniu;
public:
WithCoffee(Menu* n) :
meniu{ n } {
}
std::string description() override {
return meniu->description() + " with coffee ";
}
int getPrice() override {
return meniu->getPrice() + 5;
}
};
class Breakfast : public Menu {
private:
std::string name;
public:
Breakfast(std::string n, int p) : Menu{ p }, name{ n } {}
std::string description() override {
return name;
}
};
std::vector<std::unique_ptr<Menu>> func(void) {
std::vector <std::unique_ptr<Menu> > c;
auto a = std::make_unique<Breakfast>("breakfast eggs", 10);
auto breakfast_with_coffee = std::make_unique<WithCoffee>(a.release());
//Menu* breakfast_with_coffee_and_lemon = new WithLemon{ breakfast_with_coffee };
//std::cout << breakfast_with_coffee_and_lemon->description() << " " << breakfast_with_coffee_and_lemon->getPrice();// print ----> breakfast eggs with coffee with lemon 19
c.push_back(std::make_unique<WithLemon>(breakfast_with_coffee.release()));
return c;
}
int main() {
std::vector < std::unique_ptr<Menu> > lista = func();
for (const auto &i : lista) {
std::cout << i->description() << " " << i->getPrice() << std::endl; //error read memory access
}
return 0;
}
Avoid raw new and delete. Avoid pointers to automatic memory.
I am trying to write a super basic program which creates an array of objects under class Receipt. The class includes an int price, string good (name), and a simple function that adds an item to the list. I am stuck because every time I compile it seg faults before it even gets to the add function, meaning something is wrong with my default constructor.
I am still really new to C++ and pointers are probably my biggest struggle. I have looked online and at my lecture notes trying to figure out what I am doing wrong. I feel like it's something small but I cannot figure it out.
Here is my program:
#include <iostream>
#include <string>
using namespace std;
class Receipt {
private:
int price;
string good;
Receipt* goods[500]; //partially filled array
public:
Receipt();
void add(string name, int cost);
string getName();
int getPrice();
void setName(string name_in);
void setPrice(int price_in);
void displayList();
};
Receipt::Receipt()
{
for (int i=0; i < 500; i++)
{
goods[i]->setName("Empty");
goods[i]->setPrice(-1);
}
}
void Receipt::add(string name, int cost)
{
int place=0;
for (int i=0; i <500; i++)
{
if (goods[i]->getName()=="Empty" && goods[i]->getPrice()==-1)
{
place = i;
break;
}
}
goods[place]->setName(name);
goods[place]->setPrice(cost);
}
int Receipt::getPrice()
{
return price;
}
string Receipt::getName()
{
return good;
}
void Receipt::setName(string name_in)
{
good = name_in;
}
void Receipt::setPrice(int price_in)
{
price = price_in;
}
void Receipt::displayList()
{
//just displaying first item in list for debugging purposes
cout << goods[0]->getName() << endl << goods[0]->getPrice();
}
int main()
{
Receipt mine; //seg faults here
mine.add("banana", 50);
mine.displayList();
return 0;
}
your design is wrong, you have array of Receipt inside Receipt so when you initialize the object, it create 500 where each of them create another 500 endlessly. I think you want to create something like this instead
#include <iostream>
#include <string>
using namespace std;
class Receipt {
private:
int price;
string good;
public:
void setName(string name_in);
void setPrice(int price_in);
string getName();
int getPrice();
};
class Receipts {
private:
Receipt* goods[500]; //partially filled array
public:
Receipts();
void add(string name, int cost);
void displayList();
};
Receipts::Receipts()
{
for (int i = 0; i < 500; i++)
{
goods[i] = new Receipt();
goods[i]->setName("Empty");
goods[i]->setPrice(-1);
}
}
void Receipts::add(string name, int cost)
{
int place = 0;
for (int i = 0; i <500; i++)
{
if (goods[i]->getName() == "Empty" && goods[i]->getPrice() == -1)
{
place = i;
break;
}
}
goods[place]->setName(name);
goods[place]->setPrice(cost);
}
int Receipt::getPrice()
{
return price;
}
string Receipt::getName()
{
return good;
}
void Receipt::setName(string name_in)
{
good = name_in;
}
void Receipt::setPrice(int price_in)
{
price = price_in;
}
void Receipts::displayList()
{
//just displaying first item in list for debugging purposes
cout << goods[0]->getName() << endl << goods[0]->getPrice();
}
int main()
{
Receipts mine; //seg faults here
mine.add("banana", 50);
mine.displayList();
return 0;
}
It has two issues
1). program is not taking input it exits without getting value of variable option.
2).Also my base and derived class are not initializing they are displaying garbage value.
Here is complete code.
#include<iostream>
using namespace std;
class BeverageItem
{
protected:
string name;
double price;
public:
void set_name(string n);
string get_name();
void set_price(double pr);
double get_price();
};
void BeverageItem::set_name(string n)
{
name=n;
}
string BeverageItem::get_name()
{
return(name);
}
void BeverageItem::set_price(double pr)
{
price=pr;
}
double BeverageItem::get_price()
{
return(price);
}
class HotBeverage:public BeverageItem
{
private:
int tea_bags;
int whiteners;
public:
//HotBeverage(int bg,int wht);
void set_teabags(int t_bags);
int get_teabags();
int getwhiteners();
void set_whiteners(int wht);
double basePrice();
double computeTax();
double totalCost();
void print();
};
double HotBeverage::computeTax()
{
return (0.16*price);
}
double HotBeverage::totalCost()
{
return(price+computeTax());
}
double HotBeverage::basePrice()
{
double pr;
if((tea_bags==1)&& (whiteners==1))
{
pr=20;
}
else if((tea_bags>1)&& (whiteners>1))
{
tea_bags=tea_bags-1;
pr=20+(5*tea_bags);
}
set_price(pr);
return(pr);
}
void HotBeverage::set_teabags(int t_bags)
{
tea_bags=t_bags;
}
int HotBeverage::get_teabags()
{
return(tea_bags);
}
int HotBeverage::HotBeverage::getwhiteners()
{
return(whiteners);
}
void HotBeverage::set_whiteners(int wht)
{
whiteners=wht;
}
void HotBeverage::print()
{
cout<<"Name: "<<name<<endl;
cout<<"Tax:"<<computeTax()<<endl;
cout<<"Total Cost: "<<totalCost()<<endl;
}
class ColdBeverage:public BeverageItem
{
private:
int drinkSize;
public:
//ColdBeverage(int drinkSize);
void setDrinkSsize(int sz);
int getDrinkSize();
double basePrice();
double computeTax();
double totalCost();
void print();
};
void ColdBeverage::print()
{
cout<<"Name: "<<name<<endl;
cout<<"Tax:"<<computeTax()<<endl;
cout<<"Total Cost: "<<totalCost()<<endl;
}
void ColdBeverage::setDrinkSsize(int sz)
{
drinkSize=sz;
}
int ColdBeverage::getDrinkSize()
{
return(drinkSize);
}
double ColdBeverage::computeTax()
{
return (0.16*price);
}
double ColdBeverage::totalCost()
{
return(price+computeTax());
}
double ColdBeverage::basePrice()
{
double pr;
double regularPr=30;
switch(drinkSize)
{
case 1: //regular,
pr=regularPr;
break;
case 2: //large.
price=1.5*regularPr;
break;
case 3: //extra large.
price=2*regularPr;
break;
}
set_price(pr);
return(pr);
}
int main()
{
string name;
int option;
cout<<"Enter The Beverage Name=";
cin>>name;
cout<<"1. For Hot Beverage\n\n";
cout<<"2. For Cold Beverage\n\n";
cout<<"Select Your Choice(1,2)=";
cin>>option;
BeverageItem bi;
bi.set_name(name);
**//some other code here.**
return 0;
}
1) I don't really see a problem with the compiler i tried with
http://cpp.sh/4p5mw
2) POD's in member variables are not default initialised.
https://stackoverflow.com/a/15212447/4669663
Here was the issue
cin>>name;
cin stops when whitespaces are entered in a string so I would have to use
getline(cin,name);
Second issue was because i was not calling the basePrice() before print()
thanks to everyone
I'm new to c++ because all throughout our lab activities we just use C# and I'm still trying get the gist of it. So our last lab, which uses c++, is to show our genealogy-our family tree- and my problem right now is printing. Can someone point out what's wrong in my function or why it's not printing and what's the best way to do it?
#include <iostream>
#include <string>
using namespace std;
class parent
{
public:
void setMom(string mom)
{
mother=mom;
}
void setDad(string dad)
{
father=dad;
}
string getDad(void)
{
return father;
}
string getMom(void)
{
return mother;
}
protected:
string mother;
string father;
};
class Member: public parent
{
public:
string name;
Member()
{
}
void setName(string kid)
{
name=kid;
}
void setStatus(string stat)
{
status=stat;
}
void setAge(int num)
{
age=num;
}
string getName()
{
return name;
}
private:
string status;
int age;
};
char addPerson()
{
Member person;
char mom[50];
char dad[50];
char kid[50];
char stat[7];
int num;
cout<<"Name: ";
cin>>kid;
person.setName(kid);
cout<<"Age: ";
cin>>num;
person.setAge(num);
cout<<"Status(Living/Diseased): ";
cin>>stat;
person.setStatus(stat);
cout<<"Father: ";
cin>>dad;
person.setDad(dad);
cout<<"Mother: ";
cin>>mom;
person.setMom(mom);
}
my function for printing.
void showme()
{
Member person;
cout<<person.getMom();
}
//-----------------------------------------------------MAIN--------------------------------------------------
int main()
{
while(1)
{
int choice;
cout<<" 1. Add member"<<"\n";
cout<<" 2. Edit member"<<"\n";
cout<<" 3. Show family tree"<<"\n";
cout<<" 4. Exit"<<"\n";
cin>>choice;
if(choice==1)
{
addPerson();
}
else if(choice==2)
{
}
else if(choice==3)
{
showme();
}
else if(choice==4)
{
break;
}
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
#include <iostream>
#include <cstring>
using namespace std;
class assistantnurse{
private:
char* id;
char dutytime;
public:
assistantnurse(char[] ="", char =' ');
void setid(char*);
void setdutytime(char);
char* getid()const;
char getdutytime()const;
void print()const;
void inputinfo();
~assistantnurse();
};
assistantnurse::~assistantnurse()
{
delete[] id;
id=0;
}
assistantnurse::assistantnurse(char* i, char t)
{
setid(i);
setdutytime(t);
}
void assistantnurse::setid(char *i)
{
int l=strlen(i);
id=new char[l+1];
strncpy(id,i,l);
id[l]='\0';
}
void assistantnurse::setdutytime(char t)
{
dutytime=t;
}
char* assistantnurse::getid()const{return id;}
char assistantnurse::getdutytime()const{return dutytime;}
void assistantnurse::print()const
{
cout<<"ID: "<<id<<endl;
cout<<"Duty time: "<<dutytime<<endl<<endl;
}
void assistantnurse::inputinfo()
{
char nurseid[20];
char time;
cout<<"Enter nurse ID: ";
cin>>nurseid;
do
{
cout<<"Enter nurse Duty time(d for day/n for night): ";
cin>>time;
}while(time!='n' && time!='d');
setid(nurseid);
setdutytime(time);
}
class treatingphysician{
private:
char* id;
char* phonenumber;
assistantnurse assistant[2]; //3 assistant nurses through composition from assistantnurse class
public:
void setid(char*);
void setphonenumber(char*);
char* getid()const;
char* getphonenumber()const;
void setnurse();
void getnurse()const;
treatingphysician(char[] =" ", char[] =" ", assistantnurse[]);
~treatingphysician();
void print()const;
void inputinfo();
};
void treatingphysician::inputinfo()
{
char i[20];
char p[15];
cout<<"Enter physician ID: ";
cin>>i;
setid(i);
cout<<"Enter physican phone number: ";
cin>>p;
setphonenumber(p);
setnurse();
}
void treatingphysician::print()const
{
cout<<"Physician ID: "<<id<<endl;
cout<<"Physician phone number: "<<phonenumber<<endl<<endl;
getnurse();
}
treatingphysician::~treatingphysician()
{
delete[] id;
id=0;
delete[] phonenumber;
phonenumber=0;
}
treatingphysician::treatingphysician(char*i, char*p, assistantnurse k[2]):assistant(k)
{
setid(i);
setphonenumber(p);
}
void treatingphysician::setid(char*i)
{
int l=strlen(i);
id=new char[l+1];
strncpy(id,i,l);
id[l]='\0';
}
void treatingphysician::setphonenumber(char*p)
{
int l=strlen(p);
phonenumber=new char[l+1];
strncpy(phonenumber,p,l);
phonenumber[l]='\0';
}
char* treatingphysician::getid()const{return id;}
char* treatingphysician::getphonenumber()const{return phonenumber;}
void treatingphysician::setnurse()
{
for(int i=0;i<3;i++)
{
cout<<"Enter info for nurse #"<<i+1<<endl;
assistant[i].inputinfo();
cout<<"\n";
}
}
void treatingphysician::getnurse()const
{
for(int i=0;i<3;i++)
{
cout<<"Nurse #"<<i+1<<" ";
assistant[i].print();
}
}
int main()
{
treatingphysician e;
e.inputinfo();
e.print();
system("pause");
return 0;
}
two classes;
A nurse class and a physician class;
The physician has 3 nurses and all his attributes.
Using composition to fix this, but i'm stuck on the array of objects for the 3 nurses.
In my second constructor i faced a problem i used to work on composition before with the same method but the only difference is that it wasn't an array of objects used in composition. now I'm stuck.. Please Help
Short example...
`class A{
public:
A();
};
class B{
private:
A a[2];
public:
B(A[]);
};
B::B(A c[2]):a(c){}`
Use std::vector instead of raw arrays.
class A
{
public:
A();
};
typedef std::vector< A > A_vector;
class B
{
private:
A_vector m_a;
public:
B( A_vector av ) : m_a( av )
{
}
};