switch only works with primitives (iirc). Since string is a class, switch won't work with it.
The Big Programming Thread - Page 577
Forum Index > General Forum |
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
Hyrule18982 Posts
switch only works with primitives (iirc). Since string is a class, switch won't work with it. | ||
Eiii
United States2566 Posts
On February 04 2015 03:38 darkness wrote: So yes, that's still a string. I don't care if it was even required to do silly stuff like:
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. | ||
Blisse
Canada3710 Posts
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. | ||
nunez
Norway4003 Posts
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{ 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. | ||
Artesimo
Germany542 Posts
![]() My lotto-pick has only one attribute(lets call it lottonumbers), which is an vector. My set method for the lotto-pick:
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:
and to get the picks contained in the ticket, I use:
In my main I started to test around a little bit:
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
Australia520 Posts
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... 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. | ||
meatpudding
Australia520 Posts
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); | ||
Artesimo
Germany542 Posts
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); 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
Australia520 Posts
On February 04 2015 08:03 Artesimo wrote: 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. | ||
Artesimo
Germany542 Posts
| ||
meatpudding
Australia520 Posts
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. | ||
Artesimo
Germany542 Posts
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
810 Posts
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
Germany542 Posts
Also solved :-) + Show Spoiler + 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
Germany542 Posts
| ||
zzdd
United States484 Posts
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
Belgium2760 Posts
| ||
sabas123
Netherlands3122 Posts
its quite long but really well written. maby add this to the op? http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html | ||
Artesimo
Germany542 Posts
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
what Im looking for/close to it? | ||
Manit0u
Poland17207 Posts
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
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
what Im looking for/close to it? http://stackoverflow.com/questions/4421706/operator-overloading/4421708#4421708 http://stackoverflow.com/a/4421719/4337069
| ||
| ||