Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Sun 21 Dec 2014, 09:37
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Problem with two functions in single cout
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
Page 1 of 1 Posts_count  
Author Message
mahaju


Joined: 11 Oct 2010
Posts: 493
Location: between the keyboard and the chair

PostPosted: Mon 23 Apr 2012, 01:25    Post_subject:  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 )
    long A1 = 1, A2 = 0, A3 = m,
         B1 = 0, B2 = 1, B3 = b,
         T1, T2, T3, Q;

         cout<<endl<<"eeuclid() started"<<endl;

        while(1){
            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<<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??
Back to top
View user's profile Send_private_message 
technosaurus


Joined: 18 May 2008
Posts: 4424

PostPosted: Mon 23 Apr 2012, 02:14    Post_subject:  

printf still works in C++ ... cout and cin and the <<knife jabs>> are an almost as much an abomination as sed picket fences (and reason 999 I prefer C) - you should post it on stackoverflow though, where there are a few people that bought into the hype and actually think c++ is a good tool (most Puppy coders use C or bash/shell)

I don't write much C++, but it is probably this bit FUNCTION(...)<<... (stops going to cout, and goes to your function)
... seriously printf is much easier to read and debug

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send_private_message 
mahaju


Joined: 11 Oct 2010
Posts: 493
Location: between the keyboard and the chair

PostPosted: Mon 23 Apr 2012, 03:37    Post_subject:  

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 project every time I want 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
Back to top
View user's profile Send_private_message 
Display_posts:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0528s ][ Queries: 11 (0.0058s) ][ GZIP on ]