Thread: I need help with variables, please

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    52

    I need help with variables, please

    I'm a begginer in C++. I use Microsoft Visual Studio 6.0. I'm writting a 4x4 Sudoku source code and I've named variables like that:
    a1 b1 c1 d1
    a2 b2 c2 d2
    a3 b3 c3 d3
    a4 b4 c4 d4
    I've used while-if-break senteces and I've put printf sentences beetwen them like in the follow code:

    Code:
    //sudoku.cpp:
    
    #include<stdlib.h>
    #include<math.h>
    #include<stdio.h>
    #include<time.h>
    
    void main(){
    
    srand(time(0));
    
    	int a1;
    	int b1;
    	int c1;
    	int d1;
    	int a2;
    	int b2;
    	int c2;
    	int d2;
            int a3;
    	int b3;
    	int c3;
    	int d3;
            int a4;
    	int b4;
    	int c4;
    	int d4;
    
    srand(time(0));
    
    // 1st row
    
    a1=(rand()%4)+1; 
    
    printf("a1 = %d\n",a1);
    
    while((b1!=0)||(b1=a1)){
    b1=rand()*5/32768;
    if ((b1!=0)&&(b1!=a1)){ 
    break;
    printf("b1 %d\n",b1);
    }}
    
    printf("b1 = %d\n",b1);
    
    while((c1!=0)||(c1=a1)||(c1=b1)){
    c1=rand()*5/32768;
    if ((c1!=0)&&(c1!=a1)&&(c1!=b1)){
    break;
    }}
    printf("c1 = %d\n",c1);
    
    while((d1!=0)||(d1=a1)||(d1=b1)||(d1=c1)){
    d1=rand()*5/32768;
    if ((d1!=0)&&(d1!=a1)&&(d1!=b1)&&(d1!=c1)){
    break;
    }}
    printf("d1 = %d\n",d1);
    
    
    // Column A
    
    while((a2!=0)||(a2=a1)||(a2=b1)){
    a2=rand()*5/32768;
    if ((a2!=0)&&(a2!=a1)&&(a2!=b1)){
    break;
    }}
    
    printf("a2 = %d\n",a2);
    
    while((a3!=0)||(a3=a1)||(a3=a2)){
    a3=rand()*5/32768;
    if ((a3!=0)&&(a3!=a1)&&(a3!=a2)){
    break;
    }}
    
    printf("a3 = %d\n",a3);
    
    
    while((a4!=0)||(a4=a1)||(a4=a2)||(a4=a3)){
    a4=rand()*5/32768;
    if ((a4!=0)&&(a4!=a1)&&(a4!=a2)&&(a4!=a3)){
    break;
    }}
    
    printf("a4 = %d\n",a4);
    
    // Column D
    
    while((d2!=0)||(d2=c1)||(d2=d1)||(d2=a2)){
    d2=rand()*5/32768;
    if ((d2!=0)&&(d2!=c1)&&(d2!=d1)&&(d2!=a2)){
    break;
    }}
    
    printf("d2 = %d\n",d2);
    
    
    while((d3!=0)||(d3=d1)||(d3=d2)||(d3=a3)){
    d3=rand()*5/32768;
    if ((d3!=0)&&(d3!=d1)&&(d3!=d2)&&(d3!=a3)){
    break;
    }}
    
    printf("d3 = %d\n",d3);
    
    
    while((d4!=0)||(d4=d1)||(d4=d2)||(d4=d3)||(d4=a4)){
    d4=rand()*5/32768;
    if ((d4!=0)&&(d4!=d1)&&(d4!=d2)&&(d4!=d3)&&(d4!=a4)){
    break;
    }}
    
    printf("d4 = %d\n",d4);
    
    // 4th row
    
    while((b4!=0)||(b4=a3)||(b4=a4)||(b4=b1)||(b4=d4)){
    b4=rand()*5/32768;
    if ((b4!=0)&&(b4!=a3)&&(b4!=a4)&&(b4!=b1)&&(b4!=d4)){
    break;
    }}
    
    printf("b4 = %d\n",b4);
    
    while((c4!=0)||(c4=d3)||(c4=d4)||(c4=c1)||(c4=a4)||(c4=b4)){
    c4=rand()*5/32768;
    if ((c4!=0)&&(c4!=d3)&&(c4!=d4)&&(c4!=c1)&&(c4!=a4)){
    break;
    }}
    
    printf("c4 = %d\n",c4);
    
    // Central square
    
    while((b2!=0)||(b2=a1)||(b2=b1)||(b2=a2)||(b2=d2)||(b2=b4)){
    b2=rand()*5/32768;
    if ((b2!=0)&&(b2!=a1)&&(b2!=b1)&&(b2!=a2)&&(b2!=d2)&&(b2!=b4)){
    break;
    }}
    
    printf("b2 = %d\n",b2);
    
    while((c2!=0)||(c2=c1)||(c2=d1)||(c2=a2)||(c2=b2)||(c2=d2)||(c2=c4)){
    c2=rand()*5/32768;
    if ((c2!=0)&&(c2!=c1)&&(c2!=d1)&&(c2!=a2)&&(c2!=b2)&&(c2!=d2)&&(c2!=c4)){
    break;
    }}
    
    printf("c2 = %d\n",c2);
    
    while((b3!=0)||(b3=a3)||(b3=a4)||(b3=b4)||(b3=b1)||(b3=b2||(b3=d3))){
    b3=rand()*5/32768;
    if ((b3!=0)&&(b3!=a3)&&(b3!=a4)&&(b3!=b4)&&(b3!=b1)&&(b3!=b2)&&(b3!=d3)){
    break;
    }}
    
    printf("b3 = %d\n",b3);
    
    while((c3!=0)||(c3=c4)||(c3=d4)||(c3=d3)||(c3=c1)||(c3=c2)||(c3=a3)||(c3=b3)){
    c3=rand()*5/32768;
    if ((c3!=0)&&(c3!=c4)&&(c3!=d4)&&(c3!=d3)&&(c3!=c1)&&(c3!=c2)&&(c3!=a3)&&(c3!=b3)){
    break;
    printf("c3 = %d\n",c3);
    
    }}}
    In that code the program works but sometimes it reach a absurd situation and the program stops for example, at the ninth variable:
    2 1 3 4
    4 ? ? 1
    1 ? ? 2
    3 ? ? !
    To solve this issue I've tried to put all the process into another while-if-break senetence in order to finish the program only when the last variable to be determinated (c3) has a valor.
    I've wrote that:

    Code:
    //sudoku.cpp:
    
    #include<stdlib.h>
    #include<math.h>
    #include<stdio.h>
    #include<time.h>
    
    void main(){
    
    srand(time(0));
    
    	int a1;
    	int b1;
    	int c1;
    	int d1;
    	int a2;
    	int b2;
    	int c2;
    	int d2;
            int a3;
    	int b3;
    	int c3;
    	int d3;
            int a4;
    	int b4;
    	int c4;
    	int d4;
    
    srand(time(0));
    
    
    d4=0;
    while(d4=0){
    
    // 1st row
    
    a1=(rand()%4)+1; 
    
    printf("a1 = %d\n",a1);
    
    while((b1!=0)||(b1=a1)){
    b1=rand()*5/32768;
    if ((b1!=0)&&(b1!=a1)){ 
    break;
    printf("b1 %d\n",b1);
    }}
    
    printf("b1 = %d\n",b1);
    
    while((c1!=0)||(c1=a1)||(c1=b1)){
    c1=rand()*5/32768;
    if ((c1!=0)&&(c1!=a1)&&(c1!=b1)){
    break;
    }}
    printf("c1 = %d\n",c1);
    
    while((d1!=0)||(d1=a1)||(d1=b1)||(d1=c1)){
    d1=rand()*5/32768;
    if ((d1!=0)&&(d1!=a1)&&(d1!=b1)&&(d1!=c1)){
    break;
    }}
    printf("d1 = %d\n",d1);
    
    
    // Column A
    
    while((a2!=0)||(a2=a1)||(a2=b1)){
    a2=rand()*5/32768;
    if ((a2!=0)&&(a2!=a1)&&(a2!=b1)){
    break;
    }}
    
    printf("a2 = %d\n",a2);
    
    while((a3!=0)||(a3=a1)||(a3=a2)){
    a3=rand()*5/32768;
    if ((a3!=0)&&(a3!=a1)&&(a3!=a2)){
    break;
    }}
    
    printf("a3 = %d\n",a3);
    
    
    while((a4!=0)||(a4=a1)||(a4=a2)||(a4=a3)){
    a4=rand()*5/32768;
    if ((a4!=0)&&(a4!=a1)&&(a4!=a2)&&(a4!=a3)){
    break;
    }}
    
    printf("a4 = %d\n",a4);
    
    // Column D
    
    while((d2!=0)||(d2=c1)||(d2=d1)||(d2=a2)){
    d2=rand()*5/32768;
    if ((d2!=0)&&(d2!=c1)&&(d2!=d1)&&(d2!=a2)){
    break;
    }}
    
    printf("d2 = %d\n",d2);
    
    
    while((d3!=0)||(d3=d1)||(d3=d2)||(d3=a3)){
    d3=rand()*5/32768;
    if ((d3!=0)&&(d3!=d1)&&(d3!=d2)&&(d3!=a3)){
    break;
    }}
    
    printf("d3 = %d\n",d3);
    
    
    while((d4!=0)||(d4=d1)||(d4=d2)||(d4=d3)||(d4=a4)){
    d4=rand()*5/32768;
    if ((d4!=0)&&(d4!=d1)&&(d4!=d2)&&(d4!=d3)&&(d4!=a4)){
    break;
    }}
    
    printf("d4 = %d\n",d4);
    
    // 4th row
    
    while((b4!=0)||(b4=a3)||(b4=a4)||(b4=b1)||(b4=d4)){
    b4=rand()*5/32768;
    if ((b4!=0)&&(b4!=a3)&&(b4!=a4)&&(b4!=b1)&&(b4!=d4)){
    break;
    }}
    
    printf("b4 = %d\n",b4);
    
    while((c4!=0)||(c4=d3)||(c4=d4)||(c4=c1)||(c4=a4)||(c4=b4)){
    c4=rand()*5/32768;
    if ((c4!=0)&&(c4!=d3)&&(c4!=d4)&&(c4!=c1)&&(c4!=a4)){
    break;
    }}
    
    printf("c4 = %d\n",c4);
    
    // Central square 
    
    while((b2!=0)||(b2=a1)||(b2=b1)||(b2=a2)||(b2=d2)||(b2=b4)){
    b2=rand()*5/32768;
    if ((b2!=0)&&(b2!=a1)&&(b2!=b1)&&(b2!=a2)&&(b2!=d2)&&(b2!=b4)){
    break;
    }}
    
    printf("b2 = %d\n",b2);
    
    while((c2!=0)||(c2=c1)||(c2=d1)||(c2=a2)||(c2=b2)||(c2=d2)||(c2=c4)){
    c2=rand()*5/32768;
    if ((c2!=0)&&(c2!=c1)&&(c2!=d1)&&(c2!=a2)&&(c2!=b2)&&(c2!=d2)&&(c2!=c4)){
    break;
    }}
    
    printf("c2 = %d\n",c2);
    
    while((b3!=0)||(b3=a3)||(b3=a4)||(b3=b4)||(b3=b1)||(b3=b2||(b3=d3))){
    b3=rand()*5/32768;
    if ((b3!=0)&&(b3!=a3)&&(b3!=a4)&&(b3!=b4)&&(b3!=b1)&&(b3!=b2)&&(b3!=d3)){
    break;
    }}
    
    printf("b3 = %d\n",b3);
    
    while((c3!=0)||(c3=c4)||(c3=d4)||(c3=d3)||(c3=c1)||(c3=c2)||(c3=a3)||(c3=b3)){
    c3=rand()*5/32768;
    if ((c3!=0)&&(c3!=c4)&&(c3!=d4)&&(c3!=d3)&&(c3!=c1)&&(c3!=c2)&&(c3!=a3)&&(c3!=b3)){
    break;
    }}
    
    printf("c3 = %d\n",c3);
    
    if (b4!=0){ 
    break;
    }}
    
    printf("a1 = %d\n",a1);
    
    }
    In that code original printf sentences doesn't appears when I run the program and the last one appera like no initialized.
    How can I solve that problem?
    Thank you for your help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. main returns an int
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    2. Learn how to indent code. Not only will it make it much easier for you to follow, when you come to ask for help, people will be more inclined to help you.
    So rather than this
    Code:
    while((a2!=0)||(a2=a1)||(a2=b1)){
    a2=rand()*5/32768;
    if ((a2!=0)&&(a2!=a1)&&(a2!=b1)){
    break;
    }}
    Have this
    Code:
    while((a2!=0)||(a2=a1)||(a2=b1)){
        a2=rand()*5/32768;
        if ((a2!=0)&&(a2!=a1)&&(a2!=b1)){
            break;
        }
    }
    3. Read up on arrays, in particular consider
    int puzzle[4][4];
    in place of 16 named variables.
    You'll then be able to use for loops to sum rows and columns rather than endlessly copy/pasting code and substituting variables.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    52

    Smile

    Thank you for your answer, Salem. I'll try to solve the problem and follow your advices and I'll write my results.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    Hi, I've looked out of the issue "main returns an int" and I've came to the conclusion that I should use "int main ( int argc, char *argv[] )" model. Although I've tried to apply this in my source code and it has generated a large number of errors. How can I specifically implement that in my code. Have I got to put any more specific sentences? Thank you a lot.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can specifically implement that by typing int main() instead of void main().

    What are the error messages?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's hard to help you if you neither post your latest code or your actual error messages. "I got some errors" doesn't tell us much.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    Hi, I was thinking I should use "( int argc, char *argv[] )" model as in http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    I've read "int main ( void )" model is used when you do not require access to the command line arguments bearing in mind that, later, I want to remove some valors that should be entered by an user so I want to make a game. I am right with "( int argc, char *argv[] )" model? Anyway I've tried to use the other one too and I haven't got errors but in that code original printf sentences between while-if-break senetences don't appear when I run the program and the last one appers like no initialized so I don't solve the problem yet. (I've get that warning: warning C4508: 'main' : function should return a value; 'void' return type assumed). Here there is this last code (I've just typed int main() instead of void main()).

    Code:
       
    //sudoku.cpp:
    
    #include<stdlib.h>
    #include<math.h>
    #include<stdio.h>
    #include<time.h>
    
    int main(){
    
    srand(time(0));
    
    	int a1;
    	int b1;
    	int c1;
    	int d1;
    	int a2;
    	int b2;
    	int c2;
    	int d2;
            int a3;
    	int b3;
    	int c3;
    	int d3;
            int a4;
    	int b4;
    	int c4;
    	int d4;
    
    srand(time(0));
    
    
    d4=0;
    while(d4=0){
    
    // 1st row
    
    a1=(rand()%4)+1; 
    
    printf("a1 = %d\n",a1);
    
    while((b1!=0)||(b1=a1)){
    b1=rand()*5/32768;
    if ((b1!=0)&&(b1!=a1)){ 
    break;
    printf("b1 %d\n",b1);
    }}
    
    printf("b1 = %d\n",b1);
    
    while((c1!=0)||(c1=a1)||(c1=b1)){
    c1=rand()*5/32768;
    if ((c1!=0)&&(c1!=a1)&&(c1!=b1)){
    break;
    }}
    printf("c1 = %d\n",c1);
    
    while((d1!=0)||(d1=a1)||(d1=b1)||(d1=c1)){
    d1=rand()*5/32768;
    if ((d1!=0)&&(d1!=a1)&&(d1!=b1)&&(d1!=c1)){
    break;
    }}
    printf("d1 = %d\n",d1);
    
    
    // Column A
    
    while((a2!=0)||(a2=a1)||(a2=b1)){
    a2=rand()*5/32768;
    if ((a2!=0)&&(a2!=a1)&&(a2!=b1)){
    break;
    }}
    
    printf("a2 = %d\n",a2);
    
    while((a3!=0)||(a3=a1)||(a3=a2)){
    a3=rand()*5/32768;
    if ((a3!=0)&&(a3!=a1)&&(a3!=a2)){
    break;
    }}
    
    printf("a3 = %d\n",a3);
    
    
    while((a4!=0)||(a4=a1)||(a4=a2)||(a4=a3)){
    a4=rand()*5/32768;
    if ((a4!=0)&&(a4!=a1)&&(a4!=a2)&&(a4!=a3)){
    break;
    }}
    
    printf("a4 = %d\n",a4);
    
    // Column D
    
    while((d2!=0)||(d2=c1)||(d2=d1)||(d2=a2)){
    d2=rand()*5/32768;
    if ((d2!=0)&&(d2!=c1)&&(d2!=d1)&&(d2!=a2)){
    break;
    }}
    
    printf("d2 = %d\n",d2);
    
    
    while((d3!=0)||(d3=d1)||(d3=d2)||(d3=a3)){
    d3=rand()*5/32768;
    if ((d3!=0)&&(d3!=d1)&&(d3!=d2)&&(d3!=a3)){
    break;
    }}
    
    printf("d3 = %d\n",d3);
    
    
    while((d4!=0)||(d4=d1)||(d4=d2)||(d4=d3)||(d4=a4)){
    d4=rand()*5/32768;
    if ((d4!=0)&&(d4!=d1)&&(d4!=d2)&&(d4!=d3)&&(d4!=a4)){
    break;
    }}
    
    printf("d4 = %d\n",d4);
    
    // 4th row
    
    while((b4!=0)||(b4=a3)||(b4=a4)||(b4=b1)||(b4=d4)){
    b4=rand()*5/32768;
    if ((b4!=0)&&(b4!=a3)&&(b4!=a4)&&(b4!=b1)&&(b4!=d4)){
    break;
    }}
    
    printf("b4 = %d\n",b4);
    
    while((c4!=0)||(c4=d3)||(c4=d4)||(c4=c1)||(c4=a4)||(c4=b4)){
    c4=rand()*5/32768;
    if ((c4!=0)&&(c4!=d3)&&(c4!=d4)&&(c4!=c1)&&(c4!=a4)){
    break;
    }}
    
    printf("c4 = %d\n",c4);
    
    // Central square
    
    while((b2!=0)||(b2=a1)||(b2=b1)||(b2=a2)||(b2=d2)||(b2=b4)){
    b2=rand()*5/32768;
    if ((b2!=0)&&(b2!=a1)&&(b2!=b1)&&(b2!=a2)&&(b2!=d2)&&(b2!=b4)){
    break;
    }}
    
    printf("b2 = %d\n",b2);
    
    while((c2!=0)||(c2=c1)||(c2=d1)||(c2=a2)||(c2=b2)||(c2=d2)||(c2=c4)){
    c2=rand()*5/32768;
    if ((c2!=0)&&(c2!=c1)&&(c2!=d1)&&(c2!=a2)&&(c2!=b2)&&(c2!=d2)&&(c2!=c4)){
    break;
    }}
    
    printf("c2 = %d\n",c2);
    
    while((b3!=0)||(b3=a3)||(b3=a4)||(b3=b4)||(b3=b1)||(b3=b2||(b3=d3))){
    b3=rand()*5/32768;
    if ((b3!=0)&&(b3!=a3)&&(b3!=a4)&&(b3!=b4)&&(b3!=b1)&&(b3!=b2)&&(b3!=d3)){
    break;
    }}
    
    printf("b3 = %d\n",b3);
    
    while((c3!=0)||(c3=c4)||(c3=d4)||(c3=d3)||(c3=c1)||(c3=c2)||(c3=a3)||(c3=b3)){
    c3=rand()*5/32768;
    if ((c3!=0)&&(c3!=c4)&&(c3!=d4)&&(c3!=d3)&&(c3!=c1)&&(c3!=c2)&&(c3!=a3)&&(c3!=b3)){
    break;
    }}
    
    printf("c3 = %d\n",c3);
    
    if (b4!=0){ 
    break;
    }}
    
    printf("a1 = %d\n",a1);
    
    }
    I've tried the "( int argc, char *argv[] )" model too, I dunno if I'm in the right way anyway I've post here it:
    Code:
    //sudoku.cpp:
    
    #include<stdlib.h>
    #include<math.h>
    #include<stdio.h>
    #include<time.h>
    
    int main(int a1, int b1, int c1, int d1, int a2, int b2, int c2, int d2, int a3, int b3, int c3, int d3, int a4, int b4, int c4, int d4){
    
    srand(time(0));
    
    	int a1;
    	int b1;
    	int c1;
    	int d1;
    	int a2;
    	int b2;
    	int c2;
    	int d2;
            int a3;
    	int b3;
    	int c3;
    	int d3;
            int a4;
    	int b4;
    	int c4;
    	int d4;
    
    srand(time(0));
    
    
    d4=0;
    while(d4=0){
    
    // 1st row
    
    a1=(rand()%4)+1; 
    
    printf("a1 = %d\n",a1);
    
    while((b1!=0)||(b1=a1)){
    b1=rand()*5/32768;
    if ((b1!=0)&&(b1!=a1)){ 
    break;
    printf("b1 %d\n",b1);
    }}
    
    printf("b1 = %d\n",b1);
    
    while((c1!=0)||(c1=a1)||(c1=b1)){
    c1=rand()*5/32768;
    if ((c1!=0)&&(c1!=a1)&&(c1!=b1)){
    break;
    }}
    printf("c1 = %d\n",c1);
    
    while((d1!=0)||(d1=a1)||(d1=b1)||(d1=c1)){
    d1=rand()*5/32768;
    if ((d1!=0)&&(d1!=a1)&&(d1!=b1)&&(d1!=c1)){
    break;
    }}
    printf("d1 = %d\n",d1);
    
    
    // Column A
    
    while((a2!=0)||(a2=a1)||(a2=b1)){
    a2=rand()*5/32768;
    if ((a2!=0)&&(a2!=a1)&&(a2!=b1)){
    break;
    }}
    
    printf("a2 = %d\n",a2);
    
    while((a3!=0)||(a3=a1)||(a3=a2)){
    a3=rand()*5/32768;
    if ((a3!=0)&&(a3!=a1)&&(a3!=a2)){
    break;
    }}
    
    printf("a3 = %d\n",a3);
    
    
    while((a4!=0)||(a4=a1)||(a4=a2)||(a4=a3)){
    a4=rand()*5/32768;
    if ((a4!=0)&&(a4!=a1)&&(a4!=a2)&&(a4!=a3)){
    break;
    }}
    
    printf("a4 = %d\n",a4);
    
    // Column D
    
    while((d2!=0)||(d2=c1)||(d2=d1)||(d2=a2)){
    d2=rand()*5/32768;
    if ((d2!=0)&&(d2!=c1)&&(d2!=d1)&&(d2!=a2)){
    break;
    }}
    
    printf("d2 = %d\n",d2);
    
    
    while((d3!=0)||(d3=d1)||(d3=d2)||(d3=a3)){
    d3=rand()*5/32768;
    if ((d3!=0)&&(d3!=d1)&&(d3!=d2)&&(d3!=a3)){
    break;
    }}
    
    printf("d3 = %d\n",d3);
    
    
    while((d4!=0)||(d4=d1)||(d4=d2)||(d4=d3)||(d4=a4)){
    d4=rand()*5/32768;
    if ((d4!=0)&&(d4!=d1)&&(d4!=d2)&&(d4!=d3)&&(d4!=a4)){
    break;
    }}
    
    printf("d4 = %d\n",d4);
    
    // 4th row
    
    while((b4!=0)||(b4=a3)||(b4=a4)||(b4=b1)||(b4=d4)){
    b4=rand()*5/32768;
    if ((b4!=0)&&(b4!=a3)&&(b4!=a4)&&(b4!=b1)&&(b4!=d4)){
    break;
    }}
    
    printf("b4 = %d\n",b4);
    
    while((c4!=0)||(c4=d3)||(c4=d4)||(c4=c1)||(c4=a4)||(c4=b4)){
    c4=rand()*5/32768;
    if ((c4!=0)&&(c4!=d3)&&(c4!=d4)&&(c4!=c1)&&(c4!=a4)){
    break;
    }}
    
    printf("c4 = %d\n",c4);
    
    // Central square
    
    while((b2!=0)||(b2=a1)||(b2=b1)||(b2=a2)||(b2=d2)||(b2=b4)){
    b2=rand()*5/32768;
    if ((b2!=0)&&(b2!=a1)&&(b2!=b1)&&(b2!=a2)&&(b2!=d2)&&(b2!=b4)){
    break;
    }}
    
    printf("b2 = %d\n",b2);
    
    while((c2!=0)||(c2=c1)||(c2=d1)||(c2=a2)||(c2=b2)||(c2=d2)||(c2=c4)){
    c2=rand()*5/32768;
    if ((c2!=0)&&(c2!=c1)&&(c2!=d1)&&(c2!=a2)&&(c2!=b2)&&(c2!=d2)&&(c2!=c4)){
    break;
    }}
    
    printf("c2 = %d\n",c2);
    
    while((b3!=0)||(b3=a3)||(b3=a4)||(b3=b4)||(b3=b1)||(b3=b2||(b3=d3))){
    b3=rand()*5/32768;
    if ((b3!=0)&&(b3!=a3)&&(b3!=a4)&&(b3!=b4)&&(b3!=b1)&&(b3!=b2)&&(b3!=d3)){
    break;
    }}
    
    printf("b3 = %d\n",b3);
    
    while((c3!=0)||(c3=c4)||(c3=d4)||(c3=d3)||(c3=c1)||(c3=c2)||(c3=a3)||(c3=b3)){
    c3=rand()*5/32768;
    if ((c3!=0)&&(c3!=c4)&&(c3!=d4)&&(c3!=d3)&&(c3!=c1)&&(c3!=c2)&&(c3!=a3)&&(c3!=b3)){
    break;
    }}
    
    printf("c3 = %d\n",c3);
    
    if (b4!=0){ 
    break;
    }}
    
    printf("a1 = %d\n",a1);
    
    }
    With this code I get these errors:
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(13) : error C2082: redefinition of formal parameter 'a1'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(14) : error C2082: redefinition of formal parameter 'b1'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(15) : error C2082: redefinition of formal parameter 'c1'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(16) : error C2082: redefinition of formal parameter 'd1'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(17) : error C2082: redefinition of formal parameter 'a2'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(18) : error C2082: redefinition of formal parameter 'b2'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(19) : error C2082: redefinition of formal parameter 'c2'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(20) : error C2082: redefinition of formal parameter 'd2'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(21) : error C2082: redefinition of formal parameter 'a3'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(22) : error C2082: redefinition of formal parameter 'b3'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(23) : error C2082: redefinition of formal parameter 'c3'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(24) : error C2082: redefinition of formal parameter 'd3'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(25) : error C2082: redefinition of formal parameter 'a4'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(26) : error C2082: redefinition of formal parameter 'b4'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(27) : error C2082: redefinition of formal parameter 'c4'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(28) : error C2082: redefinition of formal parameter 'd4'

    And this warning:
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(179) : warning C4508: 'main' : function should return a value; 'void' return type assumed

    If it's the correct way, how to solve these errors? If it's not how to make it work correctly (showing variables initialized)?
    Thank you

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I've read "int main ( void )" model is used when you do not require access to the command line arguments bearing in mind that, later, I want to remove some valors that should be entered by an user so I want to make a game. I am right with "( int argc, char *argv[] )" model?
    That is true: you would use int main() or int main(void) if you didn't want command line arguments, and int main(int argc, char *argv[]) if you did want them.

    Do you know what command-line arguments are? Basically, they are extra text that can be typed when you run the program from the command line. (Command-line arguments can also be specified in shortcuts.) If you just want to get some input from the user, you won't need to be using them.
    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.

  9. #9
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    I would like to help you, but you indentation stinks. Your code is barely readable. Instead of this:

    Code:
    while((d1!=0)||(d1=a1)||(d1=b1)||(d1=c1)){
    d1=rand()*5/32768;
    if ((d1!=0)&&(d1!=a1)&&(d1!=b1)&&(d1!=c1)){
    break;
    }}
    printf("d1 = %d\n",d1);
    Code:
    while( (d1 != 0) || (d1 = a1) || (d1 = b1) || (d1 = c1) )
    {
        d1=rand()*5/32768;
        if ( (d1 != 0) && (d1 != a1) && (d1 != b1) && (d1 != c1))
        {
               break;
        }
    }
    printf("d1 = %d\n",d1);
    Just from that small segement you can easily see that you are doing variable assignment in a conditional. You want to use

    Code:
    while( (d1 != 0) || (d1 == a1) || (d1 == b1) || (d1 == c1) )
    {
        d1=rand()*5/32768;
        if ( (d1 != 0) && (d1 != a1) && (d1 != b1) && (d1 != c1))
        {
               break;
        }
    }
    printf("d1 = %d\n",d1);

  10. #10
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    Hi,
    you would use int main() or int main(void) if you didn't want command line arguments, and int main(int argc, char *argv[]) if you did want them.

    Do you know what command-line arguments are? Basically, they are extra text that can be typed when you run the program from the command line. (Command-line arguments can also be specified in shortcuts.) If you just want to get some input from the user, you won't need to be using them.
    I want to get some input from user but I want to compare the user's answer with values got with that code in order to determinate their validity so I think I should use int main(int argc, char *argv[]). That's right? (I dunno if it has anything to do with that but later I want to use arrays in place of 16 variables, too)
    I've post the code here:
    Code:
    //sudoku.cpp:
    
    #include<stdlib.h>
    #include<math.h>
    #include<stdio.h>
    #include<time.h>
    
    int main(int a1, int b1, int c1, int d1, int a2, int b2, int c2, int d2, int a3, int b3, int c3, int d3, int a4, int b4, int c4, int d4){
    
    srand(time(0));
    
    	int a1;
    	int b1;
    	int c1;
    	int d1;
    	int a2;
    	int b2;
    	int c2;
    	int d2;
            int a3;
    	int b3;
    	int c3;
    	int d3;
            int a4;
    	int b4;
    	int c4;
    	int d4;
    
    srand(time(0));
    
    
    d4=0;
    
    while(d4=0)
    {
    
    
    
    // 1st row
    
    a1=(rand()%4)+1; 
    
    printf("a1 = %d\n",a1);
    
    while((b1!=0)||(b1=a1))
    {
       b1=rand()*5/32768;
       if ((b1!=0)&&(b1!=a1))
       { 
          break;
       }  
    }  
    printf("b1 = %d\n",b1);
    
    
    
    while((c1!=0)||(c1=a1)||(c1=b1))
    {
       c1=rand()*5/32768;
       if ((c1!=0)&&(c1!=a1)&&(c1!=b1))
       {
          break;
       }
    } 
    printf("c1 = %d\n",c1);
    
    
    
    while((d1!=0)||(d1=a1)||(d1=b1)||(d1=c1))
    {
       d1=rand()*5/32768;
       if ((d1!=0)&&(d1!=a1)&&(d1!=b1)&&(d1!=c1))
       {
         break;
       }
    }  
    printf("d1 = %d\n",d1);
    
    
    
    // Column A
    
    while((a2!=0)||(a2=a1)||(a2=b1))
    {
       a2=rand()*5/32768;
       if ((a2!=0)&&(a2!=a1)&&(a2!=b1))
       {
         break;
       }
    }
    printf("a2 = %d\n",a2);
    
    
    
    while((a3!=0)||(a3=a1)||(a3=a2))
    {
       a3=rand()*5/32768;
       if ((a3!=0)&&(a3!=a1)&&(a3!=a2))
       {
         break;
       }
    }
    printf("a3 = %d\n",a3);
    
    
    
    while((a4!=0)||(a4=a1)||(a4=a2)||(a4=a3))
    {
       a4=rand()*5/32768;
       if ((a4!=0)&&(a4!=a1)&&(a4!=a2)&&(a4!=a3))
       {
         break;
       }
    }
    printf("a4 = %d\n",a4);
    
    
    
    // Column D
    
    while((d2!=0)||(d2=c1)||(d2=d1)||(d2=a2))
    {
       d2=rand()*5/32768;
       if ((d2!=0)&&(d2!=c1)&&(d2!=d1)&&(d2!=a2))
       {
         break;
       }
    }
    printf("d2 = %d\n",d2);
    
    
    
    while((d3!=0)||(d3=d1)||(d3=d2)||(d3=a3))
    {
       d3=rand()*5/32768;
       if ((d3!=0)&&(d3!=d1)&&(d3!=d2)&&(d3!=a3))
       {
         break;
       }
    }
    printf("d3 = %d\n",d3);
    
    
    
    while((d4!=0)||(d4=d1)||(d4=d2)||(d4=d3)||(d4=a4))
    {
       d4=rand()*5/32768;
       if ((d4!=0)&&(d4!=d1)&&(d4!=d2)&&(d4!=d3)&&(d4!=a4))
       {
         break;
       }
    }
    printf("d4 = %d\n",d4);
    
    
    
    // 4th row
    
    while((b4!=0)||(b4=a3)||(b4=a4)||(b4=b1)||(b4=d4))
    {
       b4=rand()*5/32768;
       if ((b4!=0)&&(b4!=a3)&&(b4!=a4)&&(b4!=b1)&&(b4!=d4)){
         break;
       }
    }
    printf("b4 = %d\n",b4);
    
    while((c4!=0)||(c4=d3)||(c4=d4)||(c4=c1)||(c4=a4)||(c4=b4))
    {
       c4=rand()*5/32768;
       if ((c4!=0)&&(c4!=d3)&&(c4!=d4)&&(c4!=c1)&&(c4!=a4))
       {
         break;
       }
    }
    printf("c4 = %d\n",c4);
    
    
    
    // Central square
    
    while((b2!=0)||(b2=a1)||(b2=b1)||(b2=a2)||(b2=d2)||(b2=b4))
    {
       b2=rand()*5/32768;
       if ((b2!=0)&&(b2!=a1)&&(b2!=b1)&&(b2!=a2)&&(b2!=d2)&&(b2!=b4)){
         break;
       }
    }
    printf("b2 = %d\n",b2);
    
    
    
    while((c2!=0)||(c2=c1)||(c2=d1)||(c2=a2)||(c2=b2)||(c2=d2)||(c2=c4))
    {
       c2=rand()*5/32768;
       if ((c2!=0)&&(c2!=c1)&&(c2!=d1)&&(c2!=a2)&&(c2!=b2)&&(c2!=d2)&&(c2!=c4)){
         break;
       }
    }
    printf("c2 = %d\n",c2);
    
    
    
    while((b3!=0)||(b3=a3)||(b3=a4)||(b3=b4)||(b3=b1)||(b3=b2||(b3=d3)))
    {
      b3=rand()*5/32768;
      if ((b3!=0)&&(b3!=a3)&&(b3!=a4)&&(b3!=b4)&&(b3!=b1)&&(b3!=b2)&&(b3!=d3)){
        break;
      }
    }
    printf("b3 = %d\n",b3);
    
    
    
    while((c3!=0)||(c3=c4)||(c3=d4)||(c3=d3)||(c3=c1)||(c3=c2)||(c3=a3)||(c3=b3))
    {
       c3=rand()*5/32768;
       if ((c3!=0)&&(c3!=c4)&&(c3!=d4)&&(c3!=d3)&&(c3!=c1)&&(c3!=c2)&&(c3!=a3)&&(c3!=b3)){
         break;
       }
    }
    printf("c3 = %d\n",c3);
    
    
    
    
    
    if (b4!=0)
       {
         break;
       }
    }  
    
    printf("a1 = %d\n",a1);
    
    }
    With this code I get these errors:
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(13) : error C2082: redefinition of formal parameter 'a1'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(14) : error C2082: redefinition of formal parameter 'b1'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(15) : error C2082: redefinition of formal parameter 'c1'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(16) : error C2082: redefinition of formal parameter 'd1'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(17) : error C2082: redefinition of formal parameter 'a2'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(18) : error C2082: redefinition of formal parameter 'b2'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(19) : error C2082: redefinition of formal parameter 'c2'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(20) : error C2082: redefinition of formal parameter 'd2'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(21) : error C2082: redefinition of formal parameter 'a3'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(22) : error C2082: redefinition of formal parameter 'b3'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(23) : error C2082: redefinition of formal parameter 'c3'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(24) : error C2082: redefinition of formal parameter 'd3'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(25) : error C2082: redefinition of formal parameter 'a4'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(26) : error C2082: redefinition of formal parameter 'b4'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(27) : error C2082: redefinition of formal parameter 'c4'
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(28) : error C2082: redefinition of formal parameter 'd4'

    And this warning:
    C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(179) : warning C4508: 'main' : function should return a value; 'void' return type assumed
    How to solve them?

    Thank you

  11. #11
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    You are not supposed to call main with all those variables, if you wan't to use command line arguments, you simply just have to type "int main(int argc, char *argv[])".

    But i don't think you need that in your program, if you just wan't to get input from the user, just call main like this: "int main()".

  12. #12
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    i don't think you need that in your program, if you just wan't to get input from the user, just call main like this: "int main()"
    I've done it but printf sentences between while-if-break senetences don't appear when I run the program and the last one appers like no initialized. I get the following warning: C:\Documents and Settings\-\Mis documentos\Institut\TDR\TDR (Programació en C)\SUDOKU\Sudoku\4\4.cpp(237) : warning C4508: 'main' : function should return a value; 'void' return type assumed
    How to solve that?

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And to fix your warning, add "return 0;" to the end of your "main".

    --
    Mats

  14. #14
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    And to fix your warning, add "return 0;" to the end of your "main"
    I've done it and the warning has been fixed but printf sentences between while-if-break senetences don't appear when I run the program and the last one appers like no initialized already.
    How to fix this issue?
    Thank you
    My code:
    Code:
    //sudoku.cpp:
    
    #include<stdlib.h>
    #include<math.h>
    #include<stdio.h>
    #include<time.h>
    
    int main(){
    
    srand(time(0));
    
    	int a1;
    	int b1;
    	int c1;
    	int d1;
    	int a2;
    	int b2;
    	int c2;
    	int d2;
        int a3;
    	int b3;
    	int c3;
    	int d3;
        int a4;
    	int b4;
    	int c4;
    	int d4;
    
    srand(time(0));
    
    
    d4=0;
    
    while(d4=0)
    {
    
    
    
    // 1st row
    
    a1=(rand()%4)+1; 
    
    printf("a1 = %d\n",a1);
    
    while((b1!=0)||(b1=a1))
    {
       b1=rand()*5/32768;
       if ((b1!=0)&&(b1!=a1))
       { 
          break;
       }  
    }  
    printf("b1 = %d\n",b1);
    
    
    
    while((c1!=0)||(c1=a1)||(c1=b1))
    {
       c1=rand()*5/32768;
       if ((c1!=0)&&(c1!=a1)&&(c1!=b1))
       {
          break;
       }
    } 
    printf("c1 = %d\n",c1);
    
    
    
    while((d1!=0)||(d1=a1)||(d1=b1)||(d1=c1))
    {
       d1=rand()*5/32768;
       if ((d1!=0)&&(d1!=a1)&&(d1!=b1)&&(d1!=c1))
       {
         break;
       }
    }  
    printf("d1 = %d\n",d1);
    
    
    
    // Column A
    
    while((a2!=0)||(a2=a1)||(a2=b1))
    {
       a2=rand()*5/32768;
       if ((a2!=0)&&(a2!=a1)&&(a2!=b1))
       {
         break;
       }
    }
    printf("a2 = %d\n",a2);
    
    
    
    while((a3!=0)||(a3=a1)||(a3=a2))
    {
       a3=rand()*5/32768;
       if ((a3!=0)&&(a3!=a1)&&(a3!=a2))
       {
         break;
       }
    }
    printf("a3 = %d\n",a3);
    
    
    
    while((a4!=0)||(a4=a1)||(a4=a2)||(a4=a3))
    {
       a4=rand()*5/32768;
       if ((a4!=0)&&(a4!=a1)&&(a4!=a2)&&(a4!=a3))
       {
         break;
       }
    }
    printf("a4 = %d\n",a4);
    
    
    
    // Column D
    
    while((d2!=0)||(d2=c1)||(d2=d1)||(d2=a2))
    {
       d2=rand()*5/32768;
       if ((d2!=0)&&(d2!=c1)&&(d2!=d1)&&(d2!=a2))
       {
         break;
       }
    }
    printf("d2 = %d\n",d2);
    
    
    
    while((d3!=0)||(d3=d1)||(d3=d2)||(d3=a3))
    {
       d3=rand()*5/32768;
       if ((d3!=0)&&(d3!=d1)&&(d3!=d2)&&(d3!=a3))
       {
         break;
       }
    }
    printf("d3 = %d\n",d3);
    
    
    
    while((d4!=0)||(d4=d1)||(d4=d2)||(d4=d3)||(d4=a4))
    {
       d4=rand()*5/32768;
       if ((d4!=0)&&(d4!=d1)&&(d4!=d2)&&(d4!=d3)&&(d4!=a4))
       {
         break;
       }
    }
    printf("d4 = %d\n",d4);
    
    
    
    // 4th row
    
    while((b4!=0)||(b4=a3)||(b4=a4)||(b4=b1)||(b4=d4))
    {
       b4=rand()*5/32768;
       if ((b4!=0)&&(b4!=a3)&&(b4!=a4)&&(b4!=b1)&&(b4!=d4)){
         break;
       }
    }
    printf("b4 = %d\n",b4);
    
    while((c4!=0)||(c4=d3)||(c4=d4)||(c4=c1)||(c4=a4)||(c4=b4))
    {
       c4=rand()*5/32768;
       if ((c4!=0)&&(c4!=d3)&&(c4!=d4)&&(c4!=c1)&&(c4!=a4))
       {
         break;
       }
    }
    printf("c4 = %d\n",c4);
    
    
    
    // Central square
    
    while((b2!=0)||(b2=a1)||(b2=b1)||(b2=a2)||(b2=d2)||(b2=b4))
    {
       b2=rand()*5/32768;
       if ((b2!=0)&&(b2!=a1)&&(b2!=b1)&&(b2!=a2)&&(b2!=d2)&&(b2!=b4)){
         break;
       }
    }
    printf("b2 = %d\n",b2);
    
    
    
    while((c2!=0)||(c2=c1)||(c2=d1)||(c2=a2)||(c2=b2)||(c2=d2)||(c2=c4))
    {
       c2=rand()*5/32768;
       if ((c2!=0)&&(c2!=c1)&&(c2!=d1)&&(c2!=a2)&&(c2!=b2)&&(c2!=d2)&&(c2!=c4)){
         break;
       }
    }
    printf("c2 = %d\n",c2);
    
    
    
    while((b3!=0)||(b3=a3)||(b3=a4)||(b3=b4)||(b3=b1)||(b3=b2||(b3=d3)))
    {
      b3=rand()*5/32768;
      if ((b3!=0)&&(b3!=a3)&&(b3!=a4)&&(b3!=b4)&&(b3!=b1)&&(b3!=b2)&&(b3!=d3)){
        break;
      }
    }
    printf("b3 = %d\n",b3);
    
    
    
    while((c3!=0)||(c3=c4)||(c3=d4)||(c3=d3)||(c3=c1)||(c3=c2)||(c3=a3)||(c3=b3))
    {
       c3=rand()*5/32768;
       if ((c3!=0)&&(c3!=c4)&&(c3!=d4)&&(c3!=d3)&&(c3!=c1)&&(c3!=c2)&&(c3!=a3)&&(c3!=b3)){
         break;
       }
    }
    printf("c3 = %d\n",c3);
    
    
    
    
    
    if (b4!=0)
       {
         break;
       }
    }  
    
    printf("a1 = %d\n",a1);
    return 0;
    }

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    while (d4=0)
    will not execute anything, as it changes d4 to zero, then evaluates the result (zero) to see if it should do the "while" - and since zero means "false", it doesn't perform the loop.

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  5. Replies: 6
    Last Post: 01-02-2004, 01:01 PM