KakodaimonosCode fondlerHelping the 1% get richerRegistered Userregular
edited April 2010
The fun part of shared memory is not the allocation and file layout, it's the IPC/synchronization. There's ways to deal with it, but it's a pain in the ass to do right and test it.
Kakodaimonos on
0
GnomeTankWhat the what?Portland, OregonRegistered Userregular
edited April 2010
Some guy already wrote a set of STL adapters for shared memory, that is supposedly cross platform. I just happen to stumble on it as I was looking at shared memory stuff like 15 minutes ago. Not sure what level of completeness it is, but it seemed to support the idea we're discussing here.
GnomeTankWhat the what?Portland, OregonRegistered Userregular
edited April 2010
Speaking of Boost, here's some fun Boost pre-processor meta programming iteration voodoo for you. Once I figured out how to use the meta programming iteration library, I hatched some evil, evil ideas on how to use it (this is pretty benign):
The fun part of shared memory is not the allocation and file layout, it's the IPC/synchronization. There's ways to deal with it, but it's a pain in the ass to do right and test it.
Oh yes, at least windows defines certain nice mechanisms for doing synch with arbitrary processes. This was how I set it up:
Basically when you joined the session you duped a handle to your process and put it in the lookup table (after duping and acquiring the lock for that table for your own use of course) for the entire set of processes (so given A & B in the session already, C would dup handles specifically for A & B, in turn A & B would use those handles to dup their handles for C). This takes a thread (or at least a periodic timer) per-process.
With that in place you have the ability to dup any handle from any process into any other process. A third process can even do this by proxy. Rediculously insecure of course, but if you're all under the same user, meh.
Then there was the handle class, it would keep track of all the handles and automatically acquire any needed handles into your process as you used them. This gives you everything except critical sections. Doing user level critical sections fast and across processes without them turning into useless spinlocks is hard, can't remember if I ever came up with anything good or just stuck to mutexes. Crashing is also something to consider as well, the behaviour of kernel synchronization objects is well defined when threads or processes terminate abnormally.
If you needed a pipe you could create an anonomous pipe and anyone could pick up the read end and use it. I was trying to figure our a way to do real RPC of some kind, thought about queueing to per-processor thread pools.
I should see if I can find it... I lost a bunch of stuff when I mistakenly wiped the wrong partition a while back but it may be in a backup somewhere.
Speaking of Boost, here's some fun Boost pre-processor meta programming iteration voodoo for you. Once I figured out how to use the meta programming iteration library, I hatched some evil, evil ideas on how to use it (this is pretty benign):
GnomeTankWhat the what?Portland, OregonRegistered Userregular
edited April 2010
Yah, luabind:call_function is varargs, but since it's not possible to pass varargs to another varargs function, I had to make a bunch of template functions with varying argument amounts. Doing this by hand wasn't happening, so I finally bit the bullet and learned the boost iteration stuff. I am glad I did, it's a very powerful library.
e: Also, by looking over my own code again, I noticed two functions I forgot to make const...getScriptState and the pure virtual getErrorPrefix.
Linux has two ways to do shared memory. The more modern and popular way is to use the shm_open() syscall (which takes a "file" name) and then map the resulting file descriptor via mmap(). If /dev/shm is mounted, then shm files will be created there and can be treated like any other file.
It's all well and good until you realize that you can't resize the area without having a way to indicate to everyone that's mmap()'ed the area that they need to re-map it. Very messy.
So I'm still having trouble with more or less the same set of installations as I was having a month ago. It takes me about 20 minutes to be reduced to a soulless wreck, though fortunately it wears off fairly quickly once I stop.
I've gotten all of the normal dependencies handled, I think, but now I'm vainly trying to get Ubuntu and OpenGL to play nice. At least, I think that's what I'm trying to do. Maybe I'm trying to get NVidia drivers to work on Ubuntu, or with OpenGL, or maybe I need to suffer enough to appease a deity that has it out for me. Should I be out looking for lions to strangle instead?
Edit: Dammit, what the hell kind of sucky operating system crashes the process when you try to activate a built-in driver? It doesn't even take the operating system down in a spectacular ball of flame, it just spits out an error message, suggests that you file a bug report, and takes you back to the driver list.
So I'm still having trouble with more or less the same set of installations as I was having a month ago. It takes me about 20 minutes to be reduced to a soulless wreck, though fortunately it wears off fairly quickly once I stop.
I've gotten all of the normal dependencies handled, I think, but now I'm vainly trying to get Ubuntu and OpenGL to play nice. At least, I think that's what I'm trying to do. Maybe I'm trying to get NVidia drivers to work on Ubuntu, or with OpenGL, or maybe I need to suffer enough to appease a deity that has it out for me. Should I be out looking for lions to strangle instead?
Edit: Dammit, what the hell kind of sucky operating system crashes the process when you try to activate a built-in driver? It doesn't even take the operating system down in a spectacular ball of flame, it just spits out an error message, suggests that you file a bug report, and takes you back to the driver list.
Nvidia's drivers actually replace parts of the default OpenGL libraries with their own implementation (on Debian/Ubuntu it's technically a diversion). You may want to check dmesg or Xorg.0.log to see if there's any extra information about the crash if you aren't doing any logging to see what the problematic application is up to.
Does that system at least run glxgears?
Barrakketh on
Rollers are red, chargers are blue....omae wa mou shindeiru
0
GnomeTankWhat the what?Portland, OregonRegistered Userregular
edited April 2010
Most Linux distros also have a way to set which OpenGL implementation you are using, so make sure it's not set to Mesa. (Off the top of my head, on Gentoo, it's 'eselect opengl nvidia' to switch to NVidia. Not sure on other distros).
This book is really sucking in some places. Lke telling you about stuff but the example it gives is so totally useless that Im still not sure what Im supposed to be using switch for.
The example:
#include <iostream>
using namespace std;
int main()
{
char letter;
cout << "Enter any a-z character: ";
cin >> letter;
switch(letter)
{
case 'a' : cout << "Letter \'a\' found\n"; break;
case 'b' : cout << "Letter \'b\' found\n"; break;
case 'c' : cout << "Letter \'a\' found\n"; break;
default : cout << "Letter is not a, b, or c\n";
}
return 0;
}
Anyone reccomend any online guides to help out with this? I was looking at some stuff, dunno whats decent and whats crap though. I tried an exercise I found on one forum but ended up using a ton of else if's, and Im sure theres an easier way with switch, I just dont know what it is.
This book is really sucking in some places. Lke telling you about stuff but the example it gives is so totally useless that Im still not sure what Im supposed to be using switch for.
The example:
#include <iostream>
using namespace std;
int main()
{
char letter;
cout << "Enter any a-z character: ";
cin >> letter;
switch(letter)
{
case 'a' : cout << "Letter \'a\' found\n"; break;
case 'b' : cout << "Letter \'b\' found\n"; break;
case 'c' : cout << "Letter \'a\' found\n"; break;
default : cout << "Letter is not a, b, or c\n";
}
return 0;
}
Anyone reccomend any online guides to help out with this? I was looking at some stuff, dunno whats decent and whats crap though. I tried an exercise I found on one forum but ended up using a ton of else if's, and Im sure theres an easier way with switch, I just dont know what it is.
Switches are used either because:
1) the person writing it thinks it looks better than a bunch of if-elses
2) when you have a situation where dropthough works well, such as checking the number of arguments sent to the main function and applying them. Then you write a switch like:
var numArgs = args.length;
switch(numArgs)
{
case 4: doArg4();
case 3: doArg3();
case 2: doArg2();
case 1: doArg1();
default : print("Need args dummy");
}
Then since the cases don't have break at the end, if you get 4 args it runs doArg4(), then doArg3(), etc etc
Yontouryu on
0
GnomeTankWhat the what?Portland, OregonRegistered Userregular
edited April 2010
switch is just way to write 'if' that is less verbose, when constants are involved. That's it. Plus it has the nice feature of "fall through". Let me show you:
switch(someValue) {
case 1:
case 2:
doSomething();
break;
}
GnomeTankWhat the what?Portland, OregonRegistered Userregular
edited April 2010
Also note that in C# (I know you're doing C++, but I think this is important to point out for anyone else reading), what Yontouryu showed is not possible, it will cause a compile error. You can't fall past the end of a case statement in C#. You CAN still do what I showed though, which is case stacking.
Another good use might be if for some reason you have some sort of single state variable that controls program flow, rather than a bunch of flags. That way, you can just line up each option in a neat row, rather than resorting to a nasty if chain for a bunch of equally valid alternatives.
Also note that in C# (I know you're doing C++, but I think this is important to point out for anyone else reading), what Yontouryu showed is not possible, it will cause a compile error. You can't fall past the end of a case statement in C#. You CAN still do what I showed though, which is case stacking.
Oh god that seems horrible. Thank goodness I don't use C#.
Yontouryu on
0
GnomeTankWhat the what?Portland, OregonRegistered Userregular
Also note that in C# (I know you're doing C++, but I think this is important to point out for anyone else reading), what Yontouryu showed is not possible, it will cause a compile error. You can't fall past the end of a case statement in C#. You CAN still do what I showed though, which is case stacking.
Oh god that seems horrible. Thank goodness I don't use C#.
Actually, for 99% of users, it's a good thing. Case drop through is a huge, huge source of hard to track down bugs in C/C++. For the 1% of cases where it's needed, you have if/else to deal with it.
A bunch of studies were done on this, and case pass through was like #3 on the list of C++ gotches, right behind forgetting to free allocated memory.
Also note that in C# (I know you're doing C++, but I think this is important to point out for anyone else reading), what Yontouryu showed is not possible, it will cause a compile error. You can't fall past the end of a case statement in C#. You CAN still do what I showed though, which is case stacking.
Oh god that seems horrible. Thank goodness I don't use C#.
Actually, for 99% of users, it's a good thing. Case drop through is a huge, huge source of hard to track down bugs in C/C++. For the 1% of cases where it's needed, you have if/else to deal with it.
A bunch of studies were done on this, and case pass through was like #3 on the list of C++ gotches, right behind forgetting to free allocated memory.
I will concede that case drop through can be a bitch of a thing to debug, but letting you use goto seems almost just as bad. At least with regular case drop though you only have to look at the first case and work your way down to figure out what is going on, compared even to the C# example where with goto control flow goes up from case 2 and 3 then back down.
Also note that in C# (I know you're doing C++, but I think this is important to point out for anyone else reading), what Yontouryu showed is not possible, it will cause a compile error. You can't fall past the end of a case statement in C#. You CAN still do what I showed though, which is case stacking.
Oh god that seems horrible. Thank goodness I don't use C#.
Actually, for 99% of users, it's a good thing. Case drop through is a huge, huge source of hard to track down bugs in C/C++. For the 1% of cases where it's needed, you have if/else to deal with it.
A bunch of studies were done on this, and case pass through was like #3 on the list of C++ gotches, right behind forgetting to free allocated memory.
I assume that going out of bounds on an array of pointers is first on the list?
jothki on
0
GnomeTankWhat the what?Portland, OregonRegistered Userregular
Also note that in C# (I know you're doing C++, but I think this is important to point out for anyone else reading), what Yontouryu showed is not possible, it will cause a compile error. You can't fall past the end of a case statement in C#. You CAN still do what I showed though, which is case stacking.
Oh god that seems horrible. Thank goodness I don't use C#.
Actually, for 99% of users, it's a good thing. Case drop through is a huge, huge source of hard to track down bugs in C/C++. For the 1% of cases where it's needed, you have if/else to deal with it.
A bunch of studies were done on this, and case pass through was like #3 on the list of C++ gotches, right behind forgetting to free allocated memory.
I assume that going out of bounds on an array of pointers is first on the list?
I think array out of bounds was #2, forgetting to free memory was #1.
Yontouryu: I've never once, in my entire professional C# career (8 years) used, or seen anyone use, the goto case, gotodefault stuff. In fact, if I did see one of my developers using it, I would tell them to change it, because it's horrible.
Also note that in C# (I know you're doing C++, but I think this is important to point out for anyone else reading), what Yontouryu showed is not possible, it will cause a compile error. You can't fall past the end of a case statement in C#. You CAN still do what I showed though, which is case stacking.
Oh god that seems horrible. Thank goodness I don't use C#.
Actually, for 99% of users, it's a good thing. Case drop through is a huge, huge source of hard to track down bugs in C/C++. For the 1% of cases where it's needed, you have if/else to deal with it.
A bunch of studies were done on this, and case pass through was like #3 on the list of C++ gotches, right behind forgetting to free allocated memory.
I assume that going out of bounds on an array of pointers is first on the list?
I think array out of bounds was #2, forgetting to free memory was #1.
Yontouryu: I've never once, in my entire professional C# career (8 years) used, or seen anyone use, the goto case, gotodefault stuff. In fact, if I did see one of my developers using it, I would tell them to change it, because it's horrible.
This book is really sucking in some places. Lke telling you about stuff but the example it gives is so totally useless that Im still not sure what Im supposed to be using switch for.
The example:
#include <iostream>
using namespace std;
int main()
{
char letter;
cout << "Enter any a-z character: ";
cin >> letter;
switch(letter)
{
case 'a' : cout << "Letter \'a\' found\n"; break;
case 'b' : cout << "Letter \'b\' found\n"; break;
case 'c' : cout << "Letter \'a\' found\n"; break;
default : cout << "Letter is not a, b, or c\n";
}
return 0;
}
Anyone reccomend any online guides to help out with this? I was looking at some stuff, dunno whats decent and whats crap though. I tried an exercise I found on one forum but ended up using a ton of else if's, and Im sure theres an easier way with switch, I just dont know what it is.
That's a pretty horrid example to show the use of a switch and/or input.
bowen on
not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
0
GnomeTankWhat the what?Portland, OregonRegistered Userregular
edited April 2010
I've found that 90% of programming books have shit examples. I don't think it's the authors fault really, I just think that it's difficult sometimes to come up with small, concise, "real world" examples of how to use things. Since real world software is rarely small, or concise.
#include <iostream>
using namespace std;
int main()
{
int score;
cout<<"What did you score on your test? \n";
cin>>score;
if (score==100)
{
cout<<"Congradulations, you got it!\n";
}
else if((score>=90) && (score<100))
{
cout<<"You got an A! Good job!\n";
}
else if((score>=80) && (score<90))
{
cout<<"You got a B! Pretty good\n";
}
else if((score>=70) && (score<80))
{
cout<<"You got a C! Not bad\n";
}
else if((score>=60) && (score<70))
{
cout<<"You got a D! Keep studying!\n";
}
else if(score<60)
{
cout<<"You fail, you lose, you get nothing! Good day sir!\n";
}
return 0;
}
Im messing around with a switch, but how do I tell it to chose a case where ( (score>=80) && (score<90) ) ?
CangoFett on
0
GnomeTankWhat the what?Portland, OregonRegistered Userregular
edited April 2010
You don't, that's not what switch is for...unless you want to write:
case 80:
case 81:
case 82:
etc.
Some newer languages actually support the "80..90" syntax (meaning 80 to 90), but I don't think C++ does (in fact, I am almost sure it doesn't).
So I'm still having trouble with more or less the same set of installations as I was having a month ago. It takes me about 20 minutes to be reduced to a soulless wreck, though fortunately it wears off fairly quickly once I stop.
I've gotten all of the normal dependencies handled, I think, but now I'm vainly trying to get Ubuntu and OpenGL to play nice. At least, I think that's what I'm trying to do. Maybe I'm trying to get NVidia drivers to work on Ubuntu, or with OpenGL, or maybe I need to suffer enough to appease a deity that has it out for me. Should I be out looking for lions to strangle instead?
Edit: Dammit, what the hell kind of sucky operating system crashes the process when you try to activate a built-in driver? It doesn't even take the operating system down in a spectacular ball of flame, it just spits out an error message, suggests that you file a bug report, and takes you back to the driver list.
Nvidia's drivers actually replace parts of the default OpenGL libraries with their own implementation (on Debian/Ubuntu it's technically a diversion). You may want to check dmesg or Xorg.0.log to see if there's any extra information about the crash if you aren't doing any logging to see what the problematic application is up to.
Does that system at least run glxgears?
Nope, at least not by 'default'. I think I managed to hack it to work at some point, but that solution didn't seem ideal, and didn't fix my problem anyway.
I should probably take this to the Unix thread, though.
If the program responds to different ranges of inputs, ie 10-20, 30-40, 50-60 ... for a long set of ranges, dont use switch
But if there are several different respones for seperate inupts, ie january, feburary, march... then use the switch
right?
Yah, sort of. There is really no hard, fast rule. It's really just about program readability. If you have a huge if/else block you think you can write more clean using switch, that's what you do.
switch(score/10) {
case 10:
printf("GOOD JOB 100\n");
break;
case 9:
printf("A\n");
break;
case 8:
printf("B\n");
break;
case 7:
printf("C\n");
break;
case 6:
printf("D\n");
break;
default:
printf("YOU FAIL.\n");
}
But I'm a dirty cheater.
Edit: Of course, god help you if you get above 100%. Being clever can get you bit. 8-)
End on
I wish that someway, somehow, that I could save every one of us
0
GnomeTankWhat the what?Portland, OregonRegistered Userregular
edited April 2010
You'd probably want to use modulo instead, unless you are 100% sure every grade is going to be a multiple of 10. If someone got a 97, you're example would give them the wrong grade (it would round up to 100). Unless you cast the result to float and floored it.
You'd probably want to use modulo instead, unless you are 100% sure every grade is going to be a multiple of 10. If someone got a 97, you're example would give them the wrong grade (it would round up to 100). Unless you cast the result to float and floored it.
Posts
#if !BOOST_PP_IS_ITERATING #ifndef __H_SCRIPTABLE_H__ #define __H_SCRIPTABLE_H__ namespace Tachyon { class ScriptingService; class Scriptable { friend class ScriptingService; public: Scriptable(); virtual ~Scriptable(); virtual const std::string& getErrorPrefix() = 0; protected: lua_State* getScriptState() { return _state; } void loadScript(const std::string& file); #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, 5, "Scriptable.h", 1)) #include BOOST_PP_ITERATE() private: void setScriptState(lua_State* state) { _state = state; } lua_State* _state; }; } #endif // __H_SCRIPTABLE_H__ #elif BOOST_PP_ITERATION_FLAGS() == 1 #include <boost/preprocessor/enum_params.hpp> #define SCRIPTABLE_DEFINE_ARGS(z, n, data) const A##n & a##n #define SCRIPTABLE_USE_ARGS(z, n, data) a##n #if BOOST_PP_ITERATION() template<BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A)> #endif void callScript(const std::string& name BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM(BOOST_PP_ITERATION(), SCRIPTABLE_DEFINE_ARGS, _) ) { try { luabind::object func = luabind::globals(_state)[name.c_str()]; if(func.is_valid()) { luabind::call_function<void>(func BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM(BOOST_PP_ITERATION(), SCRIPTABLE_USE_ARGS, _) ); } } catch(luabind::error& e) { luabind::object msg(luabind::from_stack(e.state(), -1)); std::stringstream s; s << msg; //TODO: Do something with this later. } } #undef SCRIPTABLE_DEFINE_ARGS #undef SCRIPTABLE_USE_ARGS #endifOh yes, at least windows defines certain nice mechanisms for doing synch with arbitrary processes. This was how I set it up:
Basically when you joined the session you duped a handle to your process and put it in the lookup table (after duping and acquiring the lock for that table for your own use of course) for the entire set of processes (so given A & B in the session already, C would dup handles specifically for A & B, in turn A & B would use those handles to dup their handles for C). This takes a thread (or at least a periodic timer) per-process.
With that in place you have the ability to dup any handle from any process into any other process. A third process can even do this by proxy. Rediculously insecure of course, but if you're all under the same user, meh.
Then there was the handle class, it would keep track of all the handles and automatically acquire any needed handles into your process as you used them. This gives you everything except critical sections. Doing user level critical sections fast and across processes without them turning into useless spinlocks is hard, can't remember if I ever came up with anything good or just stuck to mutexes. Crashing is also something to consider as well, the behaviour of kernel synchronization objects is well defined when threads or processes terminate abnormally.
If you needed a pipe you could create an anonomous pipe and anyone could pick up the read end and use it. I was trying to figure our a way to do real RPC of some kind, thought about queueing to per-processor thread pools.
I should see if I can find it... I lost a bunch of stuff when I mistakenly wiped the wrong partition a while back but it may be in a backup somewhere.
Huh, I was actually looking for something like this recently. Could never figure out how to iterate a vararg macro, forgot that boost probably could.
edit: spoilered for huge
e: Also, by looking over my own code again, I noticed two functions I forgot to make const...getScriptState and the pure virtual getErrorPrefix.
It's all well and good until you realize that you can't resize the area without having a way to indicate to everyone that's mmap()'ed the area that they need to re-map it. Very messy.
SE++ Forum Battle Archive
I've gotten all of the normal dependencies handled, I think, but now I'm vainly trying to get Ubuntu and OpenGL to play nice. At least, I think that's what I'm trying to do. Maybe I'm trying to get NVidia drivers to work on Ubuntu, or with OpenGL, or maybe I need to suffer enough to appease a deity that has it out for me. Should I be out looking for lions to strangle instead?
Edit: Dammit, what the hell kind of sucky operating system crashes the process when you try to activate a built-in driver? It doesn't even take the operating system down in a spectacular ball of flame, it just spits out an error message, suggests that you file a bug report, and takes you back to the driver list.
Nvidia's drivers actually replace parts of the default OpenGL libraries with their own implementation (on Debian/Ubuntu it's technically a diversion). You may want to check dmesg or Xorg.0.log to see if there's any extra information about the crash if you aren't doing any logging to see what the problematic application is up to.
Does that system at least run glxgears?
The example:
#include <iostream> using namespace std; int main() { char letter; cout << "Enter any a-z character: "; cin >> letter; switch(letter) { case 'a' : cout << "Letter \'a\' found\n"; break; case 'b' : cout << "Letter \'b\' found\n"; break; case 'c' : cout << "Letter \'a\' found\n"; break; default : cout << "Letter is not a, b, or c\n"; } return 0; }Anyone reccomend any online guides to help out with this? I was looking at some stuff, dunno whats decent and whats crap though. I tried an exercise I found on one forum but ended up using a ton of else if's, and Im sure theres an easier way with switch, I just dont know what it is.
if ...
else if ...
else if ...
else if ...
else if ...
[...]
1) the person writing it thinks it looks better than a bunch of if-elses
2) when you have a situation where dropthough works well, such as checking the number of arguments sent to the main function and applying them. Then you write a switch like:
var numArgs = args.length; switch(numArgs) { case 4: doArg4(); case 3: doArg3(); case 2: doArg2(); case 1: doArg1(); default : print("Need args dummy"); }Then since the cases don't have break at the end, if you get 4 args it runs doArg4(), then doArg3(), etc etcswitch(someValue) { case 1: case 2: doSomething(); break; }That's equivalent to writing:
if(someValue == 1 || someValue == 2) { doSomething(); }When you have a lot of values to rustle through like that, switch is a much less verbose way to do it.
In addition, switch has the 'default' case, which is equivalent to a dangling 'else'.
e: Jesus, we all apparently wanted to answer that one.
Actually, for 99% of users, it's a good thing. Case drop through is a huge, huge source of hard to track down bugs in C/C++. For the 1% of cases where it's needed, you have if/else to deal with it.
A bunch of studies were done on this, and case pass through was like #3 on the list of C++ gotches, right behind forgetting to free allocated memory.
I assume that going out of bounds on an array of pointers is first on the list?
I think array out of bounds was #2, forgetting to free memory was #1.
Yontouryu: I've never once, in my entire professional C# career (8 years) used, or seen anyone use, the goto case, gotodefault stuff. In fact, if I did see one of my developers using it, I would tell them to change it, because it's horrible.
That's a pretty horrid example to show the use of a switch and/or input.
#include <iostream> using namespace std; int main() { int score; cout<<"What did you score on your test? \n"; cin>>score; if (score==100) { cout<<"Congradulations, you got it!\n"; } else if((score>=90) && (score<100)) { cout<<"You got an A! Good job!\n"; } else if((score>=80) && (score<90)) { cout<<"You got a B! Pretty good\n"; } else if((score>=70) && (score<80)) { cout<<"You got a C! Not bad\n"; } else if((score>=60) && (score<70)) { cout<<"You got a D! Keep studying!\n"; } else if(score<60) { cout<<"You fail, you lose, you get nothing! Good day sir!\n"; } return 0; }Im messing around with a switch, but how do I tell it to chose a case where ( (score>=80) && (score<90) ) ?
case 80:
case 81:
case 82:
etc.
Some newer languages actually support the "80..90" syntax (meaning 80 to 90), but I don't think C++ does (in fact, I am almost sure it doesn't).
case 80:
case 81:
case 82:
...
case 89:
Edit: CURSES!
Nope, at least not by 'default'. I think I managed to hack it to work at some point, but that solution didn't seem ideal, and didn't fix my problem anyway.
I should probably take this to the Unix thread, though.
If the program responds to different ranges of inputs, ie 10-20, 30-40, 50-60 ... for a long set of ranges, dont use switch
But if there are several different respones for seperate inupts, ie january, feburary, march... then use the switch
right?
Yah, sort of. There is really no hard, fast rule. It's really just about program readability. If you have a huge if/else block you think you can write more clean using switch, that's what you do.
switch(score/10) { case 10: printf("GOOD JOB 100\n"); break; case 9: printf("A\n"); break; case 8: printf("B\n"); break; case 7: printf("C\n"); break; case 6: printf("D\n"); break; default: printf("YOU FAIL.\n"); }But I'm a dirty cheater.
Edit: Of course, god help you if you get above 100%. Being clever can get you bit. 8-)
Like:
floor(((float)grade) / 10.0f);
Nope.
#include <iostream> using namespace std; int main() { int drink; cout<<"1: Coca-Cola\n2: Dr Pepper\n3: Sprite\n4: Diet Coke\n5: Orange Drink\n"; start: cout<<"Please enter the number for your drink: "; cin>> drink; switch(drink) { case 1 : cout<< "Dispensing Coca-Cola. ENJOY!\n";break; case 2 : cout<< "Dispensing Dr Pepper. ENJOY!\n";break; case 3 : cout<< "Dispensing Sprite. ENJOY!\n";break; case 4 : cout<< "Dispensing Diet Coke. ENJOY!\n";break; case 5 : cout<< "Dispensing Grape Drink. ENJOY!\n";break; default : cout<< "You follow directions poorly, try again.\n"; goto start; } return 0; }Is there some simple way im missing to get that to go back to the beginning upon default, that i'm missing, or is it something ill pick up later?
Usually you'd do it with a while loop and some sort of flag (since a break would fail to break out of the loop due to the switch statement).
You could do that. It's confusing and bad practice. You could probably use a do-while there.