help!! problems with if stream

This is a discussion on help!! problems with if stream within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main() { int i, j, k, match, tmp[4] = {4, 12, 25, 30}; int num[4][4] = ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    somewhere in terran
    Posts
    12

    help!! problems with if stream

    Code:
    #include <stdio.h>
    int main() {
         int i, j, k, match, tmp[4] = {4, 12, 25, 30}; 
         int num[4][4] = { {34, 55, 125, 55}, {4, 515, 25, 11},{34, 5, 12, 65},{24, 15, 625, 35}};
         
         for(i=0;i<4;i++){
         printf("The matched numbers of row %d are:\n", i+1);
         for(j=0;j<4;j++){
         for(k=0;k<4;k++)
         if(num[i][j]==tmp[k])
            printf("%4d", tmp[k]);
           match = match + 1;
           }
           
    if(match = 0)
        
       printf("nil");
       printf("\n");}
    
         system("pause");
         return 0;
    }
    the program will find the matched numbers and should show "nil" when any number matched in that array.

    the output should be:
    Code:
    the matched numbers of row 1 are:
    nil
    the matched numbers of row 2 are:
    4  25
    ...etc
    the problem now is that when there isn't any numbers matched, the "nil" doesn't appear....
    why?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    match = 0 probably should be match == 0

    Oh, and kindly indent your code properly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    if(match ==0) !!!

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by laserlight View Post
    match = 0 probably should be match == 0

    Oh, and kindly indent your code properly.
    Damn, laserlight you are always sooo fast

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    wow so fast...

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    somewhere in terran
    Posts
    12
    I tried and still it doesn't work.........

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I don't even know how your program compiles given the fact that you don't even match brackets. Please post your updated code.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Location
    somewhere in terran
    Posts
    12
    broken brackets?
    are there?
    Code:
    #include <stdio.h>
    int main() {
         int i, j, k, match, tmp[4] = {4, 12, 25, 30}; 
         int num[4][4] = { {34, 55, 125, 55}, {4, 515, 25, 11},{34, 5, 12, 65},{24, 15, 625, 35}};
         
         for(i=0;i<4;i++){//first bracket
         printf("The matched numbers of row %d are:\n", i+1);
         for(j=0;j<4;j++){//second brackets
         for(k=0;k<4;k++)
         if(num[i][j]==tmp[k])
            printf("%4d", tmp[k]);
           match = match ++;
           }//second ends here
           
    if(match==0)
        
       printf("nil");
       printf("\n");
    }//first ends here
    
         system("pause");
         return 0;
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    You did not initialise match.

    EDIT:
    Quote Originally Posted by mkmk007
    broken brackets?
    are there?
    You would not need those comments if you indented your code properly, e.g.,
    Code:
    #include <stdio.h>
    int main() {
        int i, j, k, match, tmp[4] = {4, 12, 25, 30};
        int num[4][4] = {{34, 55, 125, 55}, {4, 515, 25, 11}, {34, 5, 12, 65}, {24, 15, 625, 35}};
    
        for (i = 0; i < 4; i++) {
            printf("The matched numbers of row %d are:\n", i + 1);
            for (j = 0; j < 4; j++) {
                for (k = 0; k < 4; k++)
                    if (num[i][j] == tmp[k])
                        printf("%4d", tmp[k]);
                match = match++;
            }
    
            if (match == 0)
                printf("nil");
            printf("\n");
        }
    
        system("pause");
        return 0;
    }
    Also, note that this is wrong:
    Code:
    match = match++;
    You should just write:
    Code:
    match++;
    Furthermore, the indentation reveals a logic error in your match counting.
    Last edited by laserlight; 03-21-2010 at 03:14 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Oct 2009
    Location
    somewhere in terran
    Posts
    12
    thanks, sorry for being a noob

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fstream problems
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2002, 09:44 AM
  2. Redirecting cout stream
    By Arrow Mk84 in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2002, 04:17 PM
  3. Input file stream problems
    By Shadow12345 in forum C++ Programming
    Replies: 10
    Last Post: 10-06-2002, 06:33 PM
  4. Replies: 9
    Last Post: 07-01-2002, 07:50 AM
  5. What's wrong with my Stream Cipher Encryption?
    By Davros in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2002, 09:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21