I want to write a program Using manipulators in C/C++ and using Overloading stream insertion and extraction operators.

i'm learning c++,and i'm attempting to do the following problem:
plxx some1 solv it......




Write a program that takes customer’s information from user i.e. name, ID and spending and displays the same customer information plus total bill on the screen. A customer will be an object of a class. Each object has a name, customer ID and amount spent on shopping, as its data members. You need to overload stream insertion << and stream extraction >> operators for the Customer class.

Detailed Description:
 You are required to create a class named Customer.
 It will have name, customer-ID and spending as its private data members.
 You have to make two constructors for this class, i.e. default constructor and parameterized constructor.
 Default constructor will initialize the name with “No name”, customer-id with “0” and spending with “0”;
 Parameterized constructor will take three arguments i.e. name, customer-id and spending.
 Create getter and setter functions for each data member of the class.
 This time you don’t need the friend function for calculation of total bill according to spending.
 You can modify the class (add or delete some functions) as per program requirement.
 You will overload extraction operator >> for getting input for an object of Customer class.
 You will also overload insertion operator << for displaying the customer information on screen.
 On calling extraction operator in main() e.g. cin>>obj where obj is an object of Customer class, information about customer name, ID and spending should be taken from the user.
 Take a look at the sample output for better understanding.











 On calling insertion operator in main() e.g. cout<<obj, the output in the following format should be displayed.





 You must use the formatting manipulators setw() and setfill() for formatting the text as shown in the output.
 All the formatting of text should be implemented inside overloaded function for extraction operator <<
 The main() function of your solution should contain only these instructions

Customer obj;
cin >> obj;
cout << obj;
system(“pause”);
i'm learning c++,and i'm attempting to do the following problem:

And where is your attempt? At what point of the assignment do you have problems?
http://www.cplusplus.com/doc/tutorial/classes2/

Overloading an operator is similar to to writing a normal function. Might help to actually read up on it.
Hi myda,

If you don't have code, then try this:

Your detailed description forms a design, so try copying in into a blank .cpp file as comments. Then go through & write code that does what is written in the comments.

The order of the code won't exactly follow the comments, but at least it will give you a start.

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class Customer
{
private:
string Name;
int CustomerId;
int Spending;
int tax;
int discount;
int totalbill;
public:
void setName(string);
int setCustomerId(int);
int setSpending(int);
int getName(string);
int getCustomerId(int);
int getSpending(int);
void display()
{
cout<<"Customer Name" : "Name"ID : "CustomerId"Spending : "Spending"<<endl;
}
Customer();
Customer(string, int, int);
friend ostream & operator<<(ostream &, Customer&);
friend istream & operator>>(istream &, Customer&);
friend ostream &star(ostream &);
};
ostream & star(ostream & output)
{
output"*";
return output;
}
Customer::Customer()
{
Name="No Name";
CustomerId=0;
Spending=0;
}
Customer::Customer(string s, int i, int j)
{
Name=s;
CustomerId=i;
Spending=j;
}
istream & operator>>(istream & input, Customer &c)
{
cout"Enter the name of customer : ";
input>>c.Name;
cout"Enter the customer-id : ";
input>>c.CustomerId;
cout"Enter the Spending : ";
input>>c.Spending;
return input;
}
ostream & operator<<(ostream & output, Customer &c)
{
for(int i=0;i<55;i++)
{
if(i==25)
{
output"Customer Bill Information";
}
else
{
outputstar;
}
}
outputendl;
output"Name :";
for(int a=0;a<21;a++)
{
if(a==20)
{
outputc.Nameendl;
}

else
{
outputstar;
}
}
output"ID :";
for(int a=0;a<23;a++)
{
if(a==22)
{
outputc.CustomerIdendl;
}

else
{
outputstar;
}
}
output"Spending :";
for(int a=0;a<19;a++)
{
if(a==18)
{
outputc.Spendingendl;
}

else
{
outputstar;
}
}

if(c.Spending<=5000)
{
c.tax=(c.Spending*5)/100;
c.discount=(c.Spending*1)/100;
c.totalbill=c.Spending+c.tax-c.discount;
output"Tax :";
for(int a=0;a<27;a++)
{
if(a==26)
{
outputc.taxendl"Discount :";
for(int a=0;a<19;a++)
{
if(a==18)
{
outputc.discountendl"Total Bill :";
for(int a=0;a<17;a++)
{
if(a==16)
{
outputc.totalbillendl;
}
else
{
outputstar;
}
}

}
else
{
outputstar;
}
}
}

else
{
outputstar;
}
}

}
else if(c.Spending>5000 & c.Spending<10000)
{
c.tax=(c.Spending*10)/100;
c.discount=(c.Spending*2)/100;
c.totalbill=c.Spending+c.tax-c.discount;
output"Tax :";
for(int a=0;a<27;a++)
{
if(a==26)
{
outputc.taxendl"Discount :";
for(int a=0;a<19;a++)
{
if(a==18)
{
outputc.discountendl"Total Bill :";
for(int a=0;a<17;a++)
{
if(a==16)
{
outputc.totalbillendl;
}

else
{
outputstar;
}
}

}

else
{
outputstar;
}
}
}

else
{
outputstar;
}
}

}
else if(c.Spending>10000)
{
c.tax=(c.Spending*15)/100;
c.discount=(c.Spending*3)/100;
c.totalbill=c.Spending+c.tax-c.discount;
output"Tax :";
for(int a=0;a<27;a++)
{
if(a==26)
{
outputc.taxendl"Discount :";
for(int a=0;a<19;a++)
{
if(a==18)
{
outputc.discountendl"Total Bill :";
for(int a=0;a<17;a++)
{
if(a==16)
{
outputc.totalbillendl;
}
else
{
outputstar;
}
}
}

else
{
outputstar;
}
}

}

else
{
outputstar;
}
}

}
return output;
}
main()
{
Customer obj;
cin>>obj;
coutobj;
system("pause");
}

i have codes bt with error if somebody can correct this.....??
i have codes bt with error if somebody can correct this.....??

Are you just kidding? You've lots of "easy to fix" syntax errors in your code. This kind of errors are simple to solve, even for a newbie.
We can help you with more difficult/semantic problems...
Last edited on
try to ident and tag your code, so this help us to help you (y).

and start with main () @ line 4
coutobj;
to
cout << obj;

like the problem said.
Like engelsbr said you have forgotten the output operator << at many places. The other "error" is outputstar. You have declared star as an own function which takes an output stream as parameter. So you have to call star(output) instead of outputstar.
After cleaning up this errors your program works well on my machine.
myda,

If you have lots of compile errors you don't understand, then post your compiler output, we can explain / point out stuff about these errors.

If you do this, the line numbers from the compiler won't match the line numbers in your code (when you post it in tags that is), so can you let us know how they match up.

To use code tags, use the <> button on the right under format, and it should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class Customer
{
private:
string Name;
int CustomerId;
int Spending;
int tax;
int discount;
int totalbill;
i have done it...thank you all for your help......
Topic archived. No new replies allowed.