Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Program doesn't return right value

Posted on 2003-10-21
6
Medium Priority
?
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

636 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