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

C++ help

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 54
Thread images: 3

File: devenv_2017-04-24_23-23-57.png (9KB, 316x225px) Image search: [Google]
devenv_2017-04-24_23-23-57.png
9KB, 316x225px
hi /g/,
I have a final exam tomorrow for my Into C++ class and I'm going over the practice exam my professor gave out. One of the questions asks what the output of the code in the image is, but when I try to test it out, it says "a value of type "double *" cannot be assigned to an entity of type "double"".

Did my teacher mess this up? Is it a trick question? Can I fix something in the code so that I can see the intended result?

Please help. Thank you.
>>
first post on /g/ I'll try and post the code.
int main () {
double *p,*q;
double i=2.3, k =4.3;
p=new double[2];
*p=i; q=p;
cout<<*p<<" "<<*q<<endl;
i=8.4;
cout<<*p; p[1]=&i; i=5.2;
cout<<" "<<*p<<endl;
*q=k;
cout<<p[0]<<" "<<*q<<p[1]<<endl;
return 0;
}
>>
"p[1] = &i" is the culprit (incompatible types). You should see a result (not necessarily that which is intended) by replacing it with "p[1] = (double)&i" (casting the double* to a double)
>>
>>60062132
I attempted to do that and it still says it's an invalid type conversion.

also tried to do static_cast<double>(&i);
>>
Piggy backing for my own question;
I have a major assignment due soon in which we have to parse an entire (oxford) dictionary file into an array where every word and its definition are a separate object in order to qucik search the word. Any idea on where to start?
>>
>>60062052
Your teacher should rethink his approach.
Ugly pointer manipulation and naked new/delete is not something you show on the first lesson.
The whole community is trying to move away from that...
>>
Just buffer it senpai, you dont want segfaults
>>
>>60062259
It's so confusing. This is her first semester teaching here. she's from some middle eastern country. I don't understand pointers at all no matter how hard I try and these kinds of quiz questions just mess me up even more.
>>
>>60062213
Did they specifically say "an array"?
Because when you hear "dictionary" or "key/value pair" you should think "map" or any other associative container.

With an array you'd need to store std::pair<std:: string, std::string> and sort/search with a custom compare function which only looks at the first element of each pair.
It's doable but completely retarded compared to using std::map.
>>
>p=new double[2];
>p[1] = &i;
wew lad
you're trying to store a reference (unsigned 32/64bit int) inside a double
the reference seems to get converted to a double* which explains the error

that'll be 1000rupees thank you pajeet
>>
>>60062259

I don't even see a delete there, lol.

I guess it's fine to start using naked new early, even though you don't want to use it as a general rule. It's easier to understand than make_unique and make_shared

>>60062052

>p[1] = &i
This bit is what's causing the error I think. Try gettong rid of the &. Just a stab in the dark, I haven't thought about it much.
>>
>>60062323

The unary & operator takes a pointer of, not a reference to
>>
>>60062323
I think she is expecting a legitimate output on the answer though. She never gives "error"/no output answers on her quizes.
>>
>>60062286
To understand pointers you need to understand the basic memory model of a computer.
Basically, a very wide array of bytes starting from index 0 to over 9000.
A pointer is simply an address (offset) in memory.
Operator& (unary) gives you the address of a thing (usually a variable).
Operator* (unary) is the reverse operation, gives the value of the stuff stored at a given address.
>>
>>60062341
I understand that much. It's just when I have so many things pointing to so many other things I get lost really quickly.
>>
>>60062335

It's probably just an oversight. It's easy to make these kinds of mistakes when you're not really thinking about what you're doing.
>>
>>60062330
Bjarne himself recommends to start directly with make_unique and either unique_ptr or simply the "auto" keyword
>>
>>60062355
that's what I'm thinking. Do you have any idea what the intended result was supposed to be?
>>
File: CS grad.jpg (103KB, 889x468px) Image search: [Google]
CS grad.jpg
103KB, 889x468px
>>60062356

Well shit, I'm BTFO.

>>60062360

change
p[1] = &i
to
p[1] = i
and see if it compiles. If it does, run it.
>>
>>60062354
This is when you get your paper and pencil and draw that shit out
>>
lemme just do your homework OP

http://ideone.com/plq62i
>>
>>60062354
That's why modern C++ practices suggest you don't juggle with more than a couple of pointers at a time.
It just makes the code harder to read for no particular benefit...
>>
>>60062395
>>60062375
yeah that's what I did and what I got... But I feel like she's trying to indicate something by the &, even if the compiler doesn't technically allow it to happen. Like that she's trying to say that p[1] will change to 5.2 when i changes or something like that.
>>
>>60062427

For that to work, p would have to be of type double*[]
i.e. a pointer to an array, which holds pointers to doubles anywhere.

It's most likely an oversight. That said the code quality here is pretty poor.
>>
>>60062427
I tried it with 3 different compilers they all throw the same error and clang even recommends leaving off the &.

The only thing I can think of is she fucked up and didn't check after making the & typo or she used some dinosaur compiler like devc++. Just complain to the teacher on the class discussion board or in person if you get it wrong.
(output on >>60062395)
>>
>>60062427
It's most likely just a logic error on the part of the programmer. If you get a question like this on the exam, that similarly also doesn't compile, don't try to guess their intentions and spout off some random answer. Just explain clearly what you think is wrong with the code, how you would fix it based on what you assume it's supposed to do, and then explain the output of the fixed version.
>>
>>60062294
They said an array or other std container so a a vector I think is more likely there are said to be more than 106000 objects
>>
File: 1492970701717.jpg (1MB, 2048x1536px) Image search: [Google]
1492970701717.jpg
1MB, 2048x1536px
I love when /g/ helps students learn. Makes me feel less like shit
t. TA for a professor at my uni
>>
>>60062052
p[1] = &i;

Makes zero sense. For a few reasons:
>already used and proved p is a double array
>tries putting the same value in again, but this time, as a pointer
>has the value k that's unused, I can only assume it's used later

He was either supposed to do one of 2 things by the looks:
p = &i;

Or

p[1] = i;

I'm guessing the latter. But his intentions are unclear.

The actual solution is to walk up, bitch slap him, and tell him you don't get paid enough to debug his shitty code.
>>
>>60062052
>SHUT THE FUCK UP!

It's a bug alright - in the code. How long have you been a
teacher? And you *still* haven't learnt the first rule of coding?

If a change results in the compiler throwing an error, it's a bug in the
code. We never EVER blame the compiler. How hard can this be to
understand?

To make matters worse, your exam is clearly total and utter
CRAP even if its code did compile. A value of type "double *" cannot be assigned to an entity of type "double". It never has been possible, and never will be.

Fix your f*cking "exam", because it is obviously broken.
And fix your approach to programming.

Anon.
>>
>>60062286
This is why they should teach you fuckers C BEFORE C++.
There's much less to get your head around and it just works. C++ throws you in the deep end and overwhelms the new user with it's use of objects in the standard library and dumb syntax to go with them.

You will see in time that pointers are nice and easy to deal with. Bad coders, on the other hand, are the reason you will hate them.
>>
>>60062857
why on earth would you think that I'm the instructor? did you even read the post?

>>60062858
yeah I gotta say I'm pretty overwhelmed.
>>
>>60062659
std::map *is* a standard container. Use it.
>>
>>60062540
This, and brace for your teacher answer.
If he insist on this being correct or even remotely a good introduction you should change course and/or university.
>>
>>60062857
>We never EVER blame the compiler.
I've had g++ segmentation fault on me providing absolutely no information about mistakes in the code. Checkmate atheist.
>>
>>60062857
Are you fucking retarded m8, maybe try actually reading OP's post instead of making shit up to vent your frustration
>>
>>60062858
>>60063100
It's entirely possible to teach C++ step by step with no introduction to C whatsoever.
It's just that half the so called "C++ teachers" are just seeing it as "C with classes", a practice that every serious C++ programmer has been considering retarded for decades
>>
>>60062858
Agreed.

There is way too much legacy shit in C++ to be taught to absolute beginners.

"& means address of, but here in the function prototype it means pass this value by reference, yes you can use pointers to do the same thing, just remember both"

this makes 0 sense to someone new to C++ and C
>>
>>60063100
>>60063142
>>60063153
>they don't read the linux mailing list

It's obviously version of a Linus Torvalds rant.
It is supposed to be written by the OP to the teacher.
>>
Can anyone help with this line of code; It errors out with long begin, end, and lineNumber. Not sure quite where I've gone wrong.

vector<Word> theDictionary;

void Dictionary::loadDictionary()
{
string filename = "dictionary.txt";
long begin, end, lineNumber;
string record;
cout << "Attempting to read text file...\n";
ifstream myfile(filename);

if (myfile.is_open())
{
// a Word instance to store the current record
Word currentRecord;

while (!myfile.eof())
{
getline(myfile, record);
// check that the current line is not a comment
if (!(record.substr(0, 2) == "//"))
{
// copy the read in line of text to the Word record
currentRecord.getWord() = record;
// put the Word record into the vector theDictionary
theDictionary.push_back(currentRecord);
}
}
myfile.close();
// now print all the records
for (Word Word : theDictionary)
{
cout << Word.getWord() << endl;
}
}
else cout << "Unable to open file";
}
>>
>>60065041
bumping
>>
>>60065041
> // copy the read in line of text to the Word record
> currentRecord.getWord() = record;

Not sure what you meant but this looks fishy.
Does getWord() return a reference?
>>
>>60065250
so the getWord() function returns the string contained in it called word but it I haven't referenced it
>>
>>60065302
Which means it returns a temporary copy of whatever the underlying object is in Word, replace the content by what you read and then discards it.
You should have a set() to mirror your get() method. There might be better alternatives from a design point of view but for now it'll suffice.
>>
Can you guys c++ experts help me with my thread:
>>60065285
I'm sure it's super simple but I've never used that "initializer_list", thank you!
>>
>>60065342
Yeah see i have a setter in my header file and another source but I don't know where it fits into this file.
>>
>>60065416
bump again due to being seriously lost
>>
>One of the questions asks what the output of the code in the image is

Obviously, the output is a compiler error.
>>
>>60063174

Don't forget about & also meaning references. It's a clusterfuck.
>>
>>60065771
Is setWord() defined? Is there a function body you can look at or is defining it part of the exercise?
>>
>>60063135
or maybe he forgot a
reinterpret_cast<double>(&i)
:^)
>>
>>60062259

>Ugly pointer manipulation and naked new/delete is not something you show on the first lesson.
>The whole community is trying to move away from that...

Pointers are one of the first things you need to teach with both C and C++. They are one of the primitive building blocks for constructing other types. Even if std::unique_ptr is better most of the time, if you want to improve understanding, it makes sense to teach the primitive from which the abstraction is constructed. It will also serve to demonstrate WHY we use the abstraction over the primitive.
>>
>>60066202
setWord is defined by me as part of the exercise but any help you can give would be ace (if you can't tell I'm very new to c++):
in the class it is defined as
void setWord(string wordIn);
and later is written as
void Word::setWord(string wordIn) {
word = wordIn;
}
>>
>>60062427
fucking email your prof and ask for clarification then.
Thread posts: 54
Thread images: 3


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

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
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.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.