Char array limit
Hi,
I am pretty new to c++ and am currently working on user input error checking for some code.
I was wondering how I could check something like this:
1 2 3 4 5 6 7 8
|
myChar[5];
for(int i = 0; i < 5; i++)
{
myChar[i] = cin.get();
}
|
So that if a user enters more than 5 characters, i.e.(ghttrr) \n.
I can prompt for a re-input or even ignore past the 5th char rather than my code going haywire.
Thanks for any help.
Do you have a reason for not using a string?
1 2
|
if(my_string.length() < 5)
cout << "that was too long";
|
Sorry, I should have been more thorough,
The whole code is more similar to:
1 2 3 4 5 6 7 8 9 10 11 12
|
myChar[5];
int count;
for(int i = 0; i < 5; i++)
{
myChar[i] = cin.get();
if(myChar[i] == 'R' || myChar[i] == 'r')
{
++count
}
}
|
In that I am checking each character for a condition .
That still does not change anything using this condition:
I can prompt for a re-input |
1 2 3 4 5
|
for(int i = 0; i < my_string.length(); i++)
{
if(my_string[i] == 'R' || my_string[i] == 'r')
count++;
}
|
Topic archived. No new replies allowed.