|
You aren't going to corrupt your I/O buffer using C++'s interface to your I/O buffer. That would just be silly. That being said, the easiest way to solve your problem would be to get the entire line of input from the user via cin.getline() and then deal with it once you have the entire thing. Try looking at the stringstream class as a place to store the user's input. It may help you. http://www.cplusplus.com/reference/iostream/stringstream/
|
# ? Nov 24, 2008 04:29 |
|
|
# ? May 18, 2024 09:06 |
|
Pesch posted:You aren't going to corrupt your I/O buffer using C++'s interface to your I/O buffer. That would just be silly. Hmm, what's the difference between "getline(cin, var, delim)", and "cin.getline(var, delim)"? Edit: Also, reading up on the istream classes, it looks like these are mostly meant for extraction of a character array (basic string). Is there a way to use these, or something else I can use, to extract a string array? Cosmopolitan fucked around with this message at 04:55 on Nov 24, 2008 |
# ? Nov 24, 2008 04:34 |
|
Anunnaki posted:I'm sure if I knew what you were talking about, I wouldn't be here asking for help. If cin >> str will get one letter's worth of morse code, and you need to get multiple letters worth of morse code, and loops make things repeat, why not use a loop that repeatedly gets one letter's worth of morse code until you have all the letters? It really doesn't need to be any more complicated than that.
|
# ? Nov 24, 2008 04:57 |
|
So, you can associate more than one thing to a unique key? In the example, mapping numbers of days to months, means you can have "31 days" associated with all the months that have 31 days? Edit - Assuming that's true, can you read a maps keys sequentially?
|
# ? Nov 24, 2008 04:59 |
|
User0015 posted:So, you can associate more than one thing to a unique key? In the example, mapping numbers of days to months, means you can have "31 days" associated with all the months that have 31 days? What are you talking about
|
# ? Nov 24, 2008 05:00 |
|
Avenging Dentist posted:What are you talking about http://www.cppreference.com/wiki/stl/map/start
|
# ? Nov 24, 2008 05:01 |
|
Smackbilly posted:If cin >> str will get one letter's worth of morse code, and you need to get multiple letters worth of morse code, and loops make things repeat, why not use a loop that repeatedly gets one letter's worth of morse code until you have all the letters? It really doesn't need to be any more complicated than that. Because a series of cins, going one character by one, is exactly what I'm trying to avoid. Although I'm running out of steam with this, so I think I might just have to fall back on that anyway.
|
# ? Nov 24, 2008 05:02 |
|
User0015 posted:http://www.cppreference.com/wiki/stl/map/start No. You are talking about a multimap, not a map. I don't know why you're talking about it, but nevertheless, you are. Anunnaki posted:Because a series of cins, going one character by one, is exactly what I'm trying to avoid. Although I'm running out of steam with this, so I think I might just have to fall back on that anyway. Why would you intentionally try to make it more difficult for yourself to tokenize the input??? If you're worried about trailing whitespace loving things up just do the same thing to a stringstream after copying from getline. Avenging Dentist fucked around with this message at 05:11 on Nov 24, 2008 |
# ? Nov 24, 2008 05:05 |
|
User0015 posted:So, you can associate more than one thing to a unique key? In the example, mapping numbers of days to months, means you can have "31 days" associated with all the months that have 31 days? to associate more than one thing to a key you need multimaps. with normal maps you just have one mapping per each key. you can read map's key sequentially. if you use map<T,E>::iterator then such iterator returns reference to a pair<T,E> where T is key and E is entry. also all entries in map are sorted based on key values. You can provide custom comparator function to order differently. vvvvvvv you should be able to just like i described with map, but I am not sure what exactly it uses for each entry, as I never used them. perhaps its just again pair<T,E> where keys are just repeated? I am not sure. hexadecimal fucked around with this message at 05:11 on Nov 24, 2008 |
# ? Nov 24, 2008 05:08 |
|
Avenging Dentist posted:No. You are talking about a multimap, not a map. I don't know why you're talking about it, but nevertheless, you are. That's surprisingly helpful. Can you sequentially read a multimap? ^^^^^^^ Thank you!
|
# ? Nov 24, 2008 05:09 |
|
User0015 posted:That's surprisingly helpful. check out this #define I have found somewhere and have been using for a while. It makes iterating through STL containers a lot easier. code:
|
# ? Nov 24, 2008 05:13 |
|
hexadecimal posted:check out this #define I have found somewhere and have been using for a while. It makes iterating through STL containers a lot easier. Stop writing awful #defines, jesus christ. Boost.Foreach has already soundly trounced that attempt anyway, and C++0x will have range-for rendering all of this moot.
|
# ? Nov 24, 2008 05:15 |
|
Avenging Dentist posted:Stop writing awful #defines, jesus christ. Boost.Foreach has already soundly trounced that attempt anyway, and C++0x will have range-for rendering all of this moot. well what if you don't want to use boost, or can't? its not an awful #define its very useful, at least for me
|
# ? Nov 24, 2008 05:18 |
|
hexadecimal posted:well what if you don't want to use boost, or can't? its not an awful #define its very useful, at least for me See what happens when either _it or _l is a computed expression (e.g., FOREACH(i++, my_map)). It's a pretty awful macro.
|
# ? Nov 24, 2008 05:19 |
|
Your #define is beyond me. That's ok though. I had an idea and it turns out multimaps might be helpful.
|
# ? Nov 24, 2008 05:20 |
|
floWenoL posted:See what happens when either _it or _l is a computed expression (e.g., FOREACH(i++, my_map)). why would you ever want to do that? and what is a better macro to take care of that case?
|
# ? Nov 24, 2008 05:20 |
|
hexadecimal posted:well what if you don't want to use boost, or can't? its not an awful #define its very useful, at least for me If you don't want to use Boost, you're probably an idiot. The only reason you'd be banned from using Boost is if your school/job has specific coding standards precluding it, and they're probably not going to like you polluting the preprocessor namespace with a bunch of extra symbols that can cause spurious substitutions. Besides that, your #define obscures compiler errors, behaves differently from the C++0x range-for standard, and uses post-increment on the iterator. Oh, also it relies on a nonstandard compiler intrinsic (__typeof) with no appropriate fallback for compilers that don't support it (such as MSVC, which you may know as the primary compiler for Windows). There's a reason that people use Boost and not their own homegrown hacks when attempting to hack in language features.
|
# ? Nov 24, 2008 05:33 |
|
Avenging Dentist posted:If you don't want to use Boost, you're probably an idiot. He calls people "duder", so I think there's no "probably" about it.
|
# ? Nov 24, 2008 05:43 |
|
I got bored so I made a solution in plain C (possibly with some GNUisms). In C++, instead of using the read line+strtok approach, you'd either read a line then split it using something like stringSplit, or you'd just read exactly one token at a time from the input. You might also use a map or a vector or something instead of a big array for the table of Morse codes, but whatever. Now I kind of want to try to golf the decoding problem. Edit: fixed the constants table, because checking my work the first time would be the pansy way to program ShoulderDaemon fucked around with this message at 06:16 on Nov 24, 2008 |
# ? Nov 24, 2008 05:45 |
|
Real men use Boost.Spirit and write an EBNF grammar to parse the morse code.
|
# ? Nov 24, 2008 05:46 |
|
Avenging Dentist posted:Real men use Boost.Spirit and write an EBNF grammar to parse the morse code. wow, it sounds really cool. Is it a lot better to use than lex/yacc? vvvvvv so template meta programing is hacking templates in C++ to generate some C++ code at compile-time that makes it faster than equivalent code that does more work at run-time? It sounds pretty cool, but from what little reading i have done in past 10 minutes, the only advantage over macros seems to be that you can do recursion using this? Can you point me to any good sources to read for this? So far I have just been reading the reference on the Boost.spirit page. hexadecimal fucked around with this message at 06:13 on Nov 24, 2008 |
# ? Nov 24, 2008 06:00 |
|
hexadecimal posted:wow, it sounds really cool. Is it a lot better to use than lex/yacc? Axiom 1: Template metaprogramming is loving awesome. Axiom 2: Regular programming is "ok" Corollary 1: Let X = {x | x is a program}, Y = {y | y is a template metaprogram}, Z = X-Y. Then ∀y∈Y,∀z∈Z (y>z) Signs point to yes.
|
# ? Nov 24, 2008 06:08 |
|
Avenging Dentist posted:Real men use Boost.Spirit and write an EBNF grammar to parse the morse code. Using a tool to build your state table seems like cheating. code:
ShoulderDaemon fucked around with this message at 04:29 on Feb 18, 2009 |
# ? Nov 24, 2008 06:49 |
|
ShoulderDaemon posted:Using a tool to build your state table seems like cheating. This is why I love the CoC.
|
# ? Nov 24, 2008 07:57 |
|
ShoulderDaemon posted:
I'm not sure but I think I hate you.
|
# ? Nov 24, 2008 08:01 |
|
ShoulderDaemon posted:Using a tool to build your state table seems like cheating. Yeah, but you still used a tool to build your state table. That tool... is you.
|
# ? Nov 24, 2008 08:02 |
|
Realtalkin', I wish I knew how to build parse tables manually. Maybe one of these days I should get a book on lexing/parsing.
|
# ? Nov 24, 2008 08:07 |
|
Avenging Dentist posted:Realtalkin', I wish I knew how to build parse tables manually. Maybe one of these days I should get a book on lexing/parsing. After looking the poo poo that yacc and lex spit out I don't relish the idea.
|
# ? Nov 24, 2008 08:09 |
|
MEAT TREAT posted:After looking the poo poo that yacc and lex spit out I don't relish the idea. Tools tend to push out some of the worst looking state machines; it's possible to build readable state machines that aren't any slower without too much trouble. I'm not a good example of that, but I've heard about it happening from a somewhat reliable source who was probably not joking at the time. Seriously, though, most of why mine looks terrible is because of the stupid gimmick I'm using where the index of a state maps directly to the character that should be printed if you terminate in that state. This neither saves memory nor speeds up execution, but I think it's kind of funny because it makes it almost impossible to read unless you have an ASCII table handy. Anyway, Morse code doesn't really lend itself to a compact representation, which is sort of unfortunate because any state table is going to end up with icky holes in it. It actually gets slightly nicer if you add the latin character encodings and other extensions to the code, because they fill in some of the holes, but I didn't want to deal with those characters in C. Also, they would have made my stupid gimmick require a bunch of rows of nothing but {0 ,0 } which just looks bad. I suppose I could have filled most of those elements with random garbage because they'd never be reached, or I could store a huffman-encoded version of the table and decode it at runtime, but I'm not currently that bored.
|
# ? Nov 24, 2008 08:31 |
|
Avenging Dentist posted:If you don't want to use Boost, you're probably an idiot. This is extremely naive fyi and I'm not certain if you're a professional programmer why you can't imagine situations in which certain libraries and language features aren't available, but you're making yourself look like either a pompous moron or a troll. C++ was designed to be a multiparadigm language, and part of the reason that it's still so widely used is that it supports many features in many different ways, and there's often no One True Solution to a particular problem.
|
# ? Nov 24, 2008 12:44 |
|
I hate to break it to you, but a good dozen libraries from Boost are in TR1 and many more are a part of TR2, so unless you're seriously arguing against the use of the standard library, you're just being stupid. More to the point though, dismissing Boost as a whole is profoundly retarded because Boost is not a single unit. There's a world of difference between disallowing Boost.Spirit and Boost.Smart_Pointer. Even more to the point, hexadecimal's code in this thread has been consistently awful and he needed to be taken to task for it. Still more to the point, has there ever once been a time that I've posted something not partially intended as a flame?
|
# ? Nov 24, 2008 13:20 |
|
Avenging Dentist posted:Still more to the point, has there ever once been a time that I've posted something not partially intended as a flame?
|
# ? Nov 24, 2008 14:28 |
|
i made my own DFA (about 15 states) table one time for an acm problem, if that counts. It involved a lot of coffee and scratch paper. actual C++ question: Do C++ compilers optimize tail recursive functions into iterative ones? hexadecimal fucked around with this message at 14:53 on Nov 24, 2008 |
# ? Nov 24, 2008 14:37 |
|
hexadecimal posted:actual C++ question: Do C++ compilers optimize tail recursive functions into iterative ones? gcc enables -foptimize-sibling-calls at -O2 and higher; I don't know about VC++. Last time I heard, gcc's TCO is unnecessarily conservative: GCC basically doesn't do the sort of analysis which lets you selectively enable the optimization, so all sorts of code can just automatically disable it. It's not the sort of thing you want to rely on unless you do. rjmccall fucked around with this message at 15:28 on Nov 24, 2008 |
# ? Nov 24, 2008 15:19 |
|
Avenging Dentist posted:Realtalkin', I wish I knew how to build parse tables manually. Maybe one of these days I should get a book on lexing/parsing. I suggest some reading on the lemon parser. Quite educational.
|
# ? Nov 24, 2008 15:41 |
|
This is a bit of a stupid newbie question - I haven't done C++ for a year and a half and even then I was basically at a casual level. I was also then doing it on a *nix system and now I'm being thrown into working with the console. I'm trying to declare a two-dimensional array of pointers. I have a "Map" class, which holds a Tile **tileptr, and obviously a "Tile" class. What I want is for each element in the array to be a pointer to a separate Tile - I'm aware that it's not really the tic-tac-toe board analogy people give and 2-d arrays are just fancy memory management, but representing it this way makes it a lot easier for this particular purpose. Since I'm dumb with pointer syntax now, I've done the following: code:
However, when I execute this, the program automatically terminates. I've previously told it to draw a 25x25 grid as well and with this function commented out in the Map constructor it works perfectly, but when included I get nothing. I've tried running it in the command prompt, no go. Is this because I haven't bothered writing a destructor yet and deallocated all those Tiles, or is there something else to blame?
|
# ? Nov 24, 2008 15:59 |
|
Srebrenica Surprise posted:This is a bit of a stupid newbie question - I haven't done C++ for a year and a half and even then I was basically at a casual level. I was also then doing it on a *nix system and now I'm being thrown into working with the console. you have segmentation fault there, because xsize and ysize are not equal. tileptr is of size xsize, but in your loop you go from 0 to ysize, so if y size is bigger than xsize you definitely get seg fault. in your loop do i < xsize instead of ysize. also in your 2d array each element is not pointer to Tile. Use *** for that. or you can use 2d vector. like vector<vector<Tile*> >( ysize, vector<Tile*>(xsize,NULL ) ); (USER WAS PUT ON PROBATION FOR THIS POST)
|
# ? Nov 24, 2008 17:39 |
|
hexadecimal posted:actual C++ question: Do C++ compilers optimize tail recursive functions into iterative ones? Some do, under certain circumstances, but I don't think anything is guaranteed. I've seen Visual C++ optimize tail recursion into a loop (/O1 and above). It sounds like GCC does it, from rjmccall's post, and I'd assume that Codewarrior does not mostly because it doesn't seem to optimize anything at all.
|
# ? Nov 24, 2008 18:01 |
|
If anyone was up for it, I had some questions I'd like to ask via e-mail. That, or start my own thread. I don't think a thread really warrants my questions, since they'd mostly be single question/comment types.
|
# ? Nov 24, 2008 21:55 |
|
|
# ? May 18, 2024 09:06 |
|
EDIT: I am doing something wrong here, carry on I just got some weird results trying to do a really simple benchmark of qsort versus std::sort. I use the following code: code:
Does anyone know what's going on with std::sort? Is it just that GCC 3.4.4 and visual c++ express suck at optimizing, or am I doing something wrong here? Painless fucked around with this message at 22:29 on Nov 24, 2008 |
# ? Nov 24, 2008 22:09 |