Thread: New operator

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    1

    New operator

    Hello,
    I have writen a program in C++.The program takes a list of books using a constructor.It has a switch statement in the main function in which there are following options:

    1] search for a book and purchase it if the book and the required number of copies are available.
    2]Change the price of the book.
    3]Check the number of successful and unsuccessful transactions.

    [Succesful transactions are when the customer buys a book and an unsucessul transaction when either the customer rejects the books or the required number of copies are not available.]

    I have a problem in the above program.When I use a new operator to allocate memory for the title,author and the publisher of the book,the program does not work.However when I modify the the same program and use single dimensional array for the title,author and the publisher of the book the program works perfectly
    Will you please tell me what is the problem.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Will you please tell me what is the problem.
    Code:
    length=strlen(title1);
    title=new char[length+1];
    strcpy(title,title1);
    
    length=strlen(author1);
    title=new char[length+1];
    strcpy(author,author1); // author points to nothing predictable
    
    length=strlen(publisher1);
    title=new char[length+1];
    strcpy(publisher,publisher1); // publisher points to nothing predictable
    When you copy/paste code, be sure to make necessary changes.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  4. Destructor - Execution problem
    By triste in forum C++ Programming
    Replies: 16
    Last Post: 09-26-2004, 01:57 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM