Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
astr0man
Feb 21, 2007

hollyeo deuroga
If all you are doing is printing out the addresses to look at them you can also just use %p in printf-family functions to print pointers in hex.

Adbot
ADBOT LOVES YOU

Sulla Faex
May 14, 2010

No man ever did me so much good, or enemy so much harm, but I repaid him with ENDLESS SHITPOSTING
Unfortunately %p will print out the hex value and I just really wanted to compare also the int value as well :)

nielsm
Jun 1, 2009



Sulla-Marius 88 posted:

Unfortunately %p will print out the hex value and I just really wanted to compare also the int value as well :)

Keep in mind, it's the same thing. Same numeric value, different ways of writing it.
e: Bunch of stuff about number systems etc

nielsm fucked around with this message at 19:11 on Apr 2, 2014

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
I am probably going to post a lot of stupid questions over the next few days. I have been asked to supply a C++ program that performs a certain task, as an evaluation exercise for a company I sent a job application to. This company does all Windows stuff, and they've indicated that they will be compiling the code I supply using MS Visual Studio. So, I figured it would be a good idea to do the coding in one of the free editions of Visual Studio. Previously I've done some stuff with C++ using Code Blocks. But it's the same language right, should just be a different interface and not a big deal? Errm...

Well, anyway, I want to start by testing that I can parrot back a command line argument. This is what's staring me in the face from the main .cpp file that was automatically generated for me by the IDE. I added the #include <iostream> and all the lines inside _tmain() (except the return 0) myself.

code:
// program-name.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
	std::cout << "There are " << argc << " arguments." << std::endl;
	for (int i = 0; i < argc; ++i) {
		std::cout << "Here is one of them:" << std::endl;
		printf("%s\r\n", argv[i]);
		//std::cout << i << ": " << *argv[i] << std::endl;
	}
	std::getchar();
	return 0;
}
Here's what you get if you run it from the Windows command line:

code:
D:\Docs\cplusplus\program-name\Debug>program-name warts apple butt
There are 4 arguments.
Here is one of them:
p
Here is one of them:
w
Here is one of them:
a
Here is one of them:
b
It prints the first letter of each argument. Here is what you get if you uncomment the commented line and comment out the printf line:

code:
D:\Docs\cplusplus\program-name\Debug>program-name warts apple butt
There are 4 arguments.
Here is one of them:
0: 112
Here is one of them:
1: 119
Here is one of them:
2: 97
Here is one of them:
3: 98
It prints the ASCII value of the first letter of the argument (it took me longer than it should have to figure out that's what it was doing). If you delete the * in *argv[i], it prints what is presumably the address in memory of the argument. You see, I understand what a pointer is, I just don't know how to make a string show up.

I miss Python already.

edit: I found some StackOverflow questions that suggested that this was something to do with UTF-16 strings and that I should use std::wcout instead. That doesn't fix the problem.

Hammerite fucked around with this message at 20:06 on Apr 4, 2014

Bonfire Lit
Jul 9, 2008

If you're one of the sinners who caused this please unfriend me now.

You have UNICODE defined. That makes <tchar.h> define _tmain and TCHAR as wmain and wchar_t.

std::cout doesn't know how to output a wide string (aka wchar_t*), but it knows that it's some kind of pointer, so it'll print the address.

printf's %s format specifier also expects a narrow string (aka char*). So when it gets a wide string as its parameter (which is UTF16LE on Windows), it'll output a byte from the string (which will be the least significant byte of the first wide character), and then, since you pass in UTF-16 encoded characters from the ASCII range, the next byte will be a 0x00, at which point printf stops. If you want to print wide strings with printf, you need to use %ls instead of %s. Alternatively, if you want to use iostreams, use std::wcout and drop the asterisk from your argv access.

nielsm
Jun 1, 2009



The reason for behavior you're seeing is the Microsoft way of writing a program that theoretically can compile to both a "Unicode" (wide-string) and an "ANSI" (multibyte-string) version, this is the _t _T prefixed stuff. By default, newer versions of Visual Studio only generate a project configuration for Unicode so despite the code form, you're only ever getting the widestring versions of things.
In other words, _tmain() becomes wmain() and _TCHAR becomes wchar_t, and more.
But you're then passing a widestring into something that expects an mbcs string instead, so it reads the string byte by byte and since the second byte of low ASCII text in UTF16 LE is always a nul, it stops there.

I would suggest dropping the TCHAR stuff and just be explicit about character widths everywhere. The only use for non-widestring software on Windows is if you need to run on Windows 98 (or Me or 95), and you most certainly don't.
On the other hand, for a simple console application you may not need to handle Unicode stuff, unless you need to open files by user-supplied filename. So write your main function for mbcs strings instead:

C++ code:
#include "stdafx.h"
#include <iostream>

int main(int argc, char* argv[])
{
	std::cout << "There are " << argc << " arguments." << std::endl;
	for (int i = 0; i < argc; ++i) {
		std::cout << "Here is one of them:" << std::endl;
		printf("%s\r\n", argv[i]);
		//std::cout << i << ": " << *argv[i] << std::endl;
	}
	std::getchar();
	return 0;
}
Note that the only thing I changed was the function name and the type of argv. The compiler should automatically figure out that you want the entry point to be main() and not wmain().

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Thankyou both, this is really helpful.

This isn't an application that will need to handle text strings as a part of its main functionality, so dropping the _t stuff as you suggested seems like a sensible way to go.

Hammerite fucked around with this message at 21:00 on Apr 4, 2014

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Bonfire Lit posted:

printf's %s format specifier also expects a narrow string (aka char*). So when it gets a wide string as its parameter (which is UTF16LE on Windows), it'll output a byte from the string (which will be the least significant byte of the first wide character), and then, since you pass in UTF-16 encoded characters from the ASCII range, the next byte will be a 0x00, at which point printf stops. If you want to print wide strings with printf, you need to use %ls instead of %s. Alternatively, if you want to use iostreams, use std::wcout and drop the asterisk from your argv access.

This is such a common bug, it's too bad MSVC doesn't warn for it. :(

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe
I'm getting a weird error when trying to initialise a class as a member of another class. I have a Character class with a member Rect bounding, but for some reason when I call the Rect constructor in the Character constructor it, for some reason, thinks I'm not giving it any arguments. I made a dummy class to see if I could recreate the issue, and it seems to be a general problem with Rect.

This is my Dummy class:
C++ code:
class Dummy {
public:
    Dummy();
    Rect bounding;
};

Dummy::Dummy() {
    this->bounding = Rect(100,100,Math::iVec2(100,100));
}
The Rect class declaration:

C++ code:
class Rect : public Shape {
public:
    Rect(int width, int height, Math::iVec2 position);
    
    bool intersect(Rect* rectangle);
    bool contains(Math::iVec2 point);
    
    void draw();
    void update();
    
    Math::iVec2 getVertex(int index);
    
private:
    Math::iVec2 vertices[4];
    int width;
    int height;
    
    void updateVertices();
};
and the Rect constructor:
C++ code:
Rect::Rect(int width, int height, Math::iVec2 position) {
    this->width = width;
    this->height = height;
    this->position = position;
    updateVertices();
}
This is the error I get:
code:
Dummy.h:19:14: error: no matching function for call to ‘Rect::Rect()’
 Dummy::Dummy() {
              ^
Dummy.h:19:14: note: candidates are:
In file included from Dummy.h:11:0,
                 from main.cpp:6:
Shape.h:38:5: note: Rect::Rect(int, int, Math::iVec2)
     Rect(int width, int height, Math::iVec2 position);
     ^
Shape.h:38:5: note:   candidate expects 3 arguments, 0 provided
I have no clue as to why it seems to ignore that I am, in fact, giving the constructor three arguments.

If I make bounding into a pointer to a Rect and allocate it on the heap with new, the class compiles and runs as expected, but I'd rather avoid allocating on the heap, since I'm working on an embedded system and every bit of optimisation helps.

This is the Dummy class that works btw:
C++ code:
class Dummy {
public:
    Dummy();
    Rect* bounding;
};

Dummy::Dummy() {
    this->bounding = new Rect(100,100,Math::iVec2(100,100));
}
E: I should note that this problem is exclusive to Rect. Any other class works fine as a member of another class. The only place I'm able to make an instance of Rect is main() and I can call members and member functions from there.

Joda fucked around with this message at 14:34 on Apr 7, 2014

Dicky B
Mar 23, 2004

C++ code:
Dummy::Dummy() {
    this->bounding = Rect(100,100,Math::iVec2(100,100));
}
There is an implicit call to Rect::Rect() here, before the curly brackets. This is because every member of Dummy needs to be fully constructed before the body of the Dummy constructor executes. The compiler will generate a default constructor for you only if you don't explicitly define any constructors yourself.

This is ok though, because you don't need a default constructor. You can just have the Dummy constructor call your user-defined Rect constructor instead, using an initialization list:
C++ code:
Dummy::Dummy() : Rect(100 ,100, Math::iVec2(100,100)) {
	// ...
}

hooah
Feb 6, 2006
WTF?
Also, you don't need to use "this->" to work with data members of the class the method belongs to. The compiler assumes you're referring to "this" object

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe
code:
Dummy.h:19:18: error: type ‘Rect’ is not a direct base of ‘Dummy’
 Dummy::Dummy() : Rect(100 ,100, Math::iVec2(100,100)) {
This is the error I get if I use that line of code. Rect isn't supposed to be a superclass to Dummy.
I also tried adding an empty default constructor for Rect and now my code compiles fine. However, when I make a call for bounding from main() I get the following error:

code:
main.cpp:13:24: error: request for member ‘bounding’ in ‘dummy’, which is of non-class type ‘Dummy()’
     std::cout << dummy.bounding.getVertex(0).x << std::endl;
this is my main():
C++ code:
Dummy dummy();

std::cout << dummy.bounding.getVertex(0).x << std::endl;
I'm not sure if it's saying bounding is of non-class type Dummy or dummy is. I feel like I made a fairly obvious mistake somewhere. Also, I have another class where I'm able to initialise all members and fields in the constructor without issue. I thought the point of a constructor was that you could use it to initialise members?

I'm coming from Java, so I'm sure I picked up loads of bad habbits which are giving me hell now.

nielsm
Jun 1, 2009



Joda posted:

code:
Dummy.h:19:18: error: type ‘Rect’ is not a direct base of ‘Dummy’
 Dummy::Dummy() : Rect(100 ,100, Math::iVec2(100,100)) {
This is the error I get if I use that line of code. Rect isn't supposed to be a superclass to Dummy.

Yeah, Dicky B made a typo in his example. Try this instead:

C++ code:
Dummy::Dummy()
: bounding(100, 100, Math::iVec2(100,100))
{
	// ...
}
The name before the constructor parameter list must be that of the field, not of the class being constructed.

But as the error message you got also hints, the same syntax is used to call the constructor of superclasses from subclasses, that's done by using the name of the superclass for the initializer.

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe
Sweet! That fixed it. Thanks for the help.

I'm still a bit confused as to why I can initialise an iVec2 in a constructor and not a Rect, but at least now I know how to handle that error.

WaterIsPoison
Nov 5, 2009

Joda posted:

Sweet! That fixed it. Thanks for the help.

I'm still a bit confused as to why I can initialise an iVec2 in a constructor and not a Rect, but at least now I know how to handle that error.

This is the key information you need from Dicky B.

Dicky B posted:

The compiler will generate a default constructor for you only if you don't explicitly define any constructors yourself.

Since you already defined a constructor for Rect, the compiler did not generate a default no-argument constructor for Rect.

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe
I guess I didn't explain this, but iVec2 has a specified constructor as well, and no default constructor.

Dicky B
Mar 23, 2004

You're confusing initialization with assignment.

C++ code:
Dummy::Dummy()
: bounding(100, 100, Math::iVec2(100,100)) // initialization (this was where the error was happening)
{
    bounding = Rect(100,100,Math::iVec2(100,100)); // assignment
}
As an experiment, try writing your own default constructor for Rect, which initializes all its members:

C++ code:
Rect::Rect() : width(0), height(0), position(Math::iVec2(0,0)) {
	updateVertices()
}

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
Here is a new stupid question to delight everybody.

I want to test that I can write to a file, and read from it. Turns out I can do the first one of those, but not the other.

Here's my code:

code:
#include <iostream>
#include <fstream>
#include <string>

int main (int argc, char * argv[]) {
    std::fstream the_file;
    std::string current_line;
    the_file.open("my-file.txt", std::ios::in);
    printf("What did it say?\r\n", argc);
    while (std::getline(the_file, current_line)) {
        printf("%s\r\n", current_line);
    }
    the_file.close();
    the_file.open("my-file.txt", std::ios::out | std::ios::trunc);
    for (int i = 0; i < argc; ++i) {
        the_file << argv[i] << "\r\n";
    }
    the_file.close();
    printf("Now it says some other things... %d things.\r\n", argc);
    std::getchar();
    return 0;
}
The expected behaviour is that when the program is invoked, lines are read from my-file.txt and displayed on the screen, and then the program's command-line arguments replace them in the file. The observed behaviour is that the command-line arguments are indeed stored in the file (I can open the file in a text editor and check this), but displaying them on the screen does not work. This is what I see (before the first invocation of the program the file does not exist):

code:
D:\Docs\cplusplus\program-name\Debug>program-name hello how are you
What did it say?
Now it says some other things... 5 things.


D:\Docs\cplusplus\program-name\Debug>program-name say something to me
What did it say?
&#9472;²B
&#9472;²B
&#9472;²B
&#9472;²B
&#9472;²B
Now it says some other things... 5 things.


D:\Docs\cplusplus\program-name\Debug>program-name welp
What did it say?
 ·¶
 ·¶
 ·¶
 ·¶
 ·¶
Now it says some other things... 2 things.
D:\Docs\cplusplus\program-name\Debug>
It always prints out the correct number of lines, but the lines are not what they should be, as you can see. The symbols that are printed are not always the same for the same input (I can invoke the program several times in succession with the same command line arguments and get a different set of 3 characters each time).

As far as I can tell I'm doing what it says in this tutorial, so I don't know why it doesn't work for me.

edit: obviously it doesn't spit HTML character entities back at me, that's the forums doing that.

pseudorandom name
May 6, 2007

code:
sa.cpp:9:36: warning: data argument not used by format string [-Wformat-extra-args]
    printf("What did it say?\r\n", argc);
           ~~~~~~~~~~~~~~~~~~~~~~  ^
sa.cpp:11:26: error: cannot pass non-POD object of type 'std::string' (aka 'basic_string<char>') to variadic function; expected type from format string was
      'char *' [-Wnon-pod-varargs]
        printf("%s\r\n", current_line);
                ~~       ^~~~~~~~~~~~
sa.cpp:11:26: note: did you mean to call the c_str() method?
        printf("%s\r\n", current_line);
                         ^
                                     .c_str()
1 warning and 1 error generated.

WaterIsPoison
Nov 5, 2009
nvm;dumb

WaterIsPoison fucked around with this message at 01:11 on Apr 8, 2014

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

pseudorandom name posted:

code:
sa.cpp:9:36: warning: data argument not used by format string [-Wformat-extra-args]
    printf("What did it say?\r\n", argc);
           ~~~~~~~~~~~~~~~~~~~~~~  ^
sa.cpp:11:26: error: cannot pass non-POD object of type 'std::string' (aka 'basic_string<char>') to variadic function; expected type from format string was
      'char *' [-Wnon-pod-varargs]
        printf("%s\r\n", current_line);
                ~~       ^~~~~~~~~~~~
sa.cpp:11:26: note: did you mean to call the c_str() method?
        printf("%s\r\n", current_line);
                         ^
                                     .c_str()
1 warning and 1 error generated.

I see that std::cout << current_line << "\r\n"; works in place of the printf line. I should have thought of trying that, but I didn't.

WaterIsPoison posted:

Hammerite, is this part of your coding interview?

It is something that I needed to work out in order to tackle the evaluation exercise they sent me, yes.

Diametunim
Oct 26, 2010
For my current CS project we have to write a pseudo Ipod. We have to hardcode "songs" that we would like to store in memory and print out a list of these stored songs to the terminal. You can have a maximum of 25 songs or a 100MB of songs total, whichever comes first. It's basically a lesson in memory management.

Here's a question, previously I wrote this program using an array for the song list and everything worked fine. This time around we're not allowed to use an array to store the songs and manage memory. We have to store the "songs" in a binary .dat file and work from there. How in the gently caress would I even do this?

We also have to create two separate classes one class for adding, removing, shuffling songs..ect ect and another class for the actual "song(s)". I'm having trouble wrapping my head around if I'm supposed to hardcode 25 different song objects so I can work with 25 potential "songs" or am I missing something here?

a "song" is just (song title, artist name, size) in text (or binary in this case). we're not actually loading in real songs.

FamDav
Mar 29, 2008
http://www.cplusplus.com/doc/tutorial/files/

FAT32 SHAMER
Aug 16, 2012



So for some reason my client code isn't recognising Class Alumni as a type.

ColMbrs.h:
C++ code:
#ifndef ColMbr_ColMbrs_h
#define ColMbr_ColMbrs_h

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;


class ColMbrs
{
public:
    ColMbrs( unsigned idNmbr, string studName ) : name( studName ), idNbr( idNmbr ){}
    
    void setName( string studName ) {}
    
    virtual void addClass( unsigned cred, unsigned qual ) = 0;
    
    virtual void display( void ) const
    {
        cout << idNbr << ", " << name << endl;
    }
    
    
private:
    const unsigned idNbr;
    string name;
};
#endif
Student.h:
C++ code:
#ifndef ColMbr_Student_h
#define ColMbr_Student_h
#include "ColMbrs.h"


#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Student : public ColMbrs
{
public:
    Student( unsigned idNmbr, const string name, unsigned credits = 0, unsigned qPts = 0, const string deg = "Unspecified" ) : ColMbrs( idNmbr, name ), credHrs( credits ), qualPts( qPts ), degSought( deg ){}
    
    void setDegree(){}
    
    void addClass( unsigned int1, unsigned int2 )
    {
        credHrs = int1;
        qualPts = int2;
    }
    
    double getGPA()
    {
        if( credHrs == 0 )
        {
            return 0;
        }
        else
        {
            return (double) qualPts / credHrs;
        }
    }
    
    virtual void display( void )
    {
        ColMbrs::display();
        cout << "Degree sought: " << degSought << endl
        << fixed << showpoint << setprecision( 2 )
        << "Current GPA: " << getGPA() << endl;
    }
    
    
    
    
    
private:
    unsigned credHrs;
    unsigned qualPts;
    string degSought;
};


#endif

Alumni.h:
C++ code:

#ifndef ColMbr_Alumni_h
#define ColMbr_Alumni_h

#include "ColMbrs.h"

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

Class Alumni : public ColMbrs, public Date
{
public:
    Alumni( unsigned studentID, const string name, int m, int d, int y, const string degree = "Unspecified" ) : ColMbrs( studentID, name ), dateGrad( m, d, y, ), degree( degree ) {}
    
    void addClass( unsigned cred, unsigned qual ){}
    
    void display( void )
    {
        ColMbrs::display();
        cout << "Graduated: " << dateGrad << "with a degree in " << degree << endl;
    }
    
    
private:
    Date dateGrad;
    string degree;
}

#endif

ColMbrTest.cpp:
C++ code:
#include <iostream>
#include <cstdlib>
#include "Student.h"



int main()
{
    ColMbrs *cMbr[4]; // array of base-class pointers
    int i; // index to array
    
    // Create some college members
    
    cMbr[0] = new Student( 12345, "Steven DiFranco", 15, 33, "AA" );
    cMbr[1] = new Alumni( 98765, "Don Green", 12, 15, 1978, "AAS" );
    cMbr[2] = new Alumni( 24680, "Henry Thoreau", 5, 22, 1846, "AA" );
    cMbr[3] = new Student( 13579, "Millenia Best" );
    
    // display the array
    
    cout << "All college members:\n";
    for( i = 0; i < 4; ++i )
    {
        cMbr[i]->display(); // no need to check type field or cast
        cout << endl;
    }
    
    // test addClass for student
    
    cMbr[3]-> addClass( 3, 12 );
    cMbr[3]-> display();
    
    cout << endl;
    return 0; 
}
Anyone have any idea why it wouldn't be recognising Alumni but it recognises Student?

Also lol at my professor for using unsigned integers? I was under the impression that unsigned ints are hilariously bad practice.

pseudorandom name
May 6, 2007

Tusen Takk posted:

So for some reason my client code isn't recognising Class Alumni as a type.

Class isn't a keyword.

Tusen Takk posted:

Also lol at my professor for using unsigned integers? I was under the impression that unsigned ints are hilariously bad practice.

Nope.

FAT32 SHAMER
Aug 16, 2012



pseudorandom name posted:

Class isn't a keyword.


Nope.

haha boy I am an idiot

edit: it still is saying "Unknown type name 'Alumni' though :(

edit 2: well gently caress me I never #include "Alumni.h"

FAT32 SHAMER fucked around with this message at 02:18 on Apr 8, 2014

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Tusen Takk posted:

Anyone have any idea why it wouldn't be recognising Alumni but it recognises Student?

Also lol at my professor for using unsigned integers? I was under the impression that unsigned ints are hilariously bad practice.

Because you're including Student.h and not Alumni.h (Alumnus, ahem).

Unsigned ints are often the right thing; array indices for example tend to fit them. What did you think was wrong with them?

Star War Sex Parrot
Oct 2, 2003

Tusen Takk posted:

Also lol at my professor for using unsigned integers? I was under the impression that unsigned ints are hilariously bad practice.
There are plenty of examples of bad practice in that code, but unsigned data types absolutely have their place and aren't being used incorrectly in this case.

Star War Sex Parrot fucked around with this message at 02:22 on Apr 8, 2014

FAT32 SHAMER
Aug 16, 2012



Subjunctive posted:

Because you're including Student.h and not Alumni.h (Alumnus, ahem).

Unsigned ints are often the right thing; array indices for example tend to fit them. What did you think was wrong with them?

Yeah, I just realised that.

In EvE Online the turbonerds are always "lol unsigned ints" when you have a stack of object x greater than whatever the largest number an unsigned int can accept or something like that. There are other examples in other games but I can't think of any other examples past that.

After adding the #include "Alumni.h", now it is giving me an error saying "Assigning to 'ColMbrs *' from incompatible type 'Alumni *'. I'm guessing I don't have pointers somewhere that they need to be :v:

Praseodymi
Aug 26, 2010

The only problem I could think of is that there's a potential for many unexpected type conversions, I've seen people avoid them for being unnecessary with a few caveats but I'll stick with them as long as I get compiler warnings for comparing an int to the size of a vector or the aforementioned indices. That and people who just use unsigned instead of unsigned int, but that's just a personal preference and they are completely equal.

Star War Sex Parrot
Oct 2, 2003

Tusen Takk posted:

In EvE Online the turbonerds are always "lol unsigned ints" when you have a stack of object x greater than whatever the largest number an unsigned int can accept or something like that.
Using a signed int wouldn't help you with this.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

A stack of things bigger than what you can fit in an unsigned int would be pretty impressive.

FAT32 SHAMER
Aug 16, 2012



Star War Sex Parrot posted:

Using a signed int wouldn't help you with this.

So basically the turbonerds are elitist idiots on top of being nerds. Got it.

My only errors now are for Alumni.h. on the constructor for Alumni it is giving me errors "no matching constructor for initialisation of 'ColMbrs'" and "expected expression".

b0lt
Apr 29, 2005

Tusen Takk posted:

Also lol at my professor for using unsigned integers? I was under the impression that unsigned ints are hilariously bad practice.

Yes, unsigned int is redundant, you should use unsigned instead.

ephphatha
Dec 18, 2009




The problem with the Eve Online inventory code is that it uses signed integers to store item quantities. Given that there's no way to have negative amounts of an item in your inventory there's no point using a signed value to store that information. This halves the maximum quantity that can be represented in a single stack of items (from ~4 mbillion to ~2 mbillion). Even so the only people who run into the item stack limit are the trillionaires with massive stacks of minerals like mynnna.

Edit: I can't count

ephphatha fucked around with this message at 06:23 on Apr 8, 2014

tractor fanatic
Sep 9, 2005

Pillbug
Unsigned ints also have the benefit of being guaranteed by the standard to obey modular arithmetic, so you don't get UB on an overflow.


Billion, not million

tractor fanatic fucked around with this message at 03:26 on Apr 8, 2014

FAT32 SHAMER
Aug 16, 2012



Tusen Takk posted:

So basically the turbonerds are elitist idiots on top of being nerds. Got it.

My only errors now are for Alumni.h. on the constructor for Alumni it is giving me errors "no matching constructor for initialisation of 'ColMbrs'" and "expected expression".
I had an extra comma in dateGrad( m, d, y ) :shobon:. Now I just have to figure out how to make a simple Date class that takes inputted (m, d, y) and spits that back out in a m/d/y format!

Ephphatha posted:

The problem with the Eve Online inventory code is that it uses signed integers to store item quantities. Given that there's no way to have negative amounts of an item in your inventory there's no point using a signed value to store that information. This halves the maximum quantity that can be represented in a single stack of items (from ~4 million to ~2 million). Even so the only people who run into the item stack limit are the trillionaires with massive stacks of minerals like mynnna.
That's it! Also it's easy to have massive stacks of minerals over 2mil, especially with trit :v:

edit: finished it. haha this loving professor hasn't graded any of my projects since HW 2 and we're on HW 8. He is either hilariously behind or he has no idea who I am.

FAT32 SHAMER fucked around with this message at 03:38 on Apr 8, 2014

shrughes
Oct 11, 2008

(call/cc call/cc)

b0lt posted:

Yes, unsigned int is redundant, you should use unsigned instead.

Or you could just go kill yourself for wanting to use an unsigned int. Unsigned int is the maximal code smell as far as unsignedness goes.

Star War Sex Parrot
Oct 2, 2003

Every for loop that I write uses an unsigned short int. :smug:

Adbot
ADBOT LOVES YOU

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Star War Sex Parrot posted:

Every for loop that I write uses an unsigned short int. :smug:
Pfh, if your for loop is shorter than 255 iterations you should be using an unsigned char!

(This isn't true at all, but if unsigned short int was a good idea then unsigned char would be a better one for the same reasons. Unless the target platform is a 16-bit machine. In which case a short int probably is a byte anyway and it's all gone horribly wrong please stop me I can't get out of this comment oh god)

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply