CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2012
    Posts
    6

    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 ;

    }

  2. #2
    Join Date
    Nov 2012
    Posts
    6

    Re: Noob Q 2D array processing

    More info : my inner loop isnt executing. why?

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  4. #4
    Join Date
    Nov 2012
    Posts
    6

    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?

  5. #5
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Noob Q 2D array processing

    Quote Originally Posted by naildirt View Post
    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!)
    Victor Nijegorodov

  6. #6
    Join Date
    Nov 2012
    Posts
    6

    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

  7. #7
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Noob Q 2D array processing

    Quote Originally Posted by naildirt View Post
    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?
    Last edited by VictorN; November 21st, 2012 at 04:36 AM.
    Victor Nijegorodov

  8. #8
    Join Date
    Nov 2012
    Posts
    6

    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

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Noob Q 2D array processing

    Quote Originally Posted by naildirt View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured