Thread: Passing Arrays into functions

  1. #16
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Code:
    if (surname[1] != 1 || surname[1] != 2 || surname[1] != 3 || surname[1] != 4 || surname[1] != 5 || surname[1] != 6 || surname[1] != 0){
            surname[1] = '0';
            }
    Should be:
    Code:
    if (surname[1] != '1' || surname[1] != '2' || surname[1] != '3' || surname[1] != '4' || surname[1] != '5' || surname[1] != '6' || surname[1] != '0'){
            surname[1] = '0';
            }
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  2. #17
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    still doesn't seem to work, whatever the value of the character in the array is it replaces it with a zero.

    Edit:

    although I did think of doing it like this, but I get an error in compiling as it doesn't seem to like the else command.

    Code:
    if (surname[1] == '1' || surname[1] == '2' || surname[1] == '3' || surname[1] == '4' || surname[1] == '5' || surname[1] == '6' || surname[1] == '0'){
            else
            surname[1] = '0';
            }
    Last edited by manutdfan; 11-13-2006 at 05:12 PM.

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by manutdfan
    still doesn't seem to work, whatever the value of the character in the array is it replaces it with a zero.
    Try actually looking at your code, specifically your braces.
    Code:
    if ( ){
            else
            surname[1] = '0';
            }
    What exactly is that else doing in there? What if does it belong to?


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    thanks a lot it is working now properly.

  5. #20
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    hmm, that wasn't in the code I looked at, was it?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  6. #21
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    no it was in the code I posted in post 17, although I couldn't get the other piece to work correctly.

  7. #22
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Weird.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  8. #23
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Code:
    if (surname[1] != '1' || surname[1] != '2' || surname[1] != '3' || surname[1] != '4'
    || surname[1] != '5' || surname[1] != '6' || surname[1] != '0'){
            surname[1] = '0';
            }
    This will always set surname[1] to '0'. If surname is equal to '1', then (surname[1] != '2') will be true and surname[1] will be set to '0'. If surname[1] is anything but '1', (surname[1] != '1') will be true, and again, surname[1] will be set to '0'.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  9. #24
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I think the OP's figured that out; see the latest version of that code.
    Code:
    if (surname[1] == '1' || surname[1] == '2' || surname[1] == '3' || surname[1] == '4' || surname[1] == '5' || surname[1] == '6' || surname[1] == '0'){
            else
            surname[1] = '0';
            }
    Presumably modified as Quzah said.

    In any case, it would be easier to use < and >:
    Code:
    if(surname[1] < '0' && surname[1] > '6') surname[1] = '0';
    I think that's what the OP intended . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  3. Functions returning char arrays
    By turmoil in forum C Programming
    Replies: 3
    Last Post: 05-27-2003, 01:43 AM
  4. passing arrays through functions
    By Ecko in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2003, 08:21 PM
  5. Passing multidimensional arrays to functions
    By maxthecat in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 03:58 PM