Getting input as a char array?

I'm at a complete loss and don't really know where to begin.
But basically what I need to do is get a 4digit positive integer and filter out incorrect inputs.
Examples of correct inputs: 1234, 0000, etc.
Examples of incorrect inputs: 12, 12wf, etc.

Pseudo-code of what I'm trying to do:
Get the input and put it into an array.
Check the array to see if it's 4 characters long, if it's not display an error and get the input again.
Check the array to see if all 4 values stored are 0-9, if they aren't display an error and get the input again.
Use a for loop from 0 to length of the array(in this case 3), and inside of the for loop you can just add one if state

if((typeof(arr[i])==int) && (arr[i]>=0)&&(arr[i]<10)}{

correct input
}
else{
incorrect input
}
This is very easy to do here this does it.
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
#include<iostream>
#include<cctype>

using namespace std;

bool doAgain();

int main()
{
	while(doAgain());
	cout << "Valid input.\n";
	return 0;
}

bool doAgain()
{
	char a[4];
	cout << "Enter a 4 digit number: " ;
	for(int i=0;i<4;i++)
	{
		cin >> a[i];
		if(!isdigit(a[i])) // checks if character by character is a digit or not.
		{
			cout << "Inputs contains non integer char.\n";
			cin.ignore(); //ignores the rest of the input.
			return 1;
		}
	}
	char x = cin.get();
	if(x!='\n') // if you find 1 more char available for input and that character is not the enter button.
	{
		cout << "Input contains more than 4 characters.\n";
		cin.ignore(); //ignores the rest of the characters in the input stream.
		return 1;
	}


	return 0;
}
mines shorter :)
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
#include <iostream>
#include <cctype>
#include <cstring>
using std::cout;
using std::cin;
using std::endl;

int main(void)
{
	const int max = 10;
	char string[max] = {0};
	cout << "Please enter 4 digits: " << endl;
	cin.getline(string, max, '\n');
		
	bool digits = true;
	for (int i = 0; i < strlen(string); i++)
	{
		if(!(isdigit(string[i])))
		{
			digits = false;
		}
	}

	while(!digits || strlen(string) < 4 || strlen(string) > 4)
	{
		cout << "Invalid input: " << endl;
		cin.getline(string, max, '\n');
	}	

	return 0;
}
Mine is shortest :D
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 <string>
#include <ctype.h>

using namespace std;

int main()
{
    string input = "";
    while (true) 
    {
        cout << "Please enter a 4 digit number: ";
        getline(cin, input);

        if(input.length() == 4)
            if(isdigit(input[0]) && isdigit(input[1]) && isdigit(input[2]) && isdigit(input[3]))
                break;
            
        cout << "Invalid input, please try again" << endl;
    }
    return 0;
}
Last edited on
I think he needs to get the input as a character array not a string that's why mine is longer.
A string is a character array. : /
except that strings suck more OOOOOOOOOOOOOOOOOOOOOOHHHH did i just say that???
Last edited on
What did the strings ever do to you, man?!
Last edited on
Yes stings are character arrays? Then why are there cstrings and why do we have this??
string str = "Hello.";
str.c_str(); ?????
The only difference between a character array and a string is that a string is a defined class with functions handling the use of the character array.
http://www.cplusplus.com/reference/string/string/

In memory it is the same thing.
Last edited on
lol i just find that character arrays are more useful if you need access to the individual characters of the string, which if youre getting user input you usually are...?
ascii, you can access the individual characters of a C++ string the exact same way as you can with a char array, with []. But you knew that. :)

-Albatross
@Albatross Sweet. I had my suspicions but forgot to try. Thanks Albatross. :D
of course i knew that, it was a... test?

Topic archived. No new replies allowed.