[ 3 / biz / cgl / ck / diy / fa / g / ic / jp / lit / sci / tg / vr / vt ] [ index / top / reports / report a bug ] [ 4plebs / archived.moe / rbt ]

/vt/ is now archived.Become a Patron!

/sci/ - Science & Math

View post   

[ Toggle deleted replies ]
File: 72 KB, 1170x998, Screenshot_1.png [View same] [iqdb] [saucenao] [google] [report]
9410430 No.9410430 [Reply] [Original] [archived.moe]

Can someone please explain how did the output turn out like this?

>> No.9410432
File: 10 KB, 407x110, Screenshot from 2017-12-16 12-19-05.png [View same] [iqdb] [saucenao] [google] [report]

>> No.9410435


>> No.9410436

The function header (line #6) has default values for width and height if none are passed explicitely

>> No.9410437

look at the number of arguments you're using for calling AreaCube, retard

>> No.9410442

Are you... Are you actually serious?

>> No.9410448

>writes in c++
>but uses the language like c
why? why would you use c++ to write a program like that?

at least use curly braces for fucks sake, you're not supposed to write int length = 100, but int length{100} in c++

>> No.9410466

That's a terrible advice.

>> No.9410468

>>but uses the language like c

Better to code like it's c than like it's java.

>you're not supposed to write int length = 100,

Literally nothing wrong with that. Fuck off to the >>>/g/hetto

>> No.9410470

What exactly is the problem? Those values are correct.

>> No.9410475

It's the advice that Barjne himself and every modern book on C++ will give you. How many relevant books have you read? That kind of assignment will protect you against all type conversion errors, it is a modern feature of C++ that should always be used.

t. brainlet
Just go read a book, or two or three, before giving advice online. Please, you're not good enough to be advising anyone on anything

>> No.9410483

the only time the {} list form shouldn't be used is when using auto to determine the type

>> No.9410489


it depends.

in the case of
$ int length = 100;
there's nothing wrong with it, since you're just initializing the int with a literal. as soon as we get to
$ int length = passed_in_from_somewhere;
land, we're allowing implicit, ie information-discarding, type conversion, whereas
$ int length{passed_in_from_somwhere};
will produce a compiler error when passing in a float.

$ std::vector<int> vec(5, 10);
$ std::vector<int> vec{5, 10};
do vastly different things; one creates a std::vector, size 5, all 10s, the other creates a std::vector, size 2, 5 and 10.

the latter is the reason why using curly braces for initialization isn't best practice yet. however, i tend to teach favoring curly braces while being aware of fun issues with container/class initialization.

in the end, it remains a matter of opinion. just as C++ remains a language where you still, despite all C++11 and following mitigation efforts, must know what you are doing.

>> No.9410496

line 6 defines width and height, so when you call the AreaCube funct, it multiplies L W and H, rather than, lets say, just return length.

>> No.9410498

>#include <iostream.h>
Stop coding like it's 1997. Use
>#include <iostream>
>using namespace std;
Standard C++ doesn't support iostream.h

>AreaCube(int length, int width, int height)
You have to include the return type in standard C++; even in the definition after the declaration.
>int AreaCube(int length, int width, int height)

It doesn't matter much and you're missing more glaring errors in OP's code.

>> No.9410504

what a retard are you.

>why? why would you use c++ to write a program like that?
there is no default arguments in C, you have to write 3 functions.

>at least use curly braces for fucks sake, you're not supposed to write int length = 100, but int length{100} in c++
xor yourself.

>> No.9410520

this is what happens when mathfags try to program

>> No.9410547

OP clearly is a CS major.

>> No.9410659

>being aware of fun issues with container/class initialization
That was my point.

The language is very hard as it is and C++ is fixing old inconsistencies by introducing new inconsistencies.

>> No.9410673

You need to overload the function (declare the function with alternate amounts of parameters) if you want to be able to pass it with only 2 arguments. I assume you can do that in c++, Otherwise if you only pass 2 arguments you'll get unpredictable results, afaik.

>> No.9410694
File: 109 KB, 588x823, 1510210435138.jpg [View same] [iqdb] [saucenao] [google] [report]

>c++ is 2 hard

>> No.9410722

>The language is very hard as it is


>> No.9410784
File: 64 KB, 734x752, Screenshot.jpg [View same] [iqdb] [saucenao] [google] [report]

>I am ok with this

How about you start reading the source code of the libraries you use.

>> No.9410791

>not {{}}

pleb, hasn't /pol/ taught you anything

>> No.9410798
File: 99 KB, 561x595, 1514517807855.png [View same] [iqdb] [saucenao] [google] [report]

>> No.9410813

>implying sisper is impressive or anything not freshman level

>> No.9411011

I used to be addicted to c++ in my cs for engineers course last spring.

I used to lookup crazy advanced ways to solve stupid programming assignments like 'make a tic tac toe program' or 'make a shopping cart'

dont listen to these ppl saying 'eewww dirty code' or w/e

>> No.9411038

I'm a beginner but here's my take:

Using AreaCube() allows you to use main's values for length width and height rather than the default values.

Second area is using the main value for length and width and the default value for height outside of the main method. So 100*50*1 = 5000 (defaults are found in line 6)

Third area uses main value for length, and default value for width and height. So 100*25*1 = 2500

Hope it helps.

Name (leave empty)
Comment (leave empty)
Password [?]Password used for file deletion.