The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
cout 1.0.1 (Default branch) iBot Software Releases - RSS News 0 06-11-2008 09:10 AM
Print to ps2pdf print queue Sean_69 SUN Solaris 2 10-22-2007 08:00 AM
Print to a ps2pdf print queue. Sean_69 UNIX for Dummies Questions & Answers 1 10-22-2007 07:58 AM
Print Problem in UNIX. Need to know the option to specify the print paper size ukarthik HP-UX 1 06-07-2007 06:35 AM
How do I get awk to print a " in it's print part? LordJezo Shell Programming and Scripting 2 06-27-2006 06:16 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-11-2008
Registered User
 

Join Date: May 2005
Posts: 19
cout doesn't print everything

Hi all,

I implemented a C++ program and successfully compiled and ran on my laptop. However when I copy my code to another machine (school's sun machine), it didn't run properly. I can compile and run, but cout does not print everything. I used cout in a loop where it iterates no more than 20 times and it only prints the first iteration.

I used cout.flush() after each cout line, it also didn't work.

Below is the code snippet:

Code:
for(iter = dist.begin(); iter != dist.end(); ++iter)//{
                cout << "Distance: " << iter->first << " , Value: " << iter->second << endl;
                cout.flush();
}
Could you please tell me what the problem is?

Thanks

OS: Solaris
Compiler: g++
Reply With Quote
Forum Sponsor
  #2  
Old 08-11-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,061
What is the purpose of the "//" before the "{" on the first line of your code?
Reply With Quote
  #3  
Old 08-11-2008
Registered User
 

Join Date: May 2005
Posts: 19
Below is my real code
Code:
        
for(iter = dist.begin(); iter != dist.end(); ++iter)//{
       cout << "Distance: " << iter->first << " , Value: " << iter->second << endl;
       //cout.flush();
//}
I removed the // from my code before post it here and apparently forgot to remove one of them. Simply, ignore the '//'..
Reply With Quote
  #4  
Old 08-12-2008
Registered User
 

Join Date: Jun 2008
Location: Scotland
Posts: 121
Quote:
it iterates no more than 20 times and it only prints the first iteration.
How do you know how many times it iterates?
I would run the code in a debugger to see how many items are in the container dist
(or you could add code to print the number).
Reply With Quote
  #5  
Old 08-12-2008
Registered User
 

Join Date: May 2005
Posts: 19
Wow, you're definitely right. The list has 1 element. Now my problem is changed, below is the code of the whole function:

Code:
//actorList is a vector which holds pointers of actor classes
//in an actor class there is an integer (dist) which represents the distance of
// current actor to another one. this function is supposed to generate new
// distances in map and if distance already exists in the map, dist map
// increases the value part of key. for instance, all actors whose distance 
//is 5 count on map dist[5][n], n shows the total number of actors. if there
// is no key=5 in the map, it simply creates one and adds it to the map (using make_pair(_,_)). 


void printDist(){
        typedef map<int, int> mapType;
        mapType dist;
        dist.insert (make_pair(0 ,0));
                                 
        mapType::iterator iter = dist.begin();
                
        cout << "Size of actor list: " << (*actorList).size() << endl;

        timer1.restart();
        for(int i=0;i<(*actorList).size();i++){
                iter = dist.find((*actorList)[i]->dist);
                
                if(iter == dist.end()){
                        if(iter->first == (*actorList)[i]->dist){
                                dist.insert (make_pair((*actorList)[i]->dist ,1)); //add new distance to map
                                cout << "Element was inserted. Size of dist:" << dist.size() << endl;
                        }
                }else{  
                        iter->second += 1; //if distance already exists in map, simply increase the value by 1.
                }
        }
        
        timer1.stop();
        timer1.check("Actor list travel: ");
                     
        for(iter = dist.begin(); iter != dist.end(); ++iter)
                cout << "Distance: " << iter->first << " , Value: " << iter->second << " Out of: " << dist.size() << endl;
}
It is weird, I can't understand why it works on Ubuntu but not on Solaris....

thanks.

Last edited by SaTYR; 08-12-2008 at 01:59 PM.
Reply With Quote
  #6  
Old 08-14-2008
Registered User
 

Join Date: May 2005
Posts: 19
I was able to handle the situation. I changed my implementation. Still I don't know why this code behaves different on Solaris and Ubuntu though...

Thanks
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:32 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0