#pragma once

#include <string>
#include <iostream>
using namespace std ;

class Document
{
public:
    Document ( ) ;
    Document operator= ( const Document & d ) ;

    void setText ( ) ;
    string getText ( ) ;
protected:
    string text ;
} ;

#include "document.h"

Document::Document ( ) : text ( "No text set" )
{ }

Document Document::operator =( const Document & d )
{
    text = d.text ;

    return *this ;
}

void Document::setText ( )
{
    cout << "Body:  " << endl ;
    getline ( cin , text ) ;
}

string Document::getText ( )
{
    return text ;
}

#pragma once

#include "document.h"
#include <string>
#include <iostream>
using namespace std ;

class Email : public Document
{
public:
    Email ( ) ;
    Email ( string s , string r , string t ) ;

    Email operator =( const Email & e ) ;

    void setData ( ) ;
    string getSender ( ) ;
    string getRecipient ( ) ;
    string getTitle ( ) ;
protected:
    string sender ;
    string recipient ;
    string title ;
} ;

#include "email.h"

Email::Email ( ) : Document ( ) , sender ( "None" ) , recipient ( "None" ) , title ( "None" )
{ }

Email::Email ( string s , string r , string t ) : Document ( ) , sender ( s ) , recipient ( r ) , title ( t )
{ }

Email Email::operator =( const Email & e )
{
    Document::operator= ( e ) ;

    sender = e.sender ;
    recipient = e.recipient ;
    title = e.title ;
    
    return *this ;
}

void Email::setData ( )
{
    cout << "From:  " << flush ;
    cin >> sender ;
    cout << "To:  " << flush ;
    cin >> recipient ;
    cout << "Subject:  " << flush ;
    cin >> title ;
    cin.ignore () ;
    Document::setText ( ) ;
}

string Email::getSender ( )
{
    return sender ;
}

string Email::getRecipient ( )
{
    return recipient ;
}

string Email::getTitle ( )
{
    return title ;
}


#pragma once

#include "document.h"
#include <string>
#include <iostream>
using namespace std ;

class File : public Document
{
public:
    File ( ) ;
    File ( string p ) ;

    File operator =( const File & f ) ;

    void setPath ( ) ;
    string getPath ( ) ;
protected:
    string path ;
} ;

#include "file.h"

File::File ( ) : Document ( ) , path ( "No path set" )
{ }

File::File ( string p ) : Document ( ) , path ( p )
{ }

File File::operator =( const File & f )
{
    if ( path == f.path )
        return *this ;
    Document::operator= ( f ) ;
    path = f.path ;

    return *this ;
}

void File::setPath ( )
{
    cout << "Path:  " << flush ;
    cin >> path ;
}

string File::getPath ( )
{
    return path ;
}

#include "document.h"
#include "email.h"
#include "file.h"

int main ( )
{
    Document doc ;
    Email email ;
    File file ;

    cout << "Document\n" ;
    doc.setText ( ) ;
    cout << "\nEmail\n" ;
    email.setData ( ) ;
    cout << "\nFile\n" << endl ;
    file.setPath ( ) ;

    cout << "\nDocument:\n" ;
    cout << doc.getText ( ) ;

    cout << "\nEmail\n" ;
    cout << "To:  \t" << email.getSender ( ) << endl ;
    cout << "From:  \t" << email.getRecipient ( ) << endl ;
    cout << "Subject:  \t" << email.getTitle ( ) << endl ;
    cout << email.getText ( ) << endl ;

    cout << "\n\n-- Done --\n\n" ;

    return 0 ;
}

Sample Output:

Document
Body:
this is a test

Email
From: me
To: you
Subject: this is a test
Body:

File

Path:

BUT, sample output:
Document
Body:
This is a test.

Email
From: me
To: you
Subject: subject
Body:
Another test

File

Path: blah/blah/blah

Document:
This is a test.
Email
To: me
From: you
Subject: subject
Another test


-- Done --

Press any key to continue . . .

Recommended Answers

All 6 Replies

Erm, the program is doing exactly what it is told to do. The error is that it is doing something you would prefer it not to do? Or that it is not doing something you would like it to do?

Whenever you are getting a string from the user at the keyboard, don't use cin >> mystring; Use instead getline( cin, mystring ); As I am not sure what exactly this is supposed to be doing I can't be definite, but this should solve some problems you are having and allow you to get rid of that cin.ignore(); statement.

(BTW. If you say: cin >> text; cin.ignore(); and the user types "This is a test" then only "This" is stored in text and " is a test" is ignore()d.)

Also, the #pragma once is a compiler-specific and problematic solution. You really should use well-named include guards instead.

Hope this helps.

>>same problem
Same problem as what?? I have no idea what you problem is. When starting a new thread do not make it dependent on a previous thread. If there is a problem then you need to describe what the problem is. To keep from repeating yourself you should not have started a new thread but just posted to the original thread so that everyone knows what the hell you are talking about.

>>same problem
Same problem as what?? I have no idea what you problem is. When starting a new thread do not make it dependent on a previous thread. If there is a problem then you need to describe what the problem is. To keep from repeating yourself you should not have started a new thread but just posted to the original thread so that everyone knows what the hell you are talking about.

ok :(

I'm having a problem with getline and cin.ignore(). Prompts keep getting skipped when more than one word is used. In the first example above, it goes straight from Body: to Path:

The key to your problem was described by Duoas so I won't repeat it.

The key to your problem was described by Duoas so I won't repeat it.

so your rant in post #3 was pointless, seeing as my question has been answered and I'm working on fixing my program already... thanks tho! :)

so your rant in post #3 was pointless, seeing as my question has been answered and I'm working on fixing my program already... thanks tho! :)

Not it was not pointless -- it applies to anyone who starts new threads ASSUMING that everyone knows that you are talking about. If you're going to start a new thread then at least give everyone the courtesy of making it a complet thread that does not require us to read some previous thread. Most people will just ignore you when you do not do that.

But, I'm glad for you that you're on you way to solving the problem :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.