INT checking

Hi!

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?
closed account (iw0XoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<stdexcept>
using namespace std;

struct Array_too_large{};
int main()
try
{
        int *output;
        int num;

        cout << "Have large will the array be? ";
        cin >> num;
        if(num > 9)
                throw Array_too_large();

        output = new int [num];
        return 0;
}
catch(Array_too_large)
{
        cerr << "array can not be larger than 9\n";
        return 1;
}
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.
Last edited on
closed account (STR9GNh0)
do while,

!= to a range of number = ignore.
closed account (iw0XoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;

struct Not_an_integer{};
int main()
try
{
        int *output;
        int num = -1;

        cout << "Have large will the array be? ";
        cin >> num;
        if(num < 0 )
                throw Not_an_integer();

        output = new int [num];
        return 0;
}
catch(Not_an_integer)
{
        cerr << "Not an integer\n";
        return 1;
}
Last edited on
Still console goes mad when I type symbols like a,b,c,\,< etc. It just removes the problem with 0 and negative numbers. =/
Last edited on
closed account (iw0XoG1T)
You cannot initialize the num variable with anything other than a number--if it is going crazy it is something else. Can you post your code?
#include<iostream>
#include <iomanip>
using namespace std;

void printArray (int array[], int num)
{
int i;

for(i=0; i<num; i++)
cout << " " << array[i];
cout << endl;
}


int main()
{
int *output;
int num;
int a=0;

cout << "Have large will the array be? ";
cin >> num;

output = new int [num];

cout << "Inupt " << num << " values - ";

for(int i=0; i<num; i++)
{
cin >> output[a];
a++;
}
printArray(output, num);

// Remove(output, num, Average(output, num));
delete output;


return(0);
}


Try typing in console any symbols not numbers
closed account (iw0XoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
#include <iomanip>
using namespace std;

void printArray (int array[], int num)
{
	int i;

	for(i=0; i<num; i++)
		cout << " " << array[i];
	cout << endl;
}

struct Not_valid{};
int main()
try
{
	int *output;
	int num;
	int a=0;
	int test = -1;

	cout << "Have large will the array be? ";
	cin >> test;
	if(test == -1) throw Not_valid();
	num = test;
	output = new int [num];

	cout << "Inupt " << num << " values - ";
	test = -1;
	for(int i=0; i<num; i++)
	{
		cin >> test;
		if(test == -1) throw Not_valid();
		output[a] = test;
		test = -1;
		a++;
	}
	printArray(output, num);

	// Remove(output, num, Average(output, num));
	delete output;


	return(0);
}
catch(Not_valid)
{
	cerr << "not valid\n";
	return 1;
}
Big thanks thanks to you, could not done it with out your help :)
Good grief.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <limits>
using namespace 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 = new int [num];
  ...
  }

Hope this helps.
closed account (iw0XoG1T)
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.
Well then, all ways being equal, why the anger?

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.


I'm outta here.
closed account (iw0XoG1T)
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.
@chwsks: I really hope you don't get a job in coding...I'd hate to have to maintain that...
closed account (iw0XoG1T)
@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.

Time to grow-up and do something worthwhile.
Last edited on
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.

-Albatross
closed account (iw0XoG1T)
@albatross

It abort because that is what the op requested:
aldaris wrote:

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.
It abort because that is what the op requested

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.

-Albatross
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. :)
Topic archived. No new replies allowed.