LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-02-2012, 05:45 PM   #1
RollingKeg
LQ Newbie
 
Registered: Nov 2012
Posts: 2

Rep: Reputation: Disabled
Unhappy Reading character by character to dynamically allocated array.


Hi,

I'm having some problems figuring out why it's not reading in the whiteline space. Here's my code: http://pastebin.com/EezBY8GK

The output is suppose to be like this:

BUMP! And then Something went BUMP! How that bump made up jump!
We looked! Then we saw him step in on the mat! We looked! And
we saw him! The Cat in the Hat! And he said to us,
"Why do you sit there like that?" "I know it is wet and the sun
is not sunny. But we can have Lots of good fun that is funny!"

but upon running the program I'm left with this.
BUMP!AndthenSomethingwentBUMP!Howthatbumpmadeupjump!Welooked!Thenwesawhimstepinonthemat!Welooked!And wesawhim!TheCatintheHat!Andhesaidtous,"Whydoyousittherelikethat?""Iknowitiswetandthesunisnotsunny.Bu twecanhaveLotsofgoodfunthatisfunny!" 305

ignore the 305, it was just for me to see how much memory is being dynamically allocated.
 
Old 11-04-2012, 09:27 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Why are you allocating an array of 0 bytes??
Code:
    int dataCounter = 0;
    ...
    holder = new char [dataCounter];
Also, as you can observe above, it is indeed possible to post code on LQ. Please don't use a third-party site.
 
Old 11-07-2012, 06:54 PM   #3
RollingKeg
LQ Newbie
 
Registered: Nov 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Originally when I declared int dataCounter and initilized it to 0 bytes I thought it would be good practice. I think this habit of practice came from learning C, but since then I've solved the space issue with getline. Thanks for the help though.
 
Old 11-07-2012, 07:44 PM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by RollingKeg View Post
Originally when I declared int dataCounter and initilized it to 0 bytes I thought it would be good practice.
It is good practice, whether C or C++.

The problem was that you were allocating the array prematurely without yet have the real value for dataCounter.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
local dynamically allocated array in fortran otoomet Programming 5 10-21-2009 08:28 AM
How to send the dynamically allocated two dimensional character array through C++ UDP kbarani Linux - Software 3 05-04-2009 11:41 PM
How to store text(strings) in a 2D character array reading from a text file(C++) bewidankit Programming 3 02-14-2008 07:08 AM
In C, using qsort for dynamically allocated array ntmsz Programming 7 08-23-2005 10:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration