• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 22:42
CET 04:42
KST 12:42
  • 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 Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12Intel X Team Liquid Seoul event: Showmatches and Meet the Pros10
Community News
Weekly Cups (Nov 24-30): MaxPax, Clem, herO win2BGE Stara Zagora 2026 announced15[BSL21] Ro.16 Group Stage (C->B->A->D)4Weekly Cups (Nov 17-23): Solar, MaxPax, Clem win3RSL Season 3: RO16 results & RO8 bracket13
StarCraft 2
General
Chinese SC2 server to reopen; live all-star event in Hangzhou Maestros of the Game: Live Finals Preview (RO4) BGE Stara Zagora 2026 announced Weekly Cups (Nov 24-30): MaxPax, Clem, herO win SC2 Proleague Discontinued; SKT, KT, SGK, CJ disband
Tourneys
RSL Offline Finals Info - Dec 13 and 14! Sparkling Tuna Cup - Weekly Open Tournament StarCraft Evolution League (SC Evo Biweekly) Sea Duckling Open (Global, Bronze-Diamond) $5,000+ WardiTV 2025 Championship
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 502 Negative Reinforcement Mutation # 501 Price of Progress Mutation # 500 Fright night Mutation # 499 Chilling Adaptation
Brood War
General
Foreign Brood War BGH Auto Balance -> http://bghmmr.eu/ Data analysis on 70 million replays BW General Discussion MBCGame Torrents
Tourneys
Small VOD Thread 2.0 [Megathread] Daily Proleagues [BSL21] RO16 Group D - Sunday 21:00 CET [BSL21] RO16 Group A - Saturday 21:00 CET
Strategy
Current Meta Game Theory for Starcraft How to stay on top of macro? PvZ map balance
Other Games
General Games
Nintendo Switch Thread Stormgate/Frost Giant Megathread Path of Exile ZeroSpace Megathread The Perfect Game
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 TL Mafia Community Thread
Community
General
US Politics Mega-thread European Politico-economics QA Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread The Big Programming Thread
Fan Clubs
White-Ra Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece Movie Discussion!
Sports
2024 - 2026 Football Thread Formula 1 Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
Where to ask questions and add stream? The Automated Ban List
Blogs
I decided to write a webnov…
DjKniteX
Physical Exertion During Gam…
TrAiDoS
James Bond movies ranking - pa…
Topin
Thanks for the RSL
Hildegard
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1131 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
Hyrule19174 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
Poland17507 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
OSC
22:00
Masters Cup #150: Playoffs
EnDerr vs ForJumy
sebesdes vs Nicoract
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
WinterStarcraft190
RuFF_SC2 185
Nina 99
StarCraft: Brood War
JulyZerg 196
NaDa 78
Leta 77
Noble 54
ggaemo 46
ZergMaN 28
Dota 2
monkeys_forever585
Super Smash Bros
C9.Mang0612
hungrybox219
Heroes of the Storm
Khaldor123
Other Games
tarik_tv11783
summit1g6937
PiGStarcraft281
XaKoH 180
ViBE128
Trikslyr56
Organizations
Other Games
gamesdonequick702
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 11 non-featured ]
StarCraft 2
• Hupsaiya 123
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Upcoming Events
Sparkling Tuna Cup
6h 19m
WardiTV 2025
8h 19m
OSC
11h 19m
IPSL
13h 19m
Bonyth vs KameZerg
BSL 21
16h 19m
Bonyth vs StRyKeR
Tarson vs Dandy
Replay Cast
1d 5h
Wardi Open
1d 8h
StarCraft2.fi
1d 12h
Monday Night Weeklies
1d 13h
Replay Cast
1d 20h
[ Show More ]
WardiTV 2025
2 days
StarCraft2.fi
2 days
PiGosaur Monday
2 days
StarCraft2.fi
3 days
Tenacious Turtle Tussle
3 days
The PondCast
4 days
WardiTV 2025
4 days
StarCraft2.fi
4 days
WardiTV 2025
5 days
StarCraft2.fi
6 days
RSL Revival
6 days
IPSL
6 days
Sziky vs JDConan
Liquipedia Results

Completed

Proleague 2025-12-04
RSL Revival: Season 3
Light HT

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
YSL S2
BSL Season 21
CSCL: Masked Kings S3
Slon Tour Season 2
Acropolis #4 - TS3
WardiTV 2025
META Madness #9
Kuram Kup
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
ESL Pro League S22

Upcoming

BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
Big Gabe Cup #3
RSL Offline Finals
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
eXTREMESLAND 2025
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.