Stuck with error

1
2
3
4
cout<<endl<<endl<<"x1= ";
        cin >> x1;
        cout <<"x2= ";
        cin >> x2;



Thats the piece of code in question. Its not the problem. The problem is if the user enter letters it locks the program up so how do I stop them from entering letters?
Information MIA. What are the types of x1 and x2?

-Albatross
They are int so thats why I need them to not put letters into them
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cctype>
using namespace std;

int main()
{
	int x1;
	cout << "enter a number :";
	cin >> x1;
	
	//if x1 is number, then 'yes' message will be printed out
	if (!isdigit(x1))
	{
		cout << "yes" << endl;
		exit(-1);
	}
	//but if it's not number, then 'no' message will be printed out
	else
	{
		cout << "no" << endl;
	}
}


this is just a simple example.
hope this help.
Last edited on
Did you even test that code? :/
[Session started at 2010-11-05 09:09:09 -0700.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1469) (Wed May  5 04:36:56 UTC 2010)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
Loading program into debugger…
Program loaded.
run
[Switching to process 11478]
Running…
enter a number :a
yes


EDIT: And that's not all...

[Session started at 2010-11-05 09:11:12 -0700.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1469) (Wed May  5 04:36:56 UTC 2010)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys001
Loading program into debugger…
Program loaded.
run
[Switching to process 11514]
Running…
enter a number :48
no


You may want to reread the docs on cctype. ;)
http://cplusplus.com/reference/clibrary/cctype/

@OP: I recommend reading this for some hints and tips. You might figure out a solution from some of these examples:
http://cplusplus.com/articles/numb_to_text/

-Albatross
Last edited on
well albatross confused me with all this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Session started at 2010-11-05 09:11:12 -0700.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1469) (Wed May  5 04:36:56 UTC 2010)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys001
Loading program into debugger…
Program loaded.
run
[Switching to process 11514]
Running…
enter a number :48
no



but I do know nosxlimits code did not work for me so what albatross is saying must be right im going to read that stuff he put links to.
Topic archived. No new replies allowed.