How to disallow characters

Hi, i have received a task for extended study to write a program in C++ that will allow you to enter 5 single digit numbers. So far the program to ONLY allow single digit numbers.

Here is my code and any help would be greatly appreciated.

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 <string>

using namespace std;

int main()
{


	int number[5];
	int i;

	for(i=0;i<=4;i++)
	{
		cout<<"Please enter a single didgit number : "<< endl;
		cin>>number[i];
	}
	for(i=0;i<=4;i++)	
	{
		cout<<"The five numbers you chose where : "<< number[i] <<endl;

	}

	system("pause");
	

}



Thanks
Last edited on
You don't need the string header.
Add a check to reject numbers:

1
2
3
4
5
6
7
8
cout<<"Please enter a single didgit number : "<< endl;
cin >> number[i];

while (number[i] >= 10 || number[i] <= -10)
{
    cout << "Not a single digit number, try again: ";
    cin >> number[i];
}
Sorry, I seem to be drawing blank. What is it that you need help with?
Da0omph >>

Hi basically i need the program shown to display a message when an incorrect symbol (I.e. Not a number between 0 and 9) is entered and ask the user to enter a valid number.

Catfish >>

Hey i have a variation of this myself however it just causes the program to break and display;

"Not a single digit number, try again: "

over and over again. Is there anything i have missed including the while loop as shown?






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

int main()
{


	int number[5];
	int i;

	for(i=0;i<=4;i++)
	{
		cout<<"Please enter a single digit number : "<< endl;
		cin>>number[i];

		while (number[i] >= 10 || number[i] <= -10)
		{
			cout<<"Not a single digit number, try again: "<<endl;
			cin>>number[i];
		}

	}
	for(i=0;i<=4;i++)	
	{
		cout<<"The five numbers you chose where : "<< number[i] <<endl;

	}

	system("pause");
	
	

}
Last edited on
You changed the topic subject, right? I don't remember it was about disallowing characters.

Anyway, here's what you can do:

* Use std::string and read a string, using getline().
* Check if the string has length equal to one, otherwise reject it.
* Use isdigit() to see if it's a digit.
* Make a stringstream out of the string in order to read the number itself.

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
#include <iostream>
#include <string> // for string and getline()
#include <cctype> // for isdigit()
#include <sstream> // for using >> on the contents of a string

using namespace std;

int main()
{
    int number[5];
    int i=0;
    string temp;

    while (i < 5)
    {
        cout<<"Please enter a single digit number : ";
        getline(cin, temp); // this will also read spaces, unlike cin>>

        bool finished=false;

        while (finished == false)
        // we dont need { and } if we have only one instruction in: if, else, for, while or do
        // (here if-else counts as a single instruction)
            if (temp.length() == 1 && isdigit(temp[0]) != 0) // good value, add it
            {
                stringstream temp_stream(temp); // with a stringstream we can use
                temp_stream >> number[i++]; // a string as if it's a file
                finished=true; // we're done reading, exit the little while()
            }
            else // not a good value, read again
            {
                cout<<"Not a single digit number, try again: ";
                getline(cin, temp);
            }
    }

    for(i=0; i<=4; ++i) // curly braces not needed for single instructions
        cout<<"The five numbers you chose were : "<< number[i] <<endl;

    system("pause");
}


Topic archived. No new replies allowed.