[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Program doesn't return right value

Posted on 2003-10-21
6
Medium Priority
?
262 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
5 Comments
 
LVL 2

Accepted Solution

by:
muskad202 earned 500 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 500 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

Hire Technology Freelancers with Gigs

Work with 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

831 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