[ / / / / / / / / / / / / / ] [ dir / random / 93 / biohzrd / hkacade / hkpnd / tct / utd / uy / yebalnia ]

/prog/ - Programming

Programming
Name
Email
Subject
REC
STOP
Comment *
File
Password (Randomized for file and post deletion; you may also set your own.)
Archive
* = required field[▶Show post options & limits]
Confused? See the FAQ.
Options

Allowed file types:jpg, jpeg, gif, png, webp,webm, mp4, mov
Max filesize is16 MB.
Max image dimensions are15000 x15000.
You may upload5 per post.


File: 1451590124908.jpg (46.65 KB,500x670,50:67,starfox.jpg)

18059a No.3787

Lads I have been trying to teach myself c++ and this exercise from my book is driving me crazy!

"Write a program in which you create a Text class that contains a string object to hold the text of a file. Give it two constructors: a default constructor and a constructor that takes a string argument that is the name of the file to open. When the second constructor is used, open the file and read the contents of the file into the string member object. Add a member function contents() to return the string so that you can display it. In main(), open a file using Text and display the contents."

I have a full solution that compiles but does not actually store any information from the inputed file (it prints empty lines or sometimes a single } )

http://coliru.stacked-crooked.com/a/d1ab2c2ad4e1b05d

Help me obi-wan you are our only hope

____________________________
Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

18059a No.3789

Your code is so fucking ugly. It took me a minute to figure out what was wrong, then I laughed and said, "That's what I would expect".

Basically, your block structure is wrong. C++ is not Python: white space doesn't mean shit.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

18059a No.3790

File: 1451603952116.jpg (8.01 KB,197x256,197:256,rad.jpg)

>>3789

Amazingly enough I have no idea what you mean could you elaborate with a specific example?

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

18059a No.3792

>>3790

There is only one place in your code where you could have fucked it up: the while loop.


while(getline(file_2open,holder));
temp += holder + "\n";

This reads every line of the file and then stops at the last line, adding the last line, and only the last line, to temp.

What you want is:


while(getline(file_2open,holder))
temp += holder + "\n";

or, maybe


while(getline(file_2open,holder))
{
temp += holder + "\n";
}

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

18059a No.3793

Fucking Pythonfags.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

18059a No.3795

>>3792

It's a great example of why you should never write it either his way or the way you suggested - it's so easy to accidentally leave a stray line terminator in that changes the logic. A better style is


while(...) {

as it resists these types of bugs (which are actually rather common in real code by good programmers). People get super stubborn about this even though this style is objectively better due to the lowered error rate. It's also a good example of why python got it (mostly) right with its spacing.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

18059a No.3799

>>3795

> this style is subjectively better

> FTFY

If you use Allman style and always use braces, even when there is only one statement, then you don't make these sorts of errors.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

18059a No.3831

The first thing you should do is fix all warnings and then quickly go fuck yourself for not using plain C.

Also write in some if statements to check whether or not you variables actually have any /value/.

guday m8

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

18059a No.3832

>>3795

Or you could format code in a decent way and not be a retard/faggot.

If you are used to writing shit this way you shouldn't have errors. It also makes you look 1337 as fuck.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

18059a No.3856

>>3792

Never Python. Not even once.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.



[Return][Go to top][Catalog][Nerve Center][Random][Post a Reply]
Delete Post [ ]
[]
[ / / / / / / / / / / / / / ] [ dir / random / 93 / biohzrd / hkacade / hkpnd / tct / utd / uy / yebalnia ]