473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

cout related problem (I think)

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

Aug 28 '05 #1
3 1015
"Cyron" <md*******@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Aug 28 '05 #2
Cyron wrote:
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;
Aug 28 '05 #3
*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

Aug 29 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: uli | last post by:
Hi all! I'm posting to both newsgroups, because it's actually a C++ problem but could be that some of you using Matlab-&-MEX-&-C++ was struggling with the same problem. I'm trying to rewrite...
9
by: Ingo Nolden | last post by:
Hi there, I am writing c++ for some months now. I think I know the language now, but not yet all the tricky things of stl. For a kernel which is not using mfc I am writing a serialization. For...
6
by: Omid | last post by:
Hi. I have problems when I try to redirect everything that is sent to cout to a file. I have one piece of code that works and one that does not work. The only difference is which headers I use....
2
by: Generic Usenet Account | last post by:
What exactly is the difference between the hex manipulator and the following statement: cout.setf(ios_base::hex)? According to Stroustrup, Third Edition, Section 21.4.4, "once set, a base is...
1
by: forums_mp | last post by:
Come to think of it I have another question: With respect to priority task1 is the highest, task2 is the lowest. The snippet: SEM_ID task1_sema = semBCreate(SEM_Q_FIFO, SEM_EMPTY); SEM_ID...
3
by: SneakyElf | last post by:
i am very green with c++ so i get stuck on very simple things anyway, i need to write a program that would read data from file (containing names of tv shows and their networks) one line at a time...
3
by: Ramon F Herrera | last post by:
Newbie alert: I come from C programming, so I still have that frame of mind, but I am trying to "Think in C++". In C this problem would be solved using unions. Hello: Please consider the...
58
by: Mark Casternoff | last post by:
I'm getting back into C++ after a long hiatus (they didn't have namespaces back then). I know this question is completely subjective, but I'd be interested in hearing which is the "better"...
2
by: sanjay | last post by:
Hi All, I have a doubt in understanding the output of the following program that i executed on my system. I was using DevC++ IDE which uses minGW based compiler. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.