Noob Q 2D array processing
I am unable to check if the character in the 2D array isalpha
here is my code
#include<iostream>
#include<cctype>
#include<cassert>
#include<cstring>
const int maxChar = 20 ;
using namespace std ;
int main()
{
int count = 0 ;
char d[2][20] = { "hi" , "di" } ;
for(int r = 0 ; r < 2 ; r++)
{
cout << "loop1 " ;
for ( int c = 0 ; c != '\0' && c < 20 ; c++)
{
if (isalpha( d[r][c]))
{
cout << d[r][c] ;
}
else
cout << "nooooooo " ;
}
count++ ;
}
cout << "\n count is" << count ;
}
Re: Noob Q 2D array processing
More info : my inner loop isnt executing. why?
Re: Noob Q 2D array processing
for (int c = 0 ; c != '\0'...
What do you think that's going to do? You set c to zero, then execute the loop as long as c is not equal zero.
Re: Noob Q 2D array processing
I thought that the 2nd statement is a conditional statement, meaning it does not assign value to c but just checks? am I correct?
Re: Noob Q 2D array processing
Quote:
Originally Posted by
naildirt
More info : my inner loop isnt executing. why?
You have to debug your code to see what happens with your variables and why this loop "isnt executing" (if it really is not executed!)
Re: Noob Q 2D array processing
The error is in the conditional statement of the second for loop. If I used the strnlen function, I dont get any trouble
Re: Noob Q 2D array processing
Quote:
Originally Posted by
naildirt
Code:
...
int main()
{
int count = 0 ;
char d[2][20] = { "hi" , "di" } ;
...
}
What type of data is this char d[2][20] supposed to contain?
Do you know that "c string" must be NULL-terminated?
Re: Noob Q 2D array processing
The type of data is char. I thought when I initialize char d[] = "hi", the size is 3, meaning the compiler automatically adds a null character to the end
Re: Noob Q 2D array processing
Quote:
Originally Posted by
naildirt
The error is in the conditional statement of the second for loop. If I used the strnlen function, I dont get any trouble
I don't know how much more clearly to explain it. You set c to zero, then execute the loop while c is not zero. Of course it won't execute.