[ home / board list / faq / random / create / bans / search / manage / irc ] [ ]

/prog/ - Programming

Programming board

Catalog

See 8chan's new software in development (discuss) (help out)
Infinity Next update (Jan 4 2016)
Name
Email
Subject
Comment *
File
* = required field[▶ Show post options & limits]
Confused? See the FAQ.
Embed
(replaces files and can be used instead)
Options
Password (For file and post deletion.)

Allowed file types:jpg, jpeg, gif, png, webm, mp4, pdf
Max filesize is 8 MB.
Max image dimensions are 10000 x 10000.
You may upload 1 per post.


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

369296 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

beb83d 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.


369296 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?


beb83d 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";
}


c8ec8f No.3793

Fucking Pythonfags.


cf1f43 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.


beb83d 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.


000000 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


000000 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.


25eb0b No.3856

>>3792

Never Python. Not even once.




[Return][Go to top][Catalog][Post a Reply]
Delete Post [ ]
[]
[ home / board list / faq / random / create / bans / search / manage / irc ] [ ]