Thread: Isn't this puzzling?

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    48

    Isn't this puzzling?

    I intend to read several numbers into the array of X and then several others into the array of Z; however, it happens that the following codes can correctly read X but not Z. Only after I change the 'i' in the second loop to 'j' will both read correctly. This is the only 'for' loops in my function.


    Code:
         for (int i=0; i<dimofX_model; ++i) 
    	 	{
            	readfile>>temp.X[i];
             }
         //read in the X variables;
         
    	 for (int i=0; i<dimofZ_model; ++i)		
         	{
    			readfile>>temp.Z[i];
    		}
         //read in the Z variables

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Hmm... I guess your compiler doesn't conform to the scoping rules of for statement initializations. Unless, I'm missing something.
    Sent from my iPadŽ

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    What compiler are you using? It's odd alright.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Using VC6? It's probably what Sly says. Does this work as expected?
    Code:
    {
    for (int i=0; i<dimofX_model; ++i)
    {
      readfile>>temp.X[i];
    }
    }
    //read in the X variables;
    {   
    for (int i=0; i<dimofZ_model; ++i)		
    {
      readfile>>temp.Z[i];
    }
    }
    //read in the Z variables
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Try this -

    Code:
    int i=0;
    
    for (i=0; i<dimofX_model; ++i) 
    {
        readfile>>temp.X[i];
    }
    
    for (i=0; i<dimofZ_model; ++i)		
    {
        readfile>>temp.Z[i];
    }
    and tell us if that works.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    48
    yeah, the last suggestion works. I'm using DevC++. Isn't that disturbing?

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    That's very odd. What version of Dev-C++ do you have? I wasn't aware of any for loop issues like VC6 has.

    In any case, you might want to consider an IDE switch; perhaps to Code::Blocks or Visual C++ Express 2005. I believe there is no longer any active development in Dev-C++
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Aye, 'tis very odd ... :/ Did you try it with a while() loop?

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    48
    By the way, I forgot to say: after I changed it to 'j', compile it and it works; after that I changed it back to 'i', compile it and it works too. Is this helpful for tacking the issue?

  10. #10
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    > By the way, I forgot to say: after I changed it to 'j', compile it and it works

    Perhaps you made other changes before this, and for some reason the project wasn't recompiled to reflect those changes. If I recall correctly, I used to have issues with Dev in this respect (making changes, hitting "Compile and Run," but not actually having the files compiled/linked)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You don't mean to say g++ contains bugs like these... JaWiB, the IDE has nothing at all to do with this issue.
    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;}

  12. #12
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by JaWiB
    > By the way, I forgot to say: after I changed it to 'j', compile it and it works

    Perhaps you made other changes before this, and for some reason the project wasn't recompiled to reflect those changes. If I recall correctly, I used to have issues with Dev in this respect (making changes, hitting "Compile and Run," but not actually having the files compiled/linked)
    This is obviously the case. If you don't use rebuild all sometimes the compiler will not reflect ALL the changes you made and saved earlier.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yeah, sometimes a rebuild all is a good idea just to make sure everything is up to date.
    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.

  14. #14
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Also, you could save the hassle of needing two for loops if you did something like this:

    Code:
    int i=0; // Could prob get rid of this and put it inthe for loop
    
    for (i=0; i<dimofX_model; ++i) 
    {
        readfile>>temp.X[i];
        readfile>>temp.Z[i];
    }
    if you saved the file from which you're reading as follows -

    Code:
    XVar1 ZVar1
    XVar2 ZVar2
    XVar3 ZVar3
    XVar4 ZVar4
    XVar5 ZVar5
    ...
    XVarN ZVarN
    Obviously all the X and Y vars are numbers (I'm assuming). You don't even need them on new lines if you don't want to. I just thought I'd throw that out there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Puzzling pointers...
    By nishkarsh in forum C Programming
    Replies: 6
    Last Post: 09-21-2008, 02:52 AM
  2. Totally puzzling IO problem
    By Vorok in forum C++ Programming
    Replies: 5
    Last Post: 01-06-2003, 07:10 PM