HI,
I got this Error in my code i can't understand how to pass a command line argument while the exe of my programe is not created how i write the name of that .exe file.
C:\Program Files\Microsoft Visual Studio\MyProjects\filehandling\file.cpp(205) : error C2451: conditional expression of type 'class std::basic_fstream >' is illegal
Ambiguous user-defined-conversion
#include "iostream"
#include "cstdlib"
#include "cstdio"
#include "ctime"
#include "fstream"
#include "istream"
using namespace std;
class shapes
{
public:
virtual void draw()=0;
virtual void save(fstream &out)=0;
virtual void open(fstream &in)=0;
};
class myline : public shapes
{
private:
int sx,sy,ex,ey,color;
public:
myline()
{}
myline(int x1, int y1, int x2, int y2, int clr)
{
sx=x1;
sy=y1;
ex=x2;
ey=y2;
color=clr;
}
void draw()
{
cout<<"Line-draw()"<<endl;
}
void save(fstream &out)
{
out<<"L"<<"\n";
out<<sx<<""<<sy<<""<<ex<<""<<ey<<""<<color<<"\n";
}
void open(fstream &in)
{
in>>sx>>sy>>ex>>ey>>color;
}
};
class myrectangle: public shapes
{
private:
int sx,sy,ex,ey,color;
public:
myrectangle()
{}
myrectangle(int x1, int y1,int x2, int y2,int clr)
{
sx=x1;
sy=y1;
ex=x2;
ey=y2;
color=clr;
}
void draw()
{
cout<<"Rectangle-draw()"<<endl;
}
void save(fstream &out)
{
out<<"R"<<"\n";
out<<sx<<""<<sy<<""<<ex<<""<<ey<<""<<color<<"\n";
}
void open(fstream &in)
{
in>>sx>>sy>>ex>>ey>>color;
}
};
class mycircle: public shapes
{
private:
int sx, sy, radius, color;
public:
mycircle()
{
}
mycircle(int x1, int y1, int r, int clr)
{
sx=x1;
sy=y1;
radius=r;
color=clr;
}
void draw()
{
cout<<"Circle-draw()"<<endl;
}
void save(fstream &out)
{
out<<"C"<<"\n";
out<<sx<<""<<sy<<""<<radius<<""<<color<<"\n";
}
void open(fstream &in)
{
in>>sx>>sy>>radius>>color;
}
};
struct node
{
void*obj;
node*link;
};
class objarray
{
private:
node*head;
public:
objarray()
{
head= NULL;
}
void add(void*o)
{
node*temp = new node;
temp->obj=o;
temp->link=NULL;
if(head==NULL)
head=temp;
else
{
node*q;
q=head;
while(q->link != NULL)
q=q->link;
q->link=temp;
}
}
void*getobj(int i)
{
node*q;
q=head;
int n;
for (n=1; n<i; n++)
{
q=q->link;
}
return(q->link);
}
int getcount()
{
int n=0;
node*q;
q=head;
while(q != NULL)
{
q=q->link;
n++;
}
return n;
}
~objarray()
{
node *q;
q=head;
while(q != NULL)
{
head = head->link;
delete q;
q=head;
}
}
};
int main(int argc ,char*argv[])
{
fstream file;
char choice;
int clmum,sx,sy,ex,ey,rad;
shapes*ptr;
objarray arr;
char a[2];
int i;
if(argc==2)
file.open(argv[1], ios::in|ios::out);
while(file)
{
file>>a;
if(strcmp(a,"L")==0)
{
myline*l = new myline();
l->open(file);
arr.add(l);
}
if(strcmp(a,"R")==0)
{
myrectangle *a=new myrectangle();
a->open(file);
arr.add(a);
}
if(strcmp(a,"C")==0)
{
mycircle*c=new mycircle();
c->open(file);
arr.add(c);
}
}
int count = arr.getcount();
for(i=1; i<=count; i++)
{
ptr=(shapes*)arr.getobj(i);
ptr->draw();
}
srand((unsigned ) time(NULL));
while(1)
{
cout<<endl<<"1.Line 2. Rectanle 3.Circle 4.Exit"<<endl;
cout<<"Your Choice:";
fflush(stdin);
cin.get(choice);;
clmum=rand()%16;
sx=rand()%638;
sy=rand()%478;
ex=rand()%638;
ey=rand()%478;
rad=rand()%200;
myline*l;
myrectangle*a;
mycircle*c;
switch(choice)
{
case '1':
l = new myline(sx, sy, ex,ey,clmum);
if(l=NULL)
exit(1);
arr.add(l);
cout<<"Following Line added to array"<<endl;
cout<<"sx="<<sx<<"sy="<<sy<<"ex ="<<ex<<"ey ="<<ey<<"color ="<<clmum<<endl;
break;
case '2':
a = new myrectangle(sx,sy,ex,ey,clmum);
if(a==NULL)
exit(1);
arr.add(a);
cout<<"Following Rectangle added to array"<<endl;
cout<<"sx="<<sx<<"sy="<<sy<<"ex ="<<ex<<"ey ="<<ey<<"color ="<<clmum<<endl;
break;
case '3':
c=new mycircle(sx,sy,rad,clmum);
if(c==NULL);
exit(1);
arr.add(c);
cout<<"Following Circle added to array"<<endl;
cout<<"sx="<<sx<<"sy="<<sy<<"rad ="<<rad<<"color"<<clmum<<endl;
break;
case '4':
if(argc==1)
{
cout<<"Enter File name:";
char name[67];
cin>>name;
file.open(name,ios::out);
}
count=arr.getcount();
file.seekp(0L,ios::beg);
file.clear();
for(i=1; i<=count;i++)
{
ptr=(shapes*) arr.getobj(i);
ptr->save(file);
}
file.close();
cout<<"Array save to file......exiting"<<endl;
exit(1);
}
}
return 0;
}
Here's your problem area (at least the area of the problem you've identified):
while(file)
{
file>>a;
What you're getting should be a warning, not an error -- there's one conversion that should be used here. Even though what it's told you is technically wrong, it's still done you a favor by identifying buggy code. The problem is that you're testing whether the read succeeded before you actually do the read. Therefore, when/if the read fails, you'll execute another iteration of your loop before the loop exits.
You want to combine the reading and testing so you'll detect a failed read immediately after it happens. You can do this by replacing both of the lines above with: while (file >> a) {
Your problem is the line 205, while (file). It should be while (!file.eof())
As pointed out by CariElf, your file won't turn to NULL or 0 or something like that during that while loop. On the other hand, the file's "end of file" marker will be true at some point.
About your... well, other question I guess ("i can't understand how to pass a command line argument while the exe of my programe is not created"):
You can tell VS you want to pass some command line arguments by changing "Command Arguments" in Project->Properties, on the Configuration Properties->Debugging tab.
Related
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;
}
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;
}
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
Here is my code for quick sort. I am a beginner kindly please help.
#include<iostream>
using namespace std;
class quick
{
private:
int n,left,right,i,j;
float a[55];
public:
void getdata();
void sort(float[],int,int);
void putdata();
};
void quick::getdata()
{
cout<<"Enter how many elements you want to enter:";
cin>>n;
for(int k=0;k<n;k++)
{
cout<<"Enter percentage of students:"<<k+1<<":";
cin>>a[k];
}
left=0;
right=n-1;
}
void quick::putdata()
{
for(int k=0;k<5;k++)
{
cout<<"\nSorted marks are:"<<a[k]<<endl;
}
}
void quick::sort(float a[],int left,int right)
{
if(left<right)
{
int i=left;
int j=right+1;
float pivot=a[left];
do{
do{
i++;
}while((a[i]<pivot)&& left<right);
do{
j--;
}while(a[j]>pivot);
if(i<j)
swap(a[i],a[j]);
}while(i<j);
a[left]=a[j];
a[j]=pivot;
sort(a,left,j-1);
sort(a,j+1,right);
}
}
int main()
{
quick obj;
obj.getdata();
obj.sort(a[],left,right);
obj.putdata();
return (0);
}
It is giving me error in int main() function:
a is not declared in this scope.
expected primary expression before ']'.
As the answer is given by #Shubham Khatri. Here is the corrected code.
#include<iostream>
using namespace std;
class quick
{
public: int n,left,right,i,j;
float a[55];
public:
void getdata();
void sort(float[],int,int);
void putdata();
};
void quick::getdata()
{
cout<<"Enter how many elements you want to enter:";
cin>>n;
for(int k=0;k<n;k++)
{
cout<<"Enter percentage of students:"<<k+1<<":";
cin>>a[k];
}
left=0;
right=n-1;
}
void quick::putdata()
{
for(int k=0;k<n;k++)
{
cout<<"\nSorted marks are:"<<a[k]<<endl;
}
}
void quick::sort(float a[],int left,int right)
{
if(left<right)
{
int i=left;
int j=right+1;
float pivot=a[left];
do{
do{
i++;
}while((a[i]<pivot)&& left<right);
do{
j--;
}while(a[j]>pivot);
if(i<j)
swap(a[i],a[j]);
}
while(i<j);
a[left]=a[j];
a[j]=pivot;
sort(a,left,j-1);
sort(a,j+1,right);
}
}
int main()
{
quick obj;
obj.getdata();
obj.sort(obj.a,obj.left,obj.right);
obj.putdata();
return (0);
}
As it mentions you have not declared a as a variable inside the int main(). Rather it is an object of quick. In a function you don't pass array like a[] rather as a only .since a, left, right are a private variable of a class you can't access it from the object directly. Declare it as public and use it as obj.a, obj.left, obj.right inside sort function.
Complete code:
#include<iostream>
using namespace std;
class quick
{
public:
int n,left,right,i,j;
float a[55];
void getdata();
void sort(float[],int,int);
void putdata();
};
void quick::getdata()
{
cout<<"Enter how many elements you want to enter:";
cin>>n;
for(int k=0;k<n;k++)
{
cout<<"Enter percentage of students:"<<k+1<<":";
cin>>a[k];
}
left=0;
right=n-1;
}
void quick::putdata()
{
for(int k=0;k<5;k++)
{
cout<<"\nSorted marks are:"<<a[k]<<endl;
}
}
void quick::sort(float a[],int left,int right)
{
if(left<right)
{
int i=left;
int j=right+1;
float pivot=a[left];
do{
do{
i++;
}while((a[i]<pivot)&& left<right);
do{
j--;
}while(a[j]>pivot);
if(i<j)
swap(a[i],a[j]);
}while(i<j);
a[left]=a[j];
a[j]=pivot;
sort(a,left,j-1);
sort(a,j+1,right);
}
}
int main()
{
quick obj;
obj.getdata();
obj.sort(obj.a,obj.left,obj.right);
obj.putdata();
return (0);
}
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;
}
}
}