?
Solved

Program doesn't return right value

Posted on 2003-10-21
6
Medium Priority
?
260 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 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…

765 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