|
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. |
On September 17 2013 14:00 sluggaslamoo wrote:Show nested quote +On September 17 2013 12:57 Millitron wrote:On September 17 2013 09:12 sluggaslamoo wrote: If your language requires you to write getters/setters then it is probably a terrible language, e.g Java/.NET.
If you don't have a choice however (working in a corporate environment), the debate is asking the wrong question. You shouldn't be lumping Getter and Setter in the same question.
Getters are perfectly fine in any situation and very useful for immutable attributes, in fact there's no point in not having them unless you are just feeling lazy. If it is mutable then don't provide a Getter obviously.
Setters on the other hand are a problem, there are almost no instances where you would need a Setter for an object. If you have setters for your objects you are in the wrong frame of mind and need to do a lot more research on the paradigm of OOP. Neither Java nor .NET force getters and setters. You can have public instance variables in both of them. I never said force, I said require. Force and require are the same thing in that context? It would only be different if you had said "if your language's idiomatic style requires..." or something along those lines. C# also has properties, which remove the need to write getters/setters for "future refactoring possibility" purposes and make it so even the idiomatic style does not encourage them.
|
On September 17 2013 14:04 tec27 wrote:Show nested quote +On September 17 2013 14:00 sluggaslamoo wrote:On September 17 2013 12:57 Millitron wrote:On September 17 2013 09:12 sluggaslamoo wrote: If your language requires you to write getters/setters then it is probably a terrible language, e.g Java/.NET.
If you don't have a choice however (working in a corporate environment), the debate is asking the wrong question. You shouldn't be lumping Getter and Setter in the same question.
Getters are perfectly fine in any situation and very useful for immutable attributes, in fact there's no point in not having them unless you are just feeling lazy. If it is mutable then don't provide a Getter obviously.
Setters on the other hand are a problem, there are almost no instances where you would need a Setter for an object. If you have setters for your objects you are in the wrong frame of mind and need to do a lot more research on the paradigm of OOP. Neither Java nor .NET force getters and setters. You can have public instance variables in both of them. I never said force, I said require. Force and require are the same thing in that context? It would only be different if you had said "if your language's idiomatic style requires..." or something along those lines. C# also has properties, which remove the need to write getters/setters for "future refactoring possibility" purposes and make it so even the idiomatic style does not encourage them.
If your language's idiomatic style requires you to write getters/setters then it is probably a terrible language.
Properties are really the same as writing getters and setters. When it is pre-compiled they are literally turned into a get_ and set_ method by name.
Future refactoring possibility isn't really a good reason to be writing a setter.
Objects are about providing services to other objects, it is called Responsibility Driven Design. If you are thinking about writing properties you are already doing it wrong.
The only time you should write properties is for creating read-only attributes, but then I don't know why the just don't have read-only attributes instead of having to waste time writing properties. Unfortunately read-only in C# is reserved when they really meant immutable not read-only.
|
Write any container class without setters. See if you like them better afterwards.
std::vector would be pretty useless without at() and overloaded [] operator for example.
|
One reason I'm wondering why people hate setters and getters:
Lets assume that you've written a RNG that has complex internal code (just as an example) and requires lots of code just to set a seed (once again, just as an example). In that case, I would find it easier to read setSeed() than reading all of the code required to set the seed itself
|
On September 17 2013 14:27 sluggaslamoo wrote:Show nested quote +On September 17 2013 14:04 tec27 wrote:On September 17 2013 14:00 sluggaslamoo wrote:On September 17 2013 12:57 Millitron wrote:On September 17 2013 09:12 sluggaslamoo wrote: If your language requires you to write getters/setters then it is probably a terrible language, e.g Java/.NET.
If you don't have a choice however (working in a corporate environment), the debate is asking the wrong question. You shouldn't be lumping Getter and Setter in the same question.
Getters are perfectly fine in any situation and very useful for immutable attributes, in fact there's no point in not having them unless you are just feeling lazy. If it is mutable then don't provide a Getter obviously.
Setters on the other hand are a problem, there are almost no instances where you would need a Setter for an object. If you have setters for your objects you are in the wrong frame of mind and need to do a lot more research on the paradigm of OOP. Neither Java nor .NET force getters and setters. You can have public instance variables in both of them. I never said force, I said require. Force and require are the same thing in that context? It would only be different if you had said "if your language's idiomatic style requires..." or something along those lines. C# also has properties, which remove the need to write getters/setters for "future refactoring possibility" purposes and make it so even the idiomatic style does not encourage them. If your language's idiomatic style requires you to write getters/setters then it is probably a terrible language. Properties are really the same as writing getters and setters. When it is pre-compiled they are literally turned into a get_ and set_ method by name. Future refactoring possibility isn't really a good reason to be writing a setter. Objects are about providing services to other objects, it is called Responsibility Driven Design. If you are thinking about writing properties you are already doing it wrong. The only time you should write properties is for creating read-only attributes, but then I don't know why the just don't have read-only attributes instead of having to waste time writing properties. Unfortunately read-only in C# is reserved when they really meant immutable not read-only. I don't know why you're arguing with me, I wasn't trying to get into this debate again; its been had at least like 4 times in this thread already. I was just replying because you were a bit short/rude with the previous poster and acted as if what you said made sense. It didn't.
I don't even disagree with you really, so your arguments are kind of useless here.
|
On September 17 2013 14:56 WindWolf wrote: One reason I'm wondering why people hate setters and getters:
Lets assume that you've written a RNG that has complex internal code (just as an example) and requires lots of code just to set a seed (once again, just as an example). In that case, I would find it easier to read setSeed() than reading all of the code required to set the seed itself This is an example of where you probably wouldn't want to name the method setSeed. getX and setX have connotations that they have very little side effects (e.g. getX has no side effects and setX has a single side effect, namely setting X). If they are expensive and/or have more side effects than just that, a different naming choice could indicate that to the user better.
But you're setting up a straw-man for your "reasoning for using setters" here. Your alternative is to repeat the same code everywhere, which is obviously bad and not what any of the anti-gettersetter crowd is arguing for.
|
On September 17 2013 14:56 WindWolf wrote: One reason I'm wondering why people hate setters and getters:
Lets assume that you've written a RNG that has complex internal code (just as an example) and requires lots of code just to set a seed (once again, just as an example). In that case, I would find it easier to read setSeed() than reading all of the code required to set the seed itself
There are plenty of reasons. In this case it could make the code hard to debug. Someone could easily already be using setSeed() deeply nested within some code file.
Then when your code doesn't randomize differently, you are sitting their scratching your head why your RNG code didn't work when really it was setSeed causing the issue.
The seed should be set in the initialiser or as a config variable or both. You aren't going to need to set the seed on the fly for RNG in a game. When the game starts you initialise the RNG object with the seed using the game's config table, after that it doesn't need to change until the next game. This allows you to isolate the issue and make debugging much easier.
IMO it is also simpler and much more intuitive than using setSeed.
|
On September 17 2013 13:34 Amnesty wrote:Anyone know of a way to put something into a input stream. So there is a default value there. So instead of cout >> "Input a number :"; int x; cin << x;
printing Input a number : it would print Input a number :150_<cursor right here> For example. I do not believe there is a standard C++ way of doing this. You can inject data into cin as follows: std::cin.putback('1'); std::cin.putback('5'); std::cin.putback('0'); But it will not show up on the console and you cannot erase those characters. Generally standard C++ is very limited in what you can do at the console. If you are just using the console because you are still learning and it is more convenient than coding a GUI interface, then I would just settle for something simpler like: Input a number (default: 150): _
If you actually want to do it properly you will need to do something platform dependent or use a third-party library that abstracts away the platform dependence. On Win32 you could do (this has no error-handling and can only handle digits, but the idea can easily be generalized if you wish):
#include <iostream> #include <windows.h>
void write_digit_to_stdin(char digit) { // Calculate virtual-key code. // Look up on MSDN for codes for other keys than the digits. int virtual_key_code = static_cast<int>(digit) + 0x30 - static_cast<int>('0');
// Fill out an input record that simulates pressing the desired key followed // by releasing the same key. INPUT_RECORD input_record[2]; for(int i = 0; i < 2; ++i) { input_record[i].EventType = KEY_EVENT; input_record[i].Event.KeyEvent.dwControlKeyState = 0; input_record[i].Event.KeyEvent.uChar.UnicodeChar = digit; input_record[i].Event.KeyEvent.wRepeatCount = 1; input_record[i].Event.KeyEvent.wVirtualKeyCode = virtual_key_code; input_record[i].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(virtual_key_code, MAPVK_VK_TO_VSC); } input_record[0].Event.KeyEvent.bKeyDown = TRUE; input_record[1].Event.KeyEvent.bKeyDown = FALSE;
DWORD tmp = 0; WriteConsoleInput( GetStdHandle(STD_INPUT_HANDLE) ,input_record,2,&tmp); }
int main() { int x; std::cout << "Input a number: "; write_digit_to_stdin('1'); write_digit_to_stdin('5'); write_digit_to_stdin('0'); std::cin >> x; std::cout << "Wrote: " << x << std::endl;
return 0; }
|
On September 17 2013 14:56 WindWolf wrote: One reason I'm wondering why people hate setters and getters:
Lets assume that you've written a RNG that has complex internal code (just as an example) and requires lots of code just to set a seed (once again, just as an example). In that case, I would find it easier to read setSeed() than reading all of the code required to set the seed itself It is some what misleading to call it "set seed" because that suggests that you are just setting an internal seed variable (which very well may happen, but you don't want the user to care). A better way to think of it is that you are asking the RNG to seed itself with some value, you are asking it to perform an action, not accessing a data element (whether or not the two may equivalent in implementation is irrelevant). You should call it "seed" not "setSeed" in my opinion.
However in any case a RNG is probably one of the cases where the user might very well care about implementation-details and therefore leaking implementation details might not be as bad.
|
On September 17 2013 15:08 rasnj wrote:Show nested quote +On September 17 2013 13:34 Amnesty wrote:Anyone know of a way to put something into a input stream. So there is a default value there. So instead of cout >> "Input a number :"; int x; cin << x;
printing Input a number : it would print Input a number :150_<cursor right here> For example. I do not believe there is a standard C++ way of doing this. You can inject data into cin as follows: std::cin.putback('1'); std::cin.putback('5'); std::cin.putback('0'); But it will not show up on the console and you cannot erase those characters. Generally standard C++ is very limited in what you can do at the console. If you are just using the console because you are still learning and it is more convenient than coding a GUI interface, then I would just settle for something simpler like: Input a number (default: 150): _ If you actually want to do it properly you will need to do something platform dependent or use a third-party library that abstracts away the platform dependence. On Win32 you could do (this has no error-handling and can only handle digits, but the idea can easily be generalized if you wish): #include <iostream> #include <windows.h>
void write_digit_to_stdin(char digit) { // Calculate virtual-key code. // Look up on MSDN for codes for other keys than the digits. int virtual_key_code = static_cast<int>(digit) + 0x30 - static_cast<int>('0');
// Fill out an input record that simulates pressing the desired key followed // by releasing the same key. INPUT_RECORD input_record[2]; for(int i = 0; i < 2; ++i) { input_record[i].EventType = KEY_EVENT; input_record[i].Event.KeyEvent.dwControlKeyState = 0; input_record[i].Event.KeyEvent.uChar.UnicodeChar = digit; input_record[i].Event.KeyEvent.wRepeatCount = 1; input_record[i].Event.KeyEvent.wVirtualKeyCode = virtual_key_code; input_record[i].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(virtual_key_code, MAPVK_VK_TO_VSC); } input_record[0].Event.KeyEvent.bKeyDown = TRUE; input_record[1].Event.KeyEvent.bKeyDown = FALSE;
DWORD tmp = 0; WriteConsoleInput( GetStdHandle(STD_INPUT_HANDLE) ,input_record,2,&tmp); }
int main() { int x; std::cout << "Input a number: "; write_digit_to_stdin('1'); write_digit_to_stdin('5'); write_digit_to_stdin('0'); std::cin >> x; std::cout << "Wrote: " << x << std::endl;
return 0; }
I'm not new to C++, just i was rather surprised i was stumped on this. I don't think I ever wanted to do such a thing before.
Anyway, i started learning assembly and wrote this puzzle game using just assembly.
![[image loading]](http://i3.photobucket.com/albums/y92/Amnesty2/mazeGame.jpg)
Since i'm not really good at assembly yet i wrote the map editor in C++ and its a console app because a full blown GUI app would be a bit overkill. It would be nice to press S for save and instead of getting a blank input for the filename it would default to something. And after saving it as a certain name, it would default to that too. Its been really annoying typing in the full name every time.
I could just write my own input loop where i push back chars into a string, popback on backspace, and set the cursor position accordingly. But I thought i would ask if there was something easy to do.
|
On September 17 2013 15:06 sluggaslamoo wrote:Show nested quote +On September 17 2013 14:56 WindWolf wrote: One reason I'm wondering why people hate setters and getters:
Lets assume that you've written a RNG that has complex internal code (just as an example) and requires lots of code just to set a seed (once again, just as an example). In that case, I would find it easier to read setSeed() than reading all of the code required to set the seed itself There are plenty of reasons. In this case it could make the code hard to debug. Someone could easily already be using setSeed() deeply nested within some code file. Then when your code doesn't randomize differently, you are sitting their scratching your head why your RNG code didn't work when really it was setSeed causing the issue. The seed should be set in the initialiser or as a config variable or both. You aren't going to need to set the seed on the fly for RNG in a game. When the game starts you initialise the RNG object with the seed using the game's config table, after that it doesn't need to change until the next game. This allows you to isolate the issue and make debugging much easier. IMO it is also simpler and much more intuitive than using setSeed. I didn't meant about RNG's specifically, I was just the principal of using a set method for a long piece of code instead of writing all of that code again.
And even is setSeed isn't a good name, what would make for a good name (we can take the debate on whenever you should be able to change the seed of a RGN or not another time)
|
RNG just happens to be a very unfortunate example since it's probably one of the main examples where you DON'T want to let anything set the value. To get as random results as possible, it's best practice to only make a singleton Random object, set the seed at program start and then poll that same object. Changing seed is just straight up bad practice, so an RNG class which lets you do that wouldn't make much sense.
However, the background concept makes sense of course. If a class has a mutable property in its interface which is implemented with a lot of code, it's definitely worth doing a setter for it. RNG just doesn't have such a mutable property in its interface, you're not supposed to change the seed of a RNG object.
|
On September 17 2013 16:37 Tobberoth wrote: RNG just happens to be a very unfortunate example since it's probably one of the main examples where you DON'T want to let anything set the value. To get as random results as possible, it's best practice to only make a singleton Random object, set the seed at program start and then poll that same object. Changing seed is just straight up bad practice, so an RNG class which lets you do that wouldn't make much sense.
However, the background concept makes sense of course. If a class has a mutable property in its interface which is implemented with a lot of code, it's definitely worth doing a setter for it. RNG just doesn't have such a mutable property in its interface, you're not supposed to change the seed of a RNG object. Yeah, I just couldn't think of any better thing at that very moment
|
Thanks for the information.
|
On September 17 2013 16:20 WindWolf wrote:Show nested quote +On September 17 2013 15:06 sluggaslamoo wrote:On September 17 2013 14:56 WindWolf wrote: One reason I'm wondering why people hate setters and getters:
Lets assume that you've written a RNG that has complex internal code (just as an example) and requires lots of code just to set a seed (once again, just as an example). In that case, I would find it easier to read setSeed() than reading all of the code required to set the seed itself There are plenty of reasons. In this case it could make the code hard to debug. Someone could easily already be using setSeed() deeply nested within some code file. Then when your code doesn't randomize differently, you are sitting their scratching your head why your RNG code didn't work when really it was setSeed causing the issue. The seed should be set in the initialiser or as a config variable or both. You aren't going to need to set the seed on the fly for RNG in a game. When the game starts you initialise the RNG object with the seed using the game's config table, after that it doesn't need to change until the next game. This allows you to isolate the issue and make debugging much easier. IMO it is also simpler and much more intuitive than using setSeed. I didn't meant about RNG's specifically, I was just the principal of using a set method for a long piece of code instead of writing all of that code again. And even is setSeed isn't a good name, what would make for a good name (we can take the debate on whenever you should be able to change the seed of a RGN or not another time)
The same principle still applies for lots of things. You want to reduce the amount of side effects to a minimum in order to make maintenance easier. I can't even remember the last time I have used a setter.
|
On September 18 2013 08:33 sluggaslamoo wrote:Show nested quote +On September 17 2013 16:20 WindWolf wrote:On September 17 2013 15:06 sluggaslamoo wrote:On September 17 2013 14:56 WindWolf wrote: One reason I'm wondering why people hate setters and getters:
Lets assume that you've written a RNG that has complex internal code (just as an example) and requires lots of code just to set a seed (once again, just as an example). In that case, I would find it easier to read setSeed() than reading all of the code required to set the seed itself There are plenty of reasons. In this case it could make the code hard to debug. Someone could easily already be using setSeed() deeply nested within some code file. Then when your code doesn't randomize differently, you are sitting their scratching your head why your RNG code didn't work when really it was setSeed causing the issue. The seed should be set in the initialiser or as a config variable or both. You aren't going to need to set the seed on the fly for RNG in a game. When the game starts you initialise the RNG object with the seed using the game's config table, after that it doesn't need to change until the next game. This allows you to isolate the issue and make debugging much easier. IMO it is also simpler and much more intuitive than using setSeed. I didn't meant about RNG's specifically, I was just the principal of using a set method for a long piece of code instead of writing all of that code again. And even is setSeed isn't a good name, what would make for a good name (we can take the debate on whenever you should be able to change the seed of a RGN or not another time) The same principle still applies for lots of things. You want to reduce the amount of side effects to a minimum in order to make maintenance easier. I can't even remember the last time I have used a setter.
Can you make some clarifications, for those of us in the peanut gallery.
What language do you do most of your programming in? And what are the size of the projects you work on? Also, are you excluding domain objects from outside of your discussions of getters and setters are bad.
|
Mutable objects that you pass around a lot in general are confusing as shit to keep track of, especially in larger code bases. Immutability wherever possible keeps things sane.
Setters are just one example of a highly mutable object that can easily get out of hand.
For example, see Effective Java ch 15.
|
On September 17 2013 16:20 WindWolf wrote:Show nested quote +On September 17 2013 15:06 sluggaslamoo wrote:On September 17 2013 14:56 WindWolf wrote: One reason I'm wondering why people hate setters and getters:
Lets assume that you've written a RNG that has complex internal code (just as an example) and requires lots of code just to set a seed (once again, just as an example). In that case, I would find it easier to read setSeed() than reading all of the code required to set the seed itself There are plenty of reasons. In this case it could make the code hard to debug. Someone could easily already be using setSeed() deeply nested within some code file. Then when your code doesn't randomize differently, you are sitting their scratching your head why your RNG code didn't work when really it was setSeed causing the issue. The seed should be set in the initialiser or as a config variable or both. You aren't going to need to set the seed on the fly for RNG in a game. When the game starts you initialise the RNG object with the seed using the game's config table, after that it doesn't need to change until the next game. This allows you to isolate the issue and make debugging much easier. IMO it is also simpler and much more intuitive than using setSeed. I didn't meant about RNG's specifically, I was just the principal of using a set method for a long piece of code instead of writing all of that code again. And even is setSeed isn't a good name, what would make for a good name (we can take the debate on whenever you should be able to change the seed of a RGN or not another time)
What you described is not a "setter" just because it has name "set" in it. Of course almost every function or even single statement in programming involves something getting "set" to something else. Doesn't make it a "setter".
edit: Although I think you inadvertently brought up a good question, which is "how much work can a method do before it's not really a 'setter' or 'getter' anymore?" I think you brought up an example where it is clearly doing too much work to be a "setter".
|
On September 18 2013 08:45 berated- wrote:Show nested quote +On September 18 2013 08:33 sluggaslamoo wrote:On September 17 2013 16:20 WindWolf wrote:On September 17 2013 15:06 sluggaslamoo wrote:On September 17 2013 14:56 WindWolf wrote: One reason I'm wondering why people hate setters and getters:
Lets assume that you've written a RNG that has complex internal code (just as an example) and requires lots of code just to set a seed (once again, just as an example). In that case, I would find it easier to read setSeed() than reading all of the code required to set the seed itself There are plenty of reasons. In this case it could make the code hard to debug. Someone could easily already be using setSeed() deeply nested within some code file. Then when your code doesn't randomize differently, you are sitting their scratching your head why your RNG code didn't work when really it was setSeed causing the issue. The seed should be set in the initialiser or as a config variable or both. You aren't going to need to set the seed on the fly for RNG in a game. When the game starts you initialise the RNG object with the seed using the game's config table, after that it doesn't need to change until the next game. This allows you to isolate the issue and make debugging much easier. IMO it is also simpler and much more intuitive than using setSeed. I didn't meant about RNG's specifically, I was just the principal of using a set method for a long piece of code instead of writing all of that code again. And even is setSeed isn't a good name, what would make for a good name (we can take the debate on whenever you should be able to change the seed of a RGN or not another time) The same principle still applies for lots of things. You want to reduce the amount of side effects to a minimum in order to make maintenance easier. I can't even remember the last time I have used a setter. Can you make some clarifications, for those of us in the peanut gallery. What language do you do most of your programming in? And what are the size of the projects you work on? Also, are you excluding domain objects from outside of your discussions of getters and setters are bad.
I use Ruby mostly, but in certain companies I have had to deal with other languages, and I've worked on both small and behemoth monolithic Java projects.
Basically what phar said. For every time you allow something to change, it means one extra thing you have to think of when debugging.
Setting is best left to initialisers or factories, then the object performs services using those parameters and keeps everything self contained. Instead of using setters, you can create a new object with new parameters.
Think of it as people working within an organisation, you don't set attributes to people, you have different people who are capable of doing different things. "Hire" the people you need, then hand off the work to each of these people to get the work done.
In Ruby I also tend to do a lot of things functionally, which removes the issue of side effects, but you also don't have these hand offs between objects when really you just want something short and sweet.
Its very hard to do right and will take years to figure out how to be able to do it in a way that makes the code look better not worse, let alone figure it out quickly. Just realise that in the long term you are going to be much better off.
The reason people write setters is because they don't realise other patterns exist to allow you to perform operations without them.
|
On September 17 2013 15:08 rasnj wrote:Show nested quote +On September 17 2013 13:34 Amnesty wrote:Anyone know of a way to put something into a input stream. So there is a default value there. So instead of cout >> "Input a number :"; int x; cin << x;
printing Input a number : it would print Input a number :150_<cursor right here> For example. I do not believe there is a standard C++ way of doing this. You can inject data into cin as follows: std::cin.putback('1'); std::cin.putback('5'); std::cin.putback('0'); But it will not show up on the console and you cannot erase those characters. Generally standard C++ is very limited in what you can do at the console. If you are just using the console because you are still learning and it is more convenient than coding a GUI interface, then I would just settle for something simpler like: Input a number (default: 150): _ If you actually want to do it properly you will need to do something platform dependent or use a third-party library that abstracts away the platform dependence. On Win32 you could do (this has no error-handling and can only handle digits, but the idea can easily be generalized if you wish): #include <iostream> #include <windows.h>
void write_digit_to_stdin(char digit) { // Calculate virtual-key code. // Look up on MSDN for codes for other keys than the digits. int virtual_key_code = static_cast<int>(digit) + 0x30 - static_cast<int>('0');
// Fill out an input record that simulates pressing the desired key followed // by releasing the same key. INPUT_RECORD input_record[2]; for(int i = 0; i < 2; ++i) { input_record[i].EventType = KEY_EVENT; input_record[i].Event.KeyEvent.dwControlKeyState = 0; input_record[i].Event.KeyEvent.uChar.UnicodeChar = digit; input_record[i].Event.KeyEvent.wRepeatCount = 1; input_record[i].Event.KeyEvent.wVirtualKeyCode = virtual_key_code; input_record[i].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(virtual_key_code, MAPVK_VK_TO_VSC); } input_record[0].Event.KeyEvent.bKeyDown = TRUE; input_record[1].Event.KeyEvent.bKeyDown = FALSE;
DWORD tmp = 0; WriteConsoleInput( GetStdHandle(STD_INPUT_HANDLE) ,input_record,2,&tmp); }
int main() { int x; std::cout << "Input a number : "; write_digit_to_stdin('1'); write_digit_to_stdin('5'); write_digit_to_stdin('0'); std::cin >> x; std::cout << "Wrote: " << x << std::endl;
return 0; }
Why make it so complicated?
#include <iostream> #include <string> #include <locale> #include <sstream> using namespace std;
int main() { locale loc; int x = 150; string str;
cout << "Input a number (default 150): "; getline(cin, str); if(strlen str) { if(isdigit(str[0], loc)) { stringstream(str) >> x; } }
cout << "Your chosen number is: " << x << endl;
return 0; }
|
|
|
|