cout related problem (I think)

C

Cyron

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 ) ;
}
 
A

angelo

Cyron said:
void Transaction::setAmount ( char newAmount[] ) {
strncpy( amount, newAmount, 9 ) ;
payee[ 9 ] = '\0' ; // Statement A
}

The problem comes from Statement A here: It should be
amount[9] = 0;
 
I

Ivan Vecerina

Cyron said:
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.
Yes. A classic copy-paste error.
void Transaction::setAmount ( char newAmount[] ) {
Note: the input parameter should be const: char const newAmount[]
strncpy( amount, newAmount, 9 ) ;
payee[ 9 ] = '\0' ; // Statement A

Review the last line once again before reading on...

You probably meant to modify "amount", not "payee".


Note that using fixed-size arrays and magic numbers in multiple
locations (10,9,51,50 array sizes) is not a good idea.
To address the latter, you could use a function such as:
template<unsigned arraySize>
void trunc_copy_string(char const* source, char (&buf)[arraySize])
{
strncpy( buf, source, arraySize-1 );
buf[arraySize-1] = '\0';
}
The setAmount function above can then correctly and safely be
implemented as:
trunc_copy_string( newAmount, amount );


Hope this helps,
Ivan
 
C

Cyron

*slaps forehead*

I guess I was staring at the code way too long. I'm sorry for taking
your time to point out such a simple error. Thank you for your time
and also for the advice on magic numbers and safe array access.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top