9 Replies - 428 Views - Last Post: 14 June 2012 - 05:41 PM Rate Topic: -----

#1 AsadBhatti  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-April 12

Coding error and File Opens then in a blink Closes

Posted 14 June 2012 - 05:47 AM

Hello Guys Please guide me with this...

Following is my Assignment

Problem Statement: Calculating customer’s bill using classes

Quote

You are required to write a program for calculating the bill of a customer according to the amount spent on shopping. 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 will calculate the total bill by adding tax in the spending and subtracting discount offered according to spending.

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.
 A friend function will calculate the total bill for each object of class Customer, passed to it as argument.
 If total spending is less than 5000, then tax added on total spending is 5% and discount is 1%. If spending is more than 5000 and less than 10,000 then 10% tax and discount is 2% and if spending is more than 10,000 then 15% tax and 3% discount is applicable.
 Remember that all this calculation will be done inside friend function.
 In the main() function, create three customer type objects by using new operator. Initialize first object with default constructor, second with parameterized and take input from user for third object (use setter functions to set third object’s values taken from the user).
 Calculate total bill for all three objects by passing them to friend function and then display the result of calculation as shown in example output.


This is the output

Posted Image
Posted Image
Posted Image


I tried to write two codes. first it was not compiling then i after compiling when i open .exe file it opens then in a blink of an eye it closes. can you please guide me?

Both are giving unknown errors



    #include <iostream.h>
    #include <stdlib.h>
    class Customer
    {
    public:
    void display();
    void setName(int i);
    void setCustomer_id(int i);
    void setSpending(int i);
    int getName();
    int getCustomer_id();
    int getSpending();
    Customer();
    Customer(int, int , int);
    ~Customer ();
    private:
    int FnlBill;
    int customer_id;
    int spending;
    int name;
    friend float dis1FnlBill(Customer );
    friend float dis2FnlBill(Customer );
    friend float dis3FnlBill(Customer );
    };
    Customer::Customer()
    {
    name= 0;
    customer_id = 0;
    spending = 0;
    }

    Customer::Customer(int thename, int thecustomer_id , int thespending)
    {
    name = thename;
    customer_id = thecustomer_id;
    spending = thespending;

    }
    Customer::~Customer()
    {
    }
    void Customer::display()
    {
    cout getName() getCustomer_id() getSpending();
    }
    void Customer::setName(int i)
    {
    name = i ;
    }
    void Customer::setCustomer_id(int i)
    {
    customer_id = i;
    }
    void Customer::setSpending(int i)
    {
    spending = i;
    }

    int Customer::getName()
    {
    return name;
    }
    int Customer::getCustomer_id()
    {
    return customer_id;
    }
    int Customer::getSpending()
    {
    return spending;
    }
    void FnlBill(Customer *d, int )
    {
    // d->FnlBill ; // Modify private data
    }

    int main()
    {

    Customer Bill1, Bill2(1,2,3), Bill3(4,5,6); // taking objects of Date class
    Bill1.display();
    Customer *aptr;
    aptr = new Customer [1];
    delete [] aptr;
    Bill2.display();
    Customer *bptr;
    bptr = new Customer [1];
    delete [] bptr;
    int name;
    int customer_id;
    int spending;
    cout<<"Enter Customer Name"<<endl;
    cin >> name ;
    cout<<"Enter Customer customer_id"<<endl;
    cin >> customer_id ;
    cout<<"Enter Customer spendings"<<endl;
    cin >> spending ;
    Bill3.display();
    Customer *cptr;
    cptr = new Customer [1];
    delete [] cptr;
    /*float spend,;
    cout "Please enter total spendings " ;
    endl;
    cin >> spend;
    if ( spend < 5000 )
    {
    cout "The Bill is : " dis1FnlBill(Bill3(4,5,6)) endl;
    }
    else if (spend => 5000 && < 10000)
    {
    cout "The Bill is : " dis2FnlBill(Bill3(4,5,6)) endl;
    }
    else if ( spend => 10000 )
    {
    cout "The Bill is : " dis3FnlBill(Bill3(4,5,6)) endl;
    }
    else
    {
    cout "Pleae epend some thing" endl;
    }
    Customer *cptr; // Declared a pointer dptr to MyPointer class object
    cptr = new Customer [1]; // Created 10 objects of MyDate and assigned the
    // pointer to the first object to dptr pointer variable.
    delete [] cptr; // Deleted (freed) the assigned memory to the objects
    //display();*/
    system ("pause");
    }
    /*
    float dis1FnlBill ( Bill3(4,5,6))
    {
    return ( (spend+(5/spend*100))-(1/spend*100)); // Addition of tax
    }
    float dis2FnlBill ( Bill3(4,5,6))
    {
    return ( (spend+(10/spend*100))-(2/spend*100)); // Addition of tax
    }
    float dis3FnlBill ( Bill3(4,5,6))
    {
    return ( (spend+(15/spend*100))-(3/spend*100)); // Addition of tax
    }
    */ 






#include <iostream.h>
#include<conio.h>
#include <stdlib.h>
using namespace std;
class Customer{
string name;
int customerId;
int spending;
float totalBill;
public:
void display();
void setName(string name);
void setCustomerId(int i);
void setSpending(int i);
string getName();
int getCustomerId();
int getSpending();
float getTotalBill();
friend float tBill( Customer *);
Customer();
Customer(string, int , int);
~Customer ();
};
Customer::Customer()
{
name= "Noname";
customerId = 0;
spending = 0;
}
Customer::Customer(string thename, int thecustomerId , int thespending){
name = thename;
customerId = thecustomerId;
spending = thespending;
}
Customer::~Customer()
{
}
void Customer::display()
{
cout<<"***Customer Name ***: "<<getName()<<endl;
cout<<"***ID ***: "<<getCustomerId()<<endl;
cout<<"***Spending*** : "<<getSpending()<<endl;
cout<<"***Total Bill*** : "<<getTotalBill();
}
void Customer::setName(string n)
{
name = n ;
}
void Customer::setCustomerId(int i)
{
customerId = a;
}
void Customer::setSpending(int i)
{
spending = a;
}
string Customer::getName()
{
return name;
}
int Customer::getCustomerId()
{
return customerId;
}
int Customer::getSpending()
{
return spending;
}
float Customer::getTotalBill()
{
return totalBill;
}
main()
{
cout<<"\n\n\*****************************\n\n***** ************************\n\n\n";
Customer *c1 = new Customer();
Customer *c2 = new Customer();
Customer *c3 = new Customer;
cout<<"***Bill Detail for customer 1, Default constructor...!!!***"<<endl<<endl;
tBill(c1);
c1->display();
cout<<endl<<endl<<"***Bill Detail for customer , Parameterized constructor...!!!***"<<endl<<endl;
tBill(c2);
c2->display();
cout<<endl<<endl<<"***Bill Detail for customer 3, Using Setters...!!!***"<<endl<<endl;
string name;
int id, spend;
cout<<"***Enter name of customer 3 ***:";
cin>>name;
cout<<"***Enter ID of customer 3 ***:";
cin>>id;
cout<<"***Enter spending of customer 3*** :";
cin>>spend;
cout<<endl;
c3->setName(name);
c3->setCustomerId(id);
c3->setSpending(spend);
tBill(c3);
c3->display();
getche();
}
float tBill( Customer *c)
{
}





Thank you in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Coding error and File Opens then in a blink Closes

#2 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 195
  • View blog
  • Posts: 544
  • Joined: 04-August 09

Re: Coding error and File Opens then in a blink Closes

Posted 14 June 2012 - 06:19 AM

You may find this link helpful.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon

Reputation: 3051
  • View blog
  • Posts: 9,291
  • Joined: 25-December 09

Re: Coding error and File Opens then in a blink Closes

Posted 14 June 2012 - 06:20 AM

First find an indentation style you like and use it consistently. Very few people will want to read through your gibberish to try to find your errors.

If you are getting compile errors post the complete error message, exactly as it appears in your development environment.


Jim

This post has been edited by jimblumberg: 14 June 2012 - 06:21 AM

Was This Post Helpful? 0
  • +
  • -

#4 AsadBhatti  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-April 12

Re: Coding error and File Opens then in a blink Closes

Posted 14 June 2012 - 08:32 AM

View Postjimblumberg, on 14 June 2012 - 06:20 AM, said:

First find an indentation style you like and use it consistently. Very few people will want to read through your gibberish to try to find your errors.

If you are getting compile errors post the complete error message, exactly as it appears in your development environment.


Jim



No disrespect to you sir, if i knew my errors then why would i ask for help. all of you here are experts that is why i cam here so that you check the code, you can do this with in less than a minute, and can tell me the problem, any ways thank you for your reply
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon

Reputation: 3051
  • View blog
  • Posts: 9,291
  • Joined: 25-December 09

Re: Coding error and File Opens then in a blink Closes

Posted 14 June 2012 - 08:51 AM

If you want people to help then you must provide code that can be read easily. Your code is horrible, it is almost impossible to read as presented. You need to properly format your code so anyone can easily follow your logic and program flow. Proper indentation is required if you ever plan to be a programmer.

Quote

Both are giving unknown errors

What kind of errors? Compile errors? If so post the complete error messages exactly as they appear in your development environment. Otherwise state the exact errors being encountered. This is your assignment and you need to tell us what is wrong.

But until you properly format your code I will not even try to find any errors, so post a properly formatted program.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5670
  • View blog
  • Posts: 22,519
  • Joined: 23-August 08

Re: Coding error and File Opens then in a blink Closes

Posted 14 June 2012 - 09:32 AM

Not to mention you're using the fossil Turbo C++ compiler to compile your code, which no one here (in their right mind) will use.
Was This Post Helpful? 0
  • +
  • -

#7 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 269
  • View blog
  • Posts: 3,827
  • Joined: 08-January 12

Re: Coding error and File Opens then in a blink Closes

Posted 14 June 2012 - 09:37 AM

Well, I would use TC++, because that is used in my school. But you can use system("pause") in TC++ ?? I never knew that. I though all system calls were useless in TC++. But yeah, you are right.
Was This Post Helpful? 0
  • +
  • -

#8 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 719
  • View blog
  • Posts: 1,978
  • Joined: 28-March 11

Re: Coding error and File Opens then in a blink Closes

Posted 14 June 2012 - 03:50 PM

View PostAsadBhatti, on 14 June 2012 - 11:32 AM, said:

No disrespect to you sir, if i knew my errors then why would i ask for help. all of you here are experts that is why i cam here so that you check the code, you can do this with in less than a minute, and can tell me the problem, any ways thank you for your reply

Yes many folks here are experts in their language. That does not mean they are your code checking/writing service.

We all do this for free in our spare time. If you are going to post code for others to look at, you must use indentation in your code otherwise 98% of people will not even look at it.

You don't know the errors? Your compiler should print the error type and line number of the problem code.

See what you did in your post here? You posted the errors your compiler gave you. Part of being a programmer is being a good troubleshooter. Look at the error message the compiler gives you, now look at the line it is telling you the problem is on. Go to that line and see if you can fix that error, maybe it was just a typo. If you can't fix the errors, then in order for others to help you, you must post the errors.
Was This Post Helpful? 0
  • +
  • -

#9 Switters  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 66
  • Joined: 03-June 12

Re: Coding error and File Opens then in a blink Closes

Posted 14 June 2012 - 03:51 PM

View PostAsadBhatti, on 14 June 2012 - 08:32 AM, said:

View Postjimblumberg, on 14 June 2012 - 06:20 AM, said:

First find an indentation style you like and use it consistently. Very few people will want to read through your gibberish to try to find your errors.

If you are getting compile errors post the complete error message, exactly as it appears in your development environment.


Jim



No disrespect to you sir, if i knew my errors then why would i ask for help. all of you here are experts that is why i cam here so that you check the code, you can do this with in less than a minute, and can tell me the problem, any ways thank you for your reply


He told you one of your errors. Your code is not indented consistently. Oftentimes the solution to the problem (an unresolved bracket or a stray semi-colon) presents itself when you correct this issue. If I wrote out my entire post without any spaces or punctuation you would probably still be able to read it. Indentation is not purely a nice formatting thing to do if you have extra time before your assignment is due. Implementing with discipline helps you get your work done.
Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 979
  • View blog
  • Posts: 3,397
  • Joined: 19-February 09

Re: Coding error and File Opens then in a blink Closes

Posted 14 June 2012 - 05:41 PM

Hi, this is a less than obvious problem - unless you've encountered it.

Using cin >> leaves a newline character in the stream.

093	cin>>spend;




The getche() function reads the newline character and doesn't pause.

100	getche();




One way to remove the character is using cin.ignore().

  // instead of getche()
  cin.ignore();
  cin.get();


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1