Thread: Problem with two functions in single cout

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    36

    Problem with two functions in single cout

    Code:
    #include<iostream>
    #include<conio.h>
    using namespace std;
    typedef unsigned long ulong;
    
    
    long eeuclid(long m, long b, long *inverse){        /// eeuclid( modulus, num whose inv is to be found, variable to put inverse )
        /// Algorithm used from Stallings book
        long A1 = 1, A2 = 0, A3 = m,
             B1 = 0, B2 = 1, B3 = b,
             T1, T2, T3, Q;
    
             cout<<endl<<"eeuclid() started"<<endl;
    
            while(1){
                cout<<"in loop\n";
                if(B3 == 0){
                    *inverse = 0;
                    return A3;      // A3 = gcd(m,b)
                }
    
                if(B3 == 1){
                    *inverse = B2; // B2 = b^-1 mod m
                    return B3;      // A3 = gcd(m,b)
                }
    
                Q = A3/B3;
    
                T1 = A1 - Q*B1;
                T2 = A2 - Q*B2;
                T3 = A3 - Q*B3;
    
                A1 = B1; A2 = B2; A3 = B3;
                B1 = T1; B2 = T2; B3 = T3;
     /*
                cout<<"here"<<endl;
                if( getch() =='q' ) exit(0);
    //  */
           }
        cout<<endl<<"ending eeuclid() "<<endl;
    }
    
    int main(){
        long a, b, c, d=0, e, inverse = 0;
        int ch;
            cout<<"Preparing extended Euclid ---> "<<endl;
            cout<<"m >> ";
            cin >> a;
            cout<<"b >> ";
            cin >> b;
            cout<<"gcd("<<a<<","<<b<<") = "<<eeuclid(a, b, &inverse);
            cout<<" and inverse  = "<<inverse<<endl;
            cout<<endl<<"Press q to quit ... "<<endl;
        return 0;
    }
    In the above code, in the main() function, if
    Code:
    cout<<"gcd("<<a<<","<<b<<") = "<<eeuclid(a, b, &inverse);
            cout<<" and inverse  = "<<inverse<<endl;
    is replaced by
    Code:
    cout<<"gcd("<<a<<","<<b<<") = "<<eeuclid(a, b, &inverse)<<" and inverse  = "<<inverse<<endl;
    the value of inverse prints out as 0, even though single stepping through the code and examining the value in memory shows that the variable inverse has the correct value
    Why is that??

    FYC: also posted here
    Problem with two functions in single cout - Dev Shed

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mahaju
    the value of inverse prints out as 0, even though single stepping through the code and examining the value in memory shows that the variable inverse has the correct value
    Why is that??
    The order of evaluation of a function's arguments, and also of expressions in general, is implementation defined. This allows the compiler more leeway for optimisation, but it also means that if you are not careful when you want something to be sequenced before something else, you could get unexpected results.

    In this case, your unexpected result was that the value of inverse was used before the change to inverse from calling eeuclid. Using two statements instead ensures that the call to eeuclid is sequenced before the use of the updated value of inverse.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    36
    What I actually don't understand is, this is code that I had completed and tested months ago
    Yesterday when I as running it again it was working fine for the first few runs
    Since this is not a very long code I usually do a rebuild and rerun of this projct every time I wasnt to run it again
    For the first few rebuilds and reruns it worked as expected
    Then suddenly this happened
    I didn't expect something like that was possible

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you did not rebuild, the behaviour should be consistent between runs. Once you compile, the compiler may determine that the evaluation should take place differently, hence it is possible that you end up with a different result.

    By the way, SimonB2's answer in DevShed is not correct: right to left evaluation of function arguments is not guaranteed. It may be easier if you examined a statement like this:
    Code:
    a << b() << c();
    rewriting it to:
    Code:
    operator<<(operator<<(a, b()), c());
    So, the compiler can make it such that either operator<<(a, b()) or c() is evaluated first. If the former is evaluated first, then b() will be called before c(), otherwise c() will be called before b().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    36
    I did do a rebuild
    I do a rebuild everytime I want to run it
    My problem is that, it was working all right the first 5 or so times
    Then suddenly this problem appeared
    It was solved by breaking the cout into two lines
    but I still don't understand why this happened in the first place

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I am telling you that what happened is that the compiler chose a different order of evaluation. That is all. Breaking up the single statement into two was the correct approach to avoid this problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cout redirection problem
    By MrLucky in forum C++ Programming
    Replies: 6
    Last Post: 06-06-2007, 11:11 AM
  2. cout problem with windows API
    By xximranxx in forum Windows Programming
    Replies: 2
    Last Post: 05-04-2007, 12:37 AM
  3. Problem with cout
    By kristentx in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2006, 12:37 PM
  4. Writing to single file in multiple functions
    By alvision in forum C Programming
    Replies: 12
    Last Post: 08-22-2004, 08:15 PM
  5. Problem with cout
    By macktrevor in forum C++ Programming
    Replies: 4
    Last Post: 11-14-2002, 12:41 AM