-
November 20th, 2012, 07:58 AM
#1
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 ;
}
-
November 20th, 2012, 08:05 AM
#2
Re: Noob Q 2D array processing
More info : my inner loop isnt executing. why?
-
November 20th, 2012, 08:16 AM
#3
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.
-
November 20th, 2012, 09:01 AM
#4
Re: Noob Q 2D array processing
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!)
Victor Nijegorodov
-
November 20th, 2012, 08:30 AM
#5
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?
-
November 20th, 2012, 09:04 AM
#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
-
November 20th, 2012, 03:52 PM
#7
Re: Noob Q 2D array processing
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.
-
November 20th, 2012, 09:05 AM
#8
Re: Noob Q 2D array processing
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?
Last edited by VictorN; November 21st, 2012 at 04:36 AM.
Victor Nijegorodov
-
November 20th, 2012, 09:34 AM
#9
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|