Hello All,
I am confused with some of the results I am getting from my C++
program. I want the program to display "This is a test" but instead
"This is a" is displayed. From experimentation I've found that by
commenting out Statement A or Statement B, I obtain the results I want
-- but I don't understand why it doesn't work the way I have it setup
now. I can see the strncpy() function is working as I expected but
somehow something is going wrong.
Thanks for your help in advance,
Mike
#include <iostream.h>
#include <conio.h>
class Transaction {
private:
char payee[51] ;
char amount[10] ;
public:
Transaction() ;
char* getPayee() ;
char* getAmount() ;
void setPayee ( char newPayee[] ) ;
void setAmount ( char newAmount[] ) ;
} ;
Transaction::Transaction() {
payee[0] = NULL ;
amount[0] = NULL ;
}
char* Transaction::getPayee() { return( payee ) ; }
char* Transaction::getAmount() { return( amount ) ; }
void Transaction::setPayee ( char newPayee[] ) {
cout << "strncpy returns : " << strncpy( payee, newPayee,
50 ) ;
payee[ 50 ] = '\0' ;
}
void Transaction::setAmount ( char newAmount[] ) {
strncpy( amount, newAmount, 9 ) ;
payee[ 9 ] = '\0' ; // Statement A
}
void displayTransaction( Transaction &object ) {
cout << "\nPayee : " << object.getPayee() ;
}
int main() {
Transaction myTransaction ;
myTransaction.setPayee( "This is a test" ) ;
myTransaction.setAmount( "1" ) ; // Statement B
displayTransaction( myTransaction ) ;
return( 0 ) ;
}