Solved

Program doesn't return right value

Posted on 2003-10-21
6
254 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now