Solved

Program doesn't return right value

Posted on 2003-10-21
6
256 Views
Last Modified: 2010-04-01
This code will only return a 'C' for author's name.  How do I get the length to display any size name?
#include <iostream.h>
#include <conio.h>
#include <string.h>
       
class Book;    // forward reference
//--------------------------------------------------------------------    
class Author
{
      friend void displayInfo(Author auth, Book trans);
      private:
            char title;
            char authorname;
            double price;
      public:
            Author(char chr = ' ', const char chr2 = ' ', const double balance = 0.0);
};
Author::Author(char chr, const char chr2, const double balance)
{
//      strcpy(title, chr);
      title = chr;
      authorname = chr2;
      price = balance;
}
//--------------------------------------------------------------------
class Book
{
      friend void displayInfo(Author auth, Book trans);
private:
      char name;
      char address;
      double royalty;
public:
      Book(const char chr3 = ' ', const char chr4 = ' ' , const double amt = 0.0);
};
Book::Book(const char chr3, const char chr4, const double amt)
{
      name = chr3;
      address = chr4;
      royalty = amt;
}
//--------------------------------------------------------------------
void displayInfo(Author auth, Book trans)
{
      cout<<"Book title: "<<auth.title<<endl;
      cout<<"Author:     "<<auth.authorname<<endl;
      cout<<"Price:      "<<auth.price<<endl;
      cout<<"Name:       "<<trans.name<<endl;
      cout<<"Address:    "<<trans.address<<endl;
      cout<<"Royalty:    "<<trans.royalty<<"%"<<endl;
}
//--------------------------------------------------------------------
void main()
{
      Author onePatron('C', 'L', 19.99);
         Book onePayment('N', 'A', 50.00);
      displayInfo(onePatron, onePayment);

      getch();
}
      

Thank you.
0
Comment
Question by:redice
6 Comments
 
LVL 2

Accepted Solution

by:
muskad202 earned 125 total points
ID: 9590419
#include <iostream.h>
#include <conio.h>
#include <string.h>
       
class Book;    // forward reference
//--------------------------------------------------------------------    
class Author
{
     friend void displayInfo(Author auth, Book trans);
     private:
          char *title;
          char *authorname;
          double price;
     public:
          Author(char *chr = "", char *chr2 = "", const double balance = 0.0);
};
Author::Author(char *chr, char *chr2, const double balance)
{
     //strcpy(title, chr);
     title = chr;
     authorname = chr2;
     price = balance;
}
//--------------------------------------------------------------------
class Book
{
     friend void displayInfo(Author auth, Book trans);
private:
     char *name;
     char *address;
     double royalty;
public:
     Book(char* chr3 = "", char* chr4 = "" , const double amt = 0.0);
};
Book::Book( char *chr3, char *chr4, const double amt)
{
     name = chr3;
     address = chr4;
     royalty = amt;
}
//--------------------------------------------------------------------
void displayInfo(Author auth, Book trans)
{
     cout<<"Book title: "<<auth.title<<endl;
     cout<<"Author:     "<<auth.authorname<<endl;
     cout<<"Price:      "<<auth.price<<endl;
     cout<<"Name:       "<<trans.name<<endl;
     cout<<"Address:    "<<trans.address<<endl;
     cout<<"Royalty:    "<<trans.royalty<<"%"<<endl;
}
//--------------------------------------------------------------------
void main()
{
     Author onePatron("TheTitle", "TheAuthor", 19.99);
        Book onePayment("TheName", "TheAddress", 50.00);
     displayInfo(onePatron, onePayment);

     getch();
}
0
 
LVL 2

Assisted Solution

by:carribus
carribus earned 125 total points
ID: 9590430
The code will only display a 'C' for the Author name, because that's all you put in. You might want to revisit your class declaration. Seeing as you're using C++, I'm going use the standard C++ library:

---------------<SNIP>----------------

#include <iostream>
#include <conio.h>
#include <string>        // stdcpp include to include the std::string class in your code

using namespace std;  // using the standard cpp lib namespace

class Book;    // forward reference
//--------------------------------------------------------------------    
class Author
{
     friend void displayInfo(Author auth, Book trans);
     private:
          string title;
          string authorname;
          double price;
     public:
          Author(string _title = "", string _name = "", const double balance = 0.0);
};
Author::Author(string _title, string _name, const double balance)
{
//     strcpy(title, chr);
     title = _title;
     authorname = _name;
     price = balance;
}
//--------------------------------------------------------------------
class Book
{
     friend void displayInfo(Author auth, Book trans);
private:
     string name;
     string address;
     double royalty;
public:
     Book(string _name = "", string _address = "" , const double amt = 0.0);
};
Book::Book(string _name, string _address, const double amt)
{
     name = _name;
     address = _address;
     royalty = amt;
}
//--------------------------------------------------------------------
void displayInfo(Author auth, Book trans)
{
     cout<<"Book title: "<<auth.title<<endl;
     cout<<"Author:     "<<auth.authorname<<endl;
     cout<<"Price:      "<<auth.price<<endl;
     cout<<"Name:       "<<trans.name<<endl;
     cout<<"Address:    "<<trans.address<<endl;
     cout<<"Royalty:    "<<trans.royalty<<"%"<<endl;
}
//--------------------------------------------------------------------
void main()
{
     Author onePatron("Author Title", "Author Name", 19.99);
        Book onePayment("Book Name", "Book Address", 50.00);
     displayInfo(onePatron, onePayment);

     getch();
}

---------------<SNIP>----------------

Does this answer your question?
0
 

Author Comment

by:redice
ID: 9590455
Yes,  Thanks for all of your help.  Have a great day.
0
 
LVL 2

Expert Comment

by:carribus
ID: 9590489
Sure thing :)
0
 
LVL 11

Expert Comment

by:bcladd
ID: 10481834
sNo comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered: Points split between muskad202 and carribus

Please leave any comments here within the next four days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ADO Memory leak with DELPHI 2007 37 181
C++ Language error 28 214
Embarcadero C++ Builder XE2 TDateTime 8 70
Eclipse IDE - Cannot copy/paste from console output 8 185
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question