dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 24

Thread: for() statement stops at 3rd interation

Threaded View

  1. #1
    Join Date
    Jul 2007
    Posts
    33

    for() statement stops at 3rd interation

    Hello.
    I'm having a problem with this computer program. Suffice it to say that I have narrowed down the problem to the exact statement that seems to be malfunctioning. Why elementary C++ statements don't work the same every time is a mystery to me. Honestly, I'm pretty fed up with C++. Too many times I've written a computer program perfectly and by the book, only to find that there's some unfathomable, and subsequently crippling, kink in the source code. If you know of a programming language that isn't as syntactically strict or so demanding of hours of troubleshooting, please let me know because that is what I need to be learning.

    Anyways, this program starts by asking for the number of output frames to produce (typing 1 will suffice for troubleshooting purposes). It then asks for the value of # in the file name LCinit#.ppm. This part only exists because I have more than one LCinit file that can be used (I've been using LCinit2.ppm for troubleshooting. This file is attached as a text file). The program output includes the number of frames that you asked to be produced. These frames should, in theory, be replicas of LCinit#.ppm. The program also outputs a file called debug.txt, which is composed of rows, that display the following information regarding LCinit#.ppm pixel upload, in the following format: [current_pixel] ([red_value]@[file_location],[green_value]@[file_location],[blue_value]@[file_location],) [color_category]

    The problem is (and you can verify this in debug.txt) the line of code, for(i=0;i<pw*ph;i++) in the function void getinitial(), stops before the 3rd iteration (i=2) even though pw*ph is equal to 32. Why this occurs, I can not even imagine.
    If you can help me figure this out and correct the problem, I would greatly appreciate it.

    Code:
    #include<iostream>
    #include<math.h>
    #include<fstream>
    #include<stdlib.h>
    #include<cstring>
    #define pi 3.14159265
    using namespace std;
    
    void cpt(int n)
         {cout<<" Checkpoint "<<n<<" "; system("PAUSE");}
    
    int pw,ph,res,x;
    double* a=NULL; double* b=NULL;
    char temp[20], tempb[20];
    double tempc[3];
    
    void putbina()
         {int i;
         for(i=0;i<pw*ph;i++)
                             {a[i]=b[i]; b[i]=0;}
         }
    
    double anglecalc(double current)
           {return atan((((current-fmod(current,pw))/pw)+1)/fmod(current,pw));}  //vector is defined within atan(f(y)/f(x))
           
    double magcalc(int current)
           {return sqrt(pow((current-(current%pw))/pw,2)+pow(current%pw,2));}
    
    void processpix(int current)
        {double theta;
        int i;
        theta=anglecalc(current);
        i=int(theta/(pi/4));
        switch(i)
                 {case 0: b[current]=(a[current]+((fmod(theta,(pi/4))/(pi/4))*a[current+1]*magcalc(current))+((1-(fmod(theta,(pi/4))/(pi/4)))*a[current+1-pw]*magcalc(current)))/magcalc(current); break;
                 case 1: b[current]=(a[current]+((fmod(theta,(pi/4))/(pi/4))*a[current+1-pw]*magcalc(current))+((1-(fmod(theta,(pi/4))/(pi/4)))*a[current-pw]*magcalc(current)))/magcalc(current); break;
                 case 2: b[current]=(a[current]+((fmod(theta,(pi/4))/(pi/4))*a[current-pw]*magcalc(current))+((1-(fmod(theta,(pi/4))/(pi/4)))*a[current-1-pw]*magcalc(current)))/magcalc(current); break;
                 case 3: b[current]=(a[current]+((fmod(theta,(pi/4))/(pi/4))*a[current-1-pw]*magcalc(current))+((1-(fmod(theta,(pi/4))/(pi/4)))*a[current-1]*magcalc(current)))/magcalc(current); break;
                 case 4: b[current]=(a[current]+((fmod(theta,(pi/4))/(pi/4))*a[current-1]*magcalc(current))+((1-(fmod(theta,(pi/4))/(pi/4)))*a[current-1+pw]*magcalc(current)))/magcalc(current); break;
                 case 5: b[current]=(a[current]+((fmod(theta,(pi/4))/(pi/4))*a[current-1+pw]*magcalc(current))+((1-(fmod(theta,(pi/4))/(pi/4)))*a[current+pw]*magcalc(current)))/magcalc(current); break;
                 case 6: b[current]=(a[current]+((fmod(theta,(pi/4))/(pi/4))*a[current+pw]*magcalc(current))+((1-(fmod(theta,(pi/4))/(pi/4)))*a[current+1+pw]*magcalc(current)))/magcalc(current); break;
                 case 7: b[current]=(a[current]+((fmod(theta,(pi/4))/(pi/4))*a[current+1+pw]*magcalc(current))+((1-(fmod(theta,(pi/4))/(pi/4)))*a[current+1]*magcalc(current)))/magcalc(current); break;
                 default: break;}
        }
    
    void cleartemp(char tempn[20])
         {int a;
         for(a=0; a<20; a++)temp[a]='\0';}
    
    void getinitial()
        {char file[10]={"LCinit"};
        cout<<"File to upload:  LCinit#.ppm\n# = ";
        cin>>temp;
        strcat(file,temp);
        cleartemp(temp);
        strcat(file,".ppm");
        ifstream lcinit(file, std::ios_base::ate);
        int strplace[2], filesize=lcinit.tellg();
        string tempfile;
        tempfile.resize(filesize);
        lcinit.seekg(0);
        for(strplace[0]=0; strplace[0]<filesize-100; strplace[0]++)
                 {tempfile[strplace[0]]=lcinit.get();}
        lcinit.close();
        strplace[0]=strplace[1]=0;
        cleartemp(temp);
        strplace[0]=tempfile.find("#",0,1); strplace[0]=strplace[0]+2; strplace[1]=tempfile.find(" ",strplace[0],1); tempfile.copy(temp,strplace[1]-strplace[0],strplace[0]); pw=atoi(temp); cleartemp(temp);
        strplace[1]++; strplace[0]=tempfile.find(" ",strplace[1],1); tempfile.copy(temp,strplace[0]-strplace[1],strplace[1]); ph=atoi(temp); cleartemp(temp);
        strplace[0]++; strplace[1]=tempfile.find(" ",strplace[0]); tempfile.copy(temp,strplace[1]-strplace[0],strplace[0]); res=atoi(temp); cleartemp(temp);
        a=new double[pw*ph];
        b=new double[pw*ph];
        int i,j;
        strplace[0]=strplace[1];
        ofstream debug ("debug.txt", ios::ate);
        for(i=0;i<pw*ph;i++)
                 {debug<<i<<"\t(";tempc[0]=tempc[1]=tempc[2]=0;
                 for(j=0;j<3;j++)
                                 {strplace[1]=tempfile.find(" ",strplace[0]);
                                 tempfile.copy(temp,strplace[1]-strplace[0],strplace[0]); tempc[j]=atoi(temp);debug<<tempc[j]<<"@"<<strplace[0]<<","; cleartemp(tempb); strplace[0]=strplace[1]+1;}
                 debug<<")\t";
                 if(tempc[0]==tempc[1]==tempc[2]==0){debug<<0;a[i]=426;}
                 else if(tempc[0]>=tempc[1] && tempc[0]>=tempc[2])
                                       {debug<<1;tempc[1]=tempc[1]*(res/tempc[0]); tempc[2]=tempc[2]*(res/tempc[0]); tempc[0]=res; a[i]=((tempc[0]*428)+(tempc[1]*566))/(tempc[1]+tempc[2]); break;}
                 else if(tempc[1]>=tempc[2] && tempc[1]>=tempc[0])
                                       {debug<<2;tempc[0]=tempc[0]*(res/tempc[1]); tempc[2]=tempc[2]*(res/tempc[1]); tempc[1]=res; a[i]=((tempc[0]*428)+(tempc[1]*566)+(tempc[2]*638))/(tempc[0]+tempc[1]+tempc[2]); break;}
                 else if(tempc[2]>=tempc[0] && tempc[2]>=tempc[1])
                                       {debug<<3;tempc[0]=tempc[0]*(res/tempc[2]); tempc[1]=tempc[1]*(res/tempc[2]); tempc[2]=res; a[i]=((tempc[0]*714)+(tempc[1]*566)+(tempc[2]*638))/(tempc[0]+tempc[1]+tempc[2]); break;}
                 debug<<"\t"<<a[i]<<"\n";}
        debug.close();}             
    
    void pixrgb(double d)
         {tempc[0]=tempc[1]=tempc[2]=0;
         if(d<=428)tempc[0]=tempc[1]=tempc[2]=0;
         else if(d>428 && d<=566)
                       {tempc[0]=(int)((566-d)*res/138); tempc[1]=(int)((d-428)*res/138); tempc[2]=0;}
         else if(d>566 && d<=638)
                       {tempc[0]=0; tempc[1]=(int)((638-d)*res/72); tempc[2]=(int)((d-566)*res/72);}
         else {tempc[0]=(int)((d-638)*res/152); tempc[1]=0; tempc[2]=(int)((714-d)*res/76);}}
    
    int main()
        {int x,i,j,framequant;
        cout<<"Number of Frames:  ";
        cin>>framequant;
    
        getinitial();
        for(x=0;x<framequant;x++)
            {char frame[17]={"PLCS1-1F"};
            itoa(x,temp,10);
            strcat(frame,temp);
            cleartemp(temp);
            strcat(frame,".ppm");
            ofstream ofile (frame, ios::trunc | ios::ate);
            ofile<<"P3# "<<pw<<" "<<ph<<" "<<res<<" \n";
            for(i=0;i<pw*ph;i++)
                                {processpix(i);
                                pixrgb(a[i]);
                                for(j=0;j<3;j++)
                                                {itoa((int)tempc[j],temp,10);
                                                ofile<<temp<<" ";}
                                }
            ofile.close();
            putbina();}
        return 1;}
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)