User can only input digits?

Hello, I'm trying to figure out what to use for "accountn" so that the user can only input a number. If they ended up entering a letter instead, they would get something like "error: You must enter a number".


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

char main ()
{
	int accountn; // account number

	cout<<"Please enter your account number: ";
	cin>>accountn;

	cout<<endl<<"Your account number is: "<<accountn<<endl;

	system ("PAUSE");
	return 0;
}

Last edited on
closed account (zb0S216C)
The only way I think of is creating a basic Win32 project, hide the window, show the window's console, and in the procedure, only register the 0-9 keys using the WM_KEYDOWN message.
EDIT:
Never mind I failed
Last edited on
@some random dude: A variable can't change its type.
http://www.cplusplus.com/forum/articles/6046/ or just check if the input was successful (then clear the state)
When I run the program I can enter a number and it prints it out, if I enter anything else it says invalid account number......
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
#include <iostream>
#include <conio.h>
using namespace std;
template <typename Type>
bool Function (Type a0)
{
	return false;
}
template <>
bool Function <int> (int a0)
{
	return true;
}
int main ()
{
	int iInput = 0;
	cout << "Enter account number: ";
	cin >> iInput; //this fail and iInput does not change
	Function (iInput); //statement has no effect (edit: but it always returns true)
	if (iInput) // if(iInput!=0)
	{
		cout << "Your account number is: " << iInput << endl;
	}
	else
	{
		cout << "Invalid account number..." << endl;
	}
	getch ();
	return 0;
}
Try to use 0 as input
Last edited on
I failed D:

here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
	int iInput = 0;
	cout << "Enter account number: ";
	cin >> iInput;
	if (iInput)
	{
		cout << "Account number: " << iInput << endl;
	}
	else
	{
		cout << "Invalid account number..." << endl;
	}
	getch ();
	return 0;
}


That works for anything but non-integer input, and 0.

I hope I didn't mess up this time... :P
Looks like I somehow found out how to do it by looking at yours. I reversed the if and else statements Can anyone check if there's any flaws with this (besides the system ("PAUSE"))?

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
#include <iostream>
using namespace std;

char main ()
{
	
	int accountn = 'a'; // account number

	a:
	cout<<"Please enter your account number: ";
	cin>>accountn;

	if (accountn=='a') // Output Error
	{
		cout<<endl<<"Invalid account number"<<endl;
		system ("PAUSE");
		goto a;
	}

	else
	{

	cout<<endl<<"Your account number is: "<<accountn<<endl;

	system ("PAUSE");
	return 0;

	}

}


Edit: Looks like I get stuck in a loop if I have it repeat action at "Output Error".
Last edited on
Handling input is a deceitfully complex task, so this question is actually a common one. The simplest and most robust way to do it is to input as a std::string, then parse it using a std::istringstream and checking for errors.

Some related examples to help:
http://www.cplusplus.com/forum/beginner/13044/page1.html#msg62827
http://www.cplusplus.com/forum/beginner/18258/#msg92955
http://www.cplusplus.com/forum/beginner/18518/
Also keep in mind that all user input will end with the user pressing the ENTER key.

Hope this helps.
Uh kaz: Yes there is a major flaw with your code. The goto statement. Do. Not. Ever. Use. It. Unless. It. Is. Absolutely. Necessary.Which.It.Is.Not.In.This.Case.And.Probably.In.No.Other.Case.You.Will.Encounter.In.A.Long.Time.
Topic archived. No new replies allowed.