简体   繁体   中英

Why wont my while statement work?

I think i am doing everything correctly.....but obviously i am missing something, my while statement doesn't seem to ever get entered. Please help....

#include <iostream>
using namespace std;


void swap(int *aPtr, int *bPtr)
{
    int holder=0;
    holder=*aPtr;
    *aPtr=*bPtr;
    *bPtr=holder;
}
void instantiator(int *dPtr,int *xPtr,int *yPtr,int a)
{
    *dPtr=a;
    *xPtr=1;
    *yPtr=0;
}
void cruncher(int *xPtr,int *dPtr, int *aPtr,int *yPtr,int *bPtr)
{
    int x1=0,x2=1,y1=1,y2=0,r=0,q=0;
    while(*bPtr>0)
        {
            q=*aPtr/(*bPtr);
            r=*aPtr%*bPtr;
            *xPtr=x2-(q*x1);
            *yPtr=y2-(q*y1);
            *aPtr=*bPtr;
            *bPtr=r;
            x2=x1;
            x1=*xPtr;
            y2=y1;
            y1=*xPtr;
            cout<<q<<" "<<r<<" "<<*xPtr<<" "<<*yPtr<<" "<<*aPtr<<" "
                <<*bPtr<<" "<<x2<<" "<<x1<<" "<<y2<<" "<<y1<<endl;
            }
    cout<<endl;
    *dPtr=*aPtr;
    *xPtr=x2;
    *yPtr=y2;

}
int main() {
    int a=4864,b=3458,d,*aPtr,*bPtr,*dPtr,x,*xPtr,y,*yPtr;
    aPtr=&a;
    bPtr=&b;
    dPtr=&d;
    xPtr=&x;
    yPtr=&y;

    if(b=0)
        instantiator(dPtr,xPtr,yPtr,a);

    if (a<b)
        swap(aPtr,bPtr);

    cruncher(xPtr,dPtr,aPtr,yPtr,bPtr);
    cout<<d<<" "<<x<<" "<<y;


    return 0;
}

if(b=0) . = is an assignment operation. You need to use == for comparison. Correct it to if(b==0)

The test in your while loop is *bPtr > 0 , yet you set b to 0 in main (using if (b=0) ) and then pass a pointer to it as bPtr . Try fixing the if statement in main and see if that changes the behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM