• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 06:12
CET 12:12
KST 20:12
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
RSL Revival - 2025 Season Finals Preview8RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12
Community News
Weekly Cups (Dec 15-21): Classic wins big, MaxPax & Clem take weeklies3ComeBackTV's documentary on Byun's Career !11Weekly Cups (Dec 8-14): MaxPax, Clem, Cure win4Weekly Cups (Dec 1-7): Clem doubles, Solar gets over the hump1Weekly Cups (Nov 24-30): MaxPax, Clem, herO win2
StarCraft 2
General
ComeBackTV's documentary on Byun's Career ! Team TLMC #5: Winners Announced! What's the best tug of war? The Grack before Christmas Weekly Cups (Dec 15-21): Classic wins big, MaxPax & Clem take weeklies
Tourneys
OSC Season 13 World Championship $5,000+ WardiTV 2025 Championship $100 Prize Pool - Winter Warp Gate Masters Showdow Sparkling Tuna Cup - Weekly Open Tournament Winter Warp Gate Amateur Showdown #1
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 506 Warp Zone Mutation # 505 Rise From Ashes Mutation # 504 Retribution Mutation # 503 Fowl Play
Brood War
General
What are former legends up to these days? BGH Auto Balance -> http://bghmmr.eu/ BW General Discussion How soO Began His ProGaming Dreams Klaucher discontinued / in-game color settings
Tourneys
[Megathread] Daily Proleagues [BSL21] LB SemiFinals - Saturday 21:00 CET [BSL21] WB & LB Finals - Sunday 21:00 CET Small VOD Thread 2.0
Strategy
Fighting Spirit mining rates Simple Questions, Simple Answers Game Theory for Starcraft Current Meta
Other Games
General Games
Nintendo Switch Thread Mechabellum Stormgate/Frost Giant Megathread Beyond All Reason Path of Exile
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Mafia Game Mode Feedback/Ideas Survivor II: The Amazon Sengoku Mafia TL Mafia Community Thread
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Has Anyone Tried Kamagra Chewable for ED? 12 Days of Starcraft The Games Industry And ATVI
Fan Clubs
White-Ra Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece
Sports
2024 - 2026 Football Thread Formula 1 Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List TL+ Announced Where to ask questions and add stream?
Blogs
National Diversity: A Challe…
TrAiDoS
I decided to write a webnov…
DjKniteX
James Bond movies ranking - pa…
Topin
Thanks for the RSL
Hildegard
Saturation point
Uldridge
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1047 users

The Big Programming Thread - Page 577

Forum Index > General Forum
Post a Reply
Prev 1 575 576 577 578 579 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
tofucake
Profile Blog Joined October 2009
Hyrule19184 Posts
February 03 2015 19:29 GMT
#11521
no, it's not.

switch only works with primitives (iirc). Since string is a class, switch won't work with it.
Liquipediaasante sana squash banana
Eiii
Profile Blog Joined April 2009
United States2566 Posts
February 03 2015 19:56 GMT
#11522
On February 04 2015 03:38 darkness wrote:
Show nested quote +
On February 04 2015 03:36 tofucake wrote:
C++ doesn't have strings. It has a string class.


So yes, that's still a string. I don't care if it was even required to do silly stuff like:


switch (string.c_str())


The point is it doesn't work, and C#/Java both have it.


string.c_str() returns a pointer to a character array-- so in your case, how do you distinguish between switching on a 'string' and switching on the value of the pointer?

A switch statement has a pretty specific low-level meaning in C/C++, as others have covered already. It's entirely understandable that non-enumerable values wouldn't be able to be switched on.
:3
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
Last Edited: 2015-02-03 20:46:19
February 03 2015 20:42 GMT
#11523
On February 03 2015 20:21 Cynry wrote:
Not quite. Let's say I have an array containing 5 arrays of int. I want to expand it to 6 arrays, so I figured I have to "realloc" it. That's the part I need for my assignment, but not what I was asking about. I'm wondering if one could write a function that would work whatever the type of array of array is, int, char...

C wiki page says you can't have an array of void, so I thought this was not an option, hence why I used a macro TAB_TYPE (for which ARRAY_TYPE would be a better name I guess) that the user defines and that lets the function knows what type of data it's working with.

Dunno if that's clear...


It's possible but you have to do more trickery

http://stackoverflow.com/questions/10950828/simulation-of-templates-in-c

Your code has other problems though.


Not sure what documentation you're reading but you can't have arrays of void because void doesn't mean anything as a type in the same way int does. It's perfectly fine to create an int** pointer and cast that to a void** pointer and use that instead, which is how you could achieve your same goal without the #define stuff. using #define like that is smart but not the best way of going about it.
There is no one like you in the universe.
nunez
Profile Blog Joined February 2011
Norway4003 Posts
Last Edited: 2015-02-03 21:55:13
February 03 2015 20:56 GMT
#11524
@darkness
the standard regarding the condition of a switch statement:
the condition shall be of integral type, enumeration type, or of a class type for which a single non-explicit conversion function to integral or enumeration type exists.

aka, this class would be ok in a switch:
class switchable{
public:
operator int() const;
};


in the implementation of this operator you still would have to identify
an int with a string, and probably end up with a if / else if statement,
or a map from string to int. note that you can swap int for any
integral type or enum here.

@tofucake
unacceptable dichtonomy.
c++ contains several concepts that fit the string
descriptor, std::string is one of them.
conspired against by a confederacy of dunces.
Artesimo
Profile Joined February 2015
Germany564 Posts
February 03 2015 22:35 GMT
#11525
I only startet programming this semester with the start of university and desperately need hep. I cant figure out where my thinking is wrong/the error is. To prepare for the upcoming exam, I picked up a few old ones from the past semesters. The instructions sound fairly simple: I am asked to create 2 classes, one that represents a lotto-pick which is 6 numbers and the other one is the lotto-ticket itself which contains up to 8 of these picks. The prof insists on using set- and get-methods since we should learn OOP (could be that the set- and get stuff isnt up to date anymore, I got the bad one ), so dont be confused by that. Also sorry for the german variablenames in the code sections, he insists on that too.

My lotto-pick has only one attribute(lets call it lottonumbers), which is an vector. My set method for the lotto-pick:


void Tippreihe::setFeld(int tippzahl)
{
if ((int)(feld.size()) < 6)
{
feld.push_back(tippzahl);
}
else
{
cerr << "Tippreihe bereits voll!";//is full
}
}


the ticket-class has the attribute "pick number" which is a vector storing the up to 8 picks. When the lottoticket is created, its supposed to be empty, so I made the constructor fill the vector with nullptr on creation. The set-method for my lotto-tickets picks is:

void Lottoschein::setTippreihe(int tippreiheNr)
{
if (tippreiheNr <= 8)
{
if (tippreihen[(tippreiheNr)] = nullptr)
{
tippreihen[(tippreiheNr)] = new Tippreihe;
}
}

}


and to get the picks contained in the ticket, I use:


Tippreihe* Lottoschein::getTippreihe(int tippreiheNr)
{
if (tippreiheNr < (tippreihen.size()) && tippreihen[tippreiheNr] != nullptr)
{
return tippreihen[tippreiheNr];
}
else
{
return nullptr;
}
}


In my main I started to test around a little bit:


Lottoschein schein;

schein.setTippreihe(0);
for (int i = 0; i < 10; i++)
{
schein.getTippreihe(0)->setFeld(i);
}


the number in the for-section is deliberitely chosen to be to big for one set of picks for testing purposes, lowering it doesnt change anything anyways. I already tried a ton of stuff, but I always get a accessviolation error, which means that some index should be out of range and everything that visual studio is telling me points towards the problem either being in one of theese methods, or me having done something completely wrong. Its really frustrating, but thats the part that has to work just to pass and I just cant manage to see the problem. On the other hand, the modification I have to do to my code for the better marks up to the 1.0 (reading in the numbers from a file, calculating how big the gap between to numbers of one pick and overload a operator to replace a browse-method) I am able to do/I am pretty sure I know how to do, just that I cant manage to get to that point.
meatpudding
Profile Joined March 2011
Australia520 Posts
February 03 2015 22:36 GMT
#11526
On February 03 2015 20:21 Cynry wrote:
Show nested quote +
On February 03 2015 15:03 meatpudding wrote:
Are you just trying to copy the contents of one array to another? Sounds like a job for memcpy.

res = malloc(sizeof(void*) * new_size);
memset(res, 0, sizeof(void*) * new_size);
if (new_size < old_size)
memcpy(res, old_tab, sizeof(void*) * new_size);
else
memcpy(res, old_tab, sizeof(void*) * old_size);
free(old_tab);
return res;

Not quite. Let's say I have an array containing 5 arrays of int. I want to expand it to 6 arrays, so I figured I have to "realloc" it. That's the part I need for my assignment, but not what I was asking about. I'm wondering if one could write a function that would work whatever the type of array of array is, int, char...

C wiki page says you can't have an array of void, so I thought this was not an option, hence why I used a macro TAB_TYPE (for which ARRAY_TYPE would be a better name I guess) that the user defines and that lets the function knows what type of data it's working with.

Dunno if that's clear...


Yeah it's true that you can't have an array of void. But, if you have an array of pointers then you can allocate memory as such. Because a pointer is an address in memery, the only difference is the size of the pointer eg 32bit or 64bit. So the size of (int*) is the size of (char*), even though the size of (int) and (char) are going to be different.
If your array always contains pointers, no special code is needed. Your function seems to only handle this case because the return type is TAB_TYPE** which is going to be a pointer to the first element in an array of TAB_TYPE* so you could just do it without defining the pointer type and then casting as necessary.
Be excellent to each other.
meatpudding
Profile Joined March 2011
Australia520 Posts
February 03 2015 22:47 GMT
#11527
@Artesimo

When you initialise Lottoschein.tippreihen are you setting all values to nullptr?

Also you should change
schein.getTippreihe(0)->setFeld(i);
to something like
Tippreihe* tip = schein.getTippreihe(0);
if (tip != nullptr)
{
tip->setFeld(i);
}
Be excellent to each other.
Artesimo
Profile Joined February 2015
Germany564 Posts
Last Edited: 2015-02-03 23:04:28
February 03 2015 23:03 GMT
#11528
On February 04 2015 07:47 meatpudding wrote:
@Artesimo

When you initialise Lottoschein.tippreihen are you setting all values to nullptr?

Also you should change
schein.getTippreihe(0)->setFeld(i);
to something like
Tippreihe* tip = schein.getTippreihe(0);
if (tip != nullptr)
{
tip->setFeld(i);
}


Yes, I set all 8 to nullptr, but I am no longer sure if my reasoning behind it is correct.

To your second part, I am not sure if I fully understand how it works. So It basicly does the same as the line I wrote, just more readable? My thought was, that when asked to read in 4 whole tips, I would have a for-loop
schein.getTippreihe(x)->setFeld(i)
where x gets increased by every 6th number added while i being the current read in line(aka the number). Is my line is still silly then?
meatpudding
Profile Joined March 2011
Australia520 Posts
February 03 2015 23:11 GMT
#11529
On February 04 2015 08:03 Artesimo wrote:
Show nested quote +
On February 04 2015 07:47 meatpudding wrote:
@Artesimo

When you initialise Lottoschein.tippreihen are you setting all values to nullptr?

Also you should change
schein.getTippreihe(0)->setFeld(i);
to something like
Tippreihe* tip = schein.getTippreihe(0);
if (tip != nullptr)
{
tip->setFeld(i);
}


Yes, I set all 8 to nullptr, but I am no longer sure if my reasoning behind it is correct.

To your second part, I am not sure if I fully understand how it works. So It basicly does the same as the line I wrote, just more readable? My thought was, that when asked to read in 4 whole tips, I would have a for-loop
schein.getTippreihe(x)->setFeld(i)
where x gets increased by every 6th number added while i being the current read in line(aka the number). Is my line is still silly then?


Because getTippreihe has the chance of returning a nullptr, you could be in the situation where you are calling nullptr->setField which is going to crash your program (this could be your accessviolation). If you are sure that getTippreihe always returns a valid object then you don't need it.

Be excellent to each other.
Artesimo
Profile Joined February 2015
Germany564 Posts
February 03 2015 23:30 GMT
#11530
Thank you a magical c++ ghost from down under, you just gifted a few more hours of relaxed sleep to me. This was indeed the problem. Even though this means that my setTippreihe isnt working properly, its still a relief to finally know whats going on.
meatpudding
Profile Joined March 2011
Australia520 Posts
February 03 2015 23:34 GMT
#11531
Oh I just took another look. You have a vey bad typo :0
if (tippreihen[(tippreiheNr)] = nullptr)

should be
if (tippreihen[(tippreiheNr)] == nullptr)

The way you have it is assigning nullptr to the array, so it will overwrite every time. You need the double equals to make a comparison.
Be excellent to each other.
Artesimo
Profile Joined February 2015
Germany564 Posts
Last Edited: 2015-02-04 12:36:37
February 03 2015 23:44 GMT
#11532
On February 04 2015 08:34 meatpudding wrote:
Oh I just took another look. You have a vey bad typo :0
if (tippreihen[(tippreiheNr)] = nullptr)

should be
if (tippreihen[(tippreiheNr)] == nullptr)

The way you have it is assigning nullptr to the array, so it will overwrite every time. You need the double equals to make a comparison.


haha, you just solved the next problem I stumbled upon... god I feel so dumb atm :D

EDIT: Yes I am. Was about to ask why I had to change one of my methods to make it wokr, even though it gets called correctly, just to realize that I mixed up how for and while-loops work... time for bed I guess. Thanks a lot meatpudding, you just taught me more than our prof.



EDIT2: fixed
Cynry
Profile Blog Joined August 2010
810 Posts
February 04 2015 12:22 GMT
#11533
So I got my array_realloc to work, just changing TAB_TYPE to void and setting the old[i] to null before freeing it. Seems ok with int **, char **, even char ***, and I still can free everything so no apparent leaks for valgrind. Now, of course, it only works if you want a bigger new array, so maybe I'll change its name, but it's good enough for what I wanted to do with it. Which is using it for int **... Still an interesting little exercice ! Thanks to all of those who answered !
And this stackoverflow link was interesting, but what I wanted to do was simpler than that. Good thing, cause I'm not sure I understood most of it ^^
Artesimo : Don't worry about feeling dumb when you start coding. Most of the mistakes we beginners make are quite stupid indeed, but that's the way to learn :D
Artesimo
Profile Joined February 2015
Germany564 Posts
Last Edited: 2015-02-04 15:23:32
February 04 2015 13:05 GMT
#11534
Thanks for the cheerup Cynry, but messing up the loops is really just clumsy. Well, it was time to go to bed and today started much better BUT;

Also solved :-)
+ Show Spoiler +

void Lottoschein::leseSchein(string source)
{
string zeile;
ifstream scheinlesen(source);
scheinlesen.open(source);
unsigned int i = 0;
int counter = 0;

while (!scheinlesen.eof())
{
if (tippreihen[i] == nullptr)
{
setTippreihe(i);
}

do
{
getline(scheinlesen, zeile, ',');

if (!zeile.empty())
{
tippreihen[i]->setFeld(stoi(zeile));
counter++;
}
else
{
cout << zeile << "+";
}

if (counter == 6 && i < tippreihen.size())
{
counter = 0;
i++;
}
} while (counter <=6);
}
}
just wont work. For some reason getline returns an empty string(even though I am not sure if this is the only problem). I already read about getline and cin and that this can happen when used together, but I dont use cin in my code... At least I got a lot better at using the debugger. When running the thing and replacing the part that checks if the string is empty, It get stuck (otherwhise it just doenst do anything but returning emtpy line after line) and I get theese, correct me if I am wrong with my assumptions:

i = 0 as it should since it is the first round
tippreihen has the size of 8, besides element 0 its all nullptrs as they should, element 0 was a nullptr before since the lottoticket was empty, and therefor got created in the if-section.
element 0 points to feld which is a vector of an object thats currently empty, just as it should. Besides zeile having the value "" it seems to be ok at least...

EDIT: Also, the txt-file looks like this:

1,3,5,7,9,11,
12,16,20,24,28,32,
33, 34, 35, 36,37,38,
2,14,18,40,44,48,
Artesimo
Profile Joined February 2015
Germany564 Posts
Last Edited: 2015-02-04 13:38:13
February 04 2015 13:33 GMT
#11535
-mods can delete this, hit quote instead of edit, sorry....-
zzdd
Profile Joined December 2010
United States484 Posts
February 04 2015 17:26 GMT
#11536
On February 04 2015 21:22 Cynry wrote:
So I got my array_realloc to work, just changing TAB_TYPE to void and setting the old[i] to null before freeing it. Seems ok with int **, char **, even char ***, and I still can free everything so no apparent leaks for valgrind. Now, of course, it only works if you want a bigger new array, so maybe I'll change its name, but it's good enough for what I wanted to do with it. Which is using it for int **... Still an interesting little exercice ! Thanks to all of those who answered !
And this stackoverflow link was interesting, but what I wanted to do was simpler than that. Good thing, cause I'm not sure I understood most of it ^^
Artesimo : Don't worry about feeling dumb when you start coding. Most of the mistakes we beginners make are quite stupid indeed, but that's the way to learn :D

You might want to look up union.
Nesserev
Profile Blog Joined January 2011
Belgium2760 Posts
February 04 2015 17:56 GMT
#11537
--- Nuked ---
sabas123
Profile Blog Joined December 2010
Netherlands3122 Posts
February 04 2015 22:17 GMT
#11538
I found a good guide about sockets in C.

its quite long but really well written. maby add this to the op?

http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html
The harder it becomes, the more you should focus on the basics.
Artesimo
Profile Joined February 2015
Germany564 Posts
February 04 2015 23:36 GMT
#11539
*sigh* its me again. I cant manage to overload the += -operator accordingly. I have a vector of objects, and now I want to overload the += operator so that

void Videothek::erhalten(string dvdName)
{
dvd.push_back(new DVD(dvdName));
}


gets accomplished with something like this(at least I think thats the idea): dvd += dvdName. I already overloaded the shift operator and looking at templates and explanations for the += operator, it seems easyer but I cant manage to do so. I am not even sure how to deklare the funktion in my header.
is

friend DVD& operator+=(DVD& , string);

what Im looking for/close to it?
Manit0u
Profile Blog Joined August 2004
Poland17550 Posts
Last Edited: 2015-02-05 00:03:51
February 05 2015 00:02 GMT
#11540
On February 05 2015 08:36 Artesimo wrote:
*sigh* its me again. I cant manage to overload the += -operator accordingly. I have a vector of objects, and now I want to overload the += operator so that

void Videothek::erhalten(string dvdName)
{
dvd.push_back(new DVD(dvdName));
}


gets accomplished with something like this(at least I think thats the idea): dvd += dvdName. I already overloaded the shift operator and looking at templates and explanations for the += operator, it seems easyer but I cant manage to do so. I am not even sure how to deklare the funktion in my header.
is

friend DVD& operator+=(DVD& , string);

what Im looking for/close to it?


http://stackoverflow.com/questions/4421706/operator-overloading/4421708#4421708
http://stackoverflow.com/a/4421719/4337069


class X {
X& operator+=(const X& rhs)
{
// actual addition of rhs to *this
return *this;
}
};
inline X operator+(X lhs, const X& rhs)
{
lhs += rhs;
return lhs;
}
Time is precious. Waste it wisely.
Prev 1 575 576 577 578 579 1032 Next
Please log in or register to reply.
Live Events Refresh
Next event in 48m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
SortOf 161
Rex 8
StarCraft: Brood War
Horang2 10436
Sea 6080
Jaedong 894
Stork 352
Larva 350
Soma 292
BeSt 195
ZerO 183
Rush 175
Hyuk 171
[ Show more ]
Mini 164
Zeus 161
Sharp 137
Pusan 126
910 123
EffOrt 112
Shuttle 77
Leta 66
Snow 63
Barracks 54
NotJumperer 47
Hyun 46
sorry 40
Mind 31
soO 29
Shine 26
ToSsGirL 25
JYJ 16
zelot 15
GoRush 15
Movie 13
Noble 11
ajuk12(nOOB) 10
Bale 6
Dota 2
Fuzer 220
NeuroSwarm182
XcaliburYe116
League of Legends
C9.Mang0439
JimRising 390
Counter-Strike
summit1g9681
olofmeister1703
shoxiejesuss868
x6flipin336
edward194
Other Games
B2W.Neo779
Mew2King97
ZerO(Twitch)8
Organizations
Other Games
gamesdonequick719
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 13 non-featured ]
StarCraft 2
• naamasc249
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• iopq 1
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Jankos2713
Upcoming Events
Wardi Open
48m
OSC
1d
Solar vs MaxPax
ByuN vs Krystianer
Spirit vs TBD
OSC
4 days
Korean StarCraft League
4 days
OSC
5 days
OSC
5 days
OSC
6 days
uThermal 2v2 Circuit
6 days
Replay Cast
6 days
Liquipedia Results

Completed

CSL Season 19: Qualifier 2
WardiTV 2025
META Madness #9

Ongoing

C-Race Season 1
IPSL Winter 2025-26
BSL Season 21
eXTREMESLAND 2025
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025

Upcoming

CSL 2025 WINTER (S19)
Escore Tournament S1: W2
Escore Tournament S1: W3
BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
Big Gabe Cup #3
OSC Championship Season 13
Nations Cup 2026
NA Kuram Kup
ESL Pro League Season 23
ESL Pro League Season 23
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.