[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Trying to learn C++ by making a text based adventure game. I'm
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 56
Thread images: 8
File: jfc.png (68 KB, 795x744) Image search: [Google]
jfc.png
68 KB, 795x744
Trying to learn C++ by making a text based adventure game. I'm currently trying to store room descriptions into a separate .txt file and then load them as an array variable when the user is in that room.

I keep getting "Segmentation Fault (core dumped)" and have figured out that it is because I am placing "res:" in the current location. How do I make this work?
>>
that code just gave me cancer


holy nigger shits, if you actually did that OP please fucking kill yourself
>>
>>53525163
that code compiles???
>>
>>53525256
Yes it compiled, I'm learning and new to it so don't expect it to look good, or be non cancerous.
>>
>>53525297
are you using "res" for goto?

if you have freed the file (closed the file) then if you jump back to res, it won't be there (because you closed it). when you access memory you don't possess, it usually results in a segfault
>>
>>53525227
dont be a fucking dick. They're learning, jesus.
>>
>>53525318
I'm trying to only free it when i = 700
I'm pretty sure that's what it says as well
>>
You might as well write it in assembly at this point..
>>
>>53525163
int invSize = 10;
string inv[invSize];
for (int i = 0; i < invSize; i++) {
inv[i] = "blank";
}


Set inventory size once and automatically populate array with blanks
>>
>>53525163
Also look at classes, you might want to get the hang of classes for this.

Class for room, enemy (base class), enemySpecific subclass extending/implementing enemy, item base class and then weapon, armour, potion sub classes etc.

Nvm i wrote all this thinking more rpg but leaving it here anyway, #yolo.
>>
File: 1447762249532.jpg (251 KB, 852x1136) Image search: [Google]
1447762249532.jpg
251 KB, 852x1136
>>53525163


Well, my terminal's locked up, and I ain't got any Mail,
And I can't recall the last time that my program didn't fail;
I've got stacks in my structs, I've got arrays in my queues,
I've got the : Segmentation violation -- Core dumped blues.

If you think that it's nice that you get what you C,
Then go : illogical statement with your whole family,
'Cause the Supreme Court ain't the only place with : Bus error views.
I've got the : Segmentation violation -- Core dumped blues.

On a PDP-11, life should be a breeze,
But with VAXen in the house even magnetic tapes would freeze.
Now you might think that unlike VAXen I'd know who I abuse,
I've got the : Segmentation violation -- Core dumped blues.
>>
>>53525677
Actually man that might look faster cos its shorter but writing the code without automation allows it to be done before compilation and will be faster in the long term, also easier to read.
>>
>>53525163
>that code

jesus H. fuck stop right now and never code again.
>>
>>53525766
clean maintainable code is superior to a tiny amount of speed up, and that code loooks very easy to read
>>
>>53525766
unrolling is funrolling
>>
>>53525753
Stop taking lewd pics of your sister
>>
File: 1457963812344.jpg (93 KB, 806x813) Image search: [Google]
1457963812344.jpg
93 KB, 806x813
>>53525227
>trying to learn
>shitposting on OP

goddammit Pajeet!
>>
>>53525163
why the fuck are you using goto, hang yourself with your goto statement.
>>
>>53525884
le goto is bad meymey
>>
>>53525895
It's no meme, it's bad.
>>
File: hmmm.png (293 KB, 1984x1160) Image search: [Google]
hmmm.png
293 KB, 1984x1160
>>53525766
>>
>>53525774
>i was never a bad programmer, even when i was learning i was a professional

kill yourself, bud
>>
>>53525996
I never was a bad programmer though Pajeet.
>>
File: ddb33bccf4[1].jpg (24 KB, 480x124) Image search: [Google]
ddb33bccf4[1].jpg
24 KB, 480x124
>>53525895
it's not a meme, it's serious
>>
>>53526013
ok
>>
>>53526027
It's used in many GNU projects and also in Linx source code. Therefore it can't be wrong.
>>
Always turn to a piece of paper first and draft an algorithm concept. Once you got the algorithm down, translating it into syntax is easy.

Do your best to avoid duplicate code and start early with functions. Object oriented programming you can always learn later on. It's easy to break functions into classes and methods anyway.

But yeah. Find some guides on popular algorithms and logic first and just read those for a week to make sure you don't do it the indian way.
>>
I understand what you're trying to do, but you're doing it wrong. You want to use a vector. A vector is basically an array that you can dynamically resize. You add to the array with the vector member function push_back(). Also consider using the function getline(), which takes a stream and a string as its arguments (and an option third argument that I won't go into here). It reads a line from the stream into the string, and then you can do something with it.

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

int main(int argc, char *argv[])
{
std::ifstream inFile("desc.txt");
std::string temp;
std::vector<std::string> data;

while (getline(inFile, temp)) {
data.push_back(temp);
}

for (std::vector<std::string>::size_type i = 0; i < data.size(); ++i) {
cout << data[i] << "\n";
}

inFile.close();
return 0;
}
>>
>>53525794
Not really, its also not tiny speedup as you also create more variables and would probably at the processor level require SIB addressing instead of just standard mod-reg-r/m.
>>
>>53526085
Whoops.

while (getline(inFile, temp)) {


should be

while (std::getline(inFile, temp)) {


And obviously

cout << data[i] << "\n";


should be

std::cout << data[i] << "\n";
>>
>>53526192
using namespace std;

done
>>
>>53525895
>>53525925
>>53526027
Ignoring the "Thou shalt not use goto" dijkstra commandment, when would you actually use goto? The last time I used goto was about 10 years ago, then I learned of the existence of for and while. Since then, I've not once felt compelled to use goto. When is it ever desirable or even remotely practical to use goto?
>>
>>53526303
void foo()
{
if (!doA())
goto exit;
if (!doB())
goto cleanupA;
if (!doC())
goto cleanupB;

/* everything has succeeded */
return;

cleanupB:
undoB();
cleanupA:
undoA();
exit:
return;
}
>>
>>53526303
also, breaking out of nested loops.
>>
>>53526303
Useful IMO for exceptions in a switch case (one case usually captures all types of data but there are just a few exceptions. Instead of copying and pasting the correct case you could just have a label and goto it. I know this usually indicates poor design and, especially in networking, vulnerability, but it's practical in some cases).
>>
>>53525325
Not here they aren't. Read a fucking book
>>
>>53526303
This

If you've structured your program I'm any way that requires a goto, you need to restructure your program
>>
>>53527007
Shit, so we have to quit using linux.
>>
>>53526362
If else you fucking degenerate
>>
>>53526362
Why can't you just do this with a switch?
>>
>>53526293
It's not recommended for anything beyond exercises.
>>
File: 20160316_190224.jpg (973 KB, 2048x1152) Image search: [Google]
20160316_190224.jpg
973 KB, 2048x1152
I'm done trying, if it works then its fine.
>>
>>53525895
>>53526055
>>53526362

You're a fucking retard.
goto is not bad by itself, but the way OP is using it is absolutely retarded.

do
{
inFile >> roomdesc[i];
} while (i++ < 700);


Is equivalent and the most important: is fucking readable.
>>
>>53527184
lol if you actually think if/else is viable
>>
File: 1437789631492.png (118 KB, 694x732) Image search: [Google]
1437789631492.png
118 KB, 694x732
>>53527214
lol
>>
>>53527446
That's your opinion, and until you provide some facts, install gentoo.
>>
>>53528503
What's the point of else if once the first condition finishes executing the second couldn't possibly execute because of the goto?
>>
>>53527214
>>>53525895
Nooooo. pls make room base class. You'll be glad you did.
>>
>>53525163
> goto
You're a troll, right ?
>>
>>53525706
He is using gotos. Don't even try to explain him OOP.
>>
>>53527214
ayy lmao
>>
>>53526055
>GNU
>Linux
>good code
>>
x86 opcodes 0xE9, 0xEB, 0xEA and 0xFF [20-30], as well as anything in C++ that directly causes a jmp to be assembled, such as goto and use of __asm including jmp, are banned in the 4Chan++ Book of Programming, as the writer of the code never has any idea how his code should be structured and the scope limitations of goto that prevent most terrible misuse shouldn't be relied on. Instead, 4Chan++ers restructure their code needlessly to avoid use of a feature of C++ because they read it somewhere in a book.
>>
>>53527324
>>53527198
>you have code i disagree with so you're a fucking retard

really dude? really? grow the fuck up. the code came from stack overflow, a place I trust immensely more than this shithole. you have a problem with the code, google it and go argue with him.
>>
File: 1451952031110.jpg (34 KB, 540x400) Image search: [Google]
1451952031110.jpg
34 KB, 540x400
>>53527324
>100% didn't read the thread, instead just makes himself look retarded
>>
>>53525970

do it a bunch of times.
Thread replies: 56
Thread images: 8

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.