I have question. For char types I can check with anscii table all that I need.
But have can I check for int variables and arrays that I have typed legal numer 0-9 and if not program stops working?
simple example:
#include<iostream>
using namespace std;
int main()
{ int *output;
int num;
cout << "Have large will the array be? ";
cin >> num;
output = new int [num]; }
What I need to write so if user presses something else than 1-9, that program would simply stop working?
I guess it`s my fault, didn`t explain like I needed to.
The problem is - if I type in console 0 or some symbol (a,b,c,d,-,+,~, etc.) program starts acting like crazy and typing in 0`os all the time. Question was what I need to do, if I want that when I type in console these symbols that program just would:
cout << "Wrong number"; and rreturn (1); as error.
#include <iostream>
#include <limits>
usingnamespace std;
int main()
{
int *output;
int num;
cout << "Have large will the array be? ";
while (true)
{
cin >> num;
bool ok = cin.good();
if (!ok) cin.clear();
cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
if (ok) break; // Other conditions go here too, like: if (ok && (num > 0) && (num < 200)) break;
cout << "Hey, that was not an integer (in 0..200). Please enter an array size? ";
}
output = newint [num];
...
}
wow Duoas you really know how to rewrite beginners code. I am sure everyone who has been coding for more than a month could have done a better job.
You do realize this was an exercise for someone who has only been writing for a couple of weeks, right? He used the wrong delete too, and you are right there is probably about 100 different ways to have done this better.
Your version aborts on error, instead of any possible recovery; it is longer; it introduces unnecessary boilerplate; and it relies upon a not-guaranteed state. Really want to fight about it?
Beginners learn best by viewing multiple perspectives while/after working their way through code. I know this after studying and practicing pedagogy for many years.
I'm tired of people throwing their hate at me for offering alternatives and clarifications, and branding me as bad-tempered for complaining about the abusive way you all treat each other.
Oh yeah, good job on explaining to me the blatantly obvious.
You will never leave -- you need the ego boost -- 4000+ post-- you don't find that pathetic. Excuse me I thought the insult was intended at the beginner.
Duoas wrote:
Good grief.
If the insult was directed at me I could care less -- I have no respect for you.
@firedraco: there is not a single chance that you will ever have to maintain any code I write. I am not a programmer because I prefer a well paying job with benefits. Why do all the best programmers eventually take jobs in academia or goverment...because they want good pay and benefits. I'll bet that your superiors at your place of employment, who make better pay and get more respect than you, are not programmers.
If you read the whole post you will notice that I was hacking the original posters code--it is not my own. Also it is clear that a simple if(!cin)do something about the failure would have been a better test.
Here is my question to you--years of programming and you still are frequenting the beginners page and reading post marked as solved.
Can you two just stop fighting?!? There's been enough flamethrowing around here!
chwsks: Your code
aborts on error, instead of any possible recovery; it is longer; it introduces unnecessary boilerplate; and it relies upon a not-guaranteed state
.
...Well, it's true! :/
Others: Is there any need to reply, really? Replying just starts a cycle that continues until someone says stop or one side "wins", and fighting is really not worth it. Besides, the OP's problem has been solved.
What I need to write so if user presses something else than 1-9, that program would simply stop working?
I did not start this flame war I responded to two unnecessary insults:
Duoas wrote:
Good grief.
and
firedraco wrote:
I really hope you don't get a job in coding
Both of these individuals have been coding for years and they are still hanging out and reading post marked as solved on the beginners forum. It is about time that both of them grow-up and move on. I suspect they are both two low level code monkeys who get no respect at work and come to the beginners board for the easy ego boost. Forgive me it I do not show them respect, but to me someone with 3000+ post throwing insults on the beginners forum is a loser.
In which case, I would ask why the OP wanted the program to stop working rather than recover.
I suspect they are both two low level code monkeys who get no respect at work and come to the beginners board for the easy ego boost.
I am not defending Duoas's or firedraco's actions, but was that really necessary? You're just lowering yourself to what I suspect to be your view of their level by insulting them in return.
Thank you for the topic, and Duoas, thanks for using cin.clear() and cin.ignore(). I was trying to think of a some kind of function and that seems to be the wrong approach in C++.
This has been bothering me for a while and now it seems so obvious. :)