cout doesn't print everything


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers cout doesn't print everything
# 1  
Old 08-11-2008
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++
# 2  
Old 08-12-2008
What is the purpose of the "//" before the "{" on the first line of your code?
# 3  
Old 08-12-2008
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 '//'..
# 4  
Old 08-12-2008
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).
# 5  
Old 08-12-2008
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 05:59 PM..
# 6  
Old 08-15-2008
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

DBMS_OUTPUT.PUT_LINE doesn't print values in shell script

Hello, I'm trying to print the value of my cursor in the dbms_output.put_line in shell script, but it only shows "PL/SQL procedure successfully completed." nothing else. I have set serveroutput on, Below is my script : Any advise would be really helpful. sqlplus -s $ORACLE_LOGON <<EOF >>... (2 Replies)
Discussion started by: mail.chiranjit
2 Replies

2. Programming

Concatenate integer and space/newline for cout in C++

I'm trying to print out integers and space/newline for a nicer output, for example, every 20 integers in a row with ternary operator. In C I could do it with:printf("%d%s",tmp_int, ((j+1)%20) ? "\t":"\n"); but could not figure out the equivalent in C++: cout << ((j+1)%20)?... (4 Replies)
Discussion started by: yifangt
4 Replies

3. Programming

Missing cout

Heyas Me trying some C.. cout in specific, thats what i remembered: #include <stdio.h> // -- Just the above or with all the below ones, no change #include <stdio_ext.h> #include <stdlib.h> #include <wchar.h> //#include <iostream> // I assume its the same anyway? //#include <iostream.h>... (2 Replies)
Discussion started by: sea
2 Replies

4. Shell Programming and Scripting

Script doesn't print error.

Hi Gurus, I have below sample script. I expect it print error when running script without input parameter. but the it doesn't. would you please help me about this issue. thanks in advance. /script$cat test.ksh #!/bin/ksh while getopts :f: arg do case $arg in ... (4 Replies)
Discussion started by: ken6503
4 Replies

5. Shell Programming and Scripting

If first character doesn't equal '#' and same line contains 'temp', print everything else

(2 Replies)
Discussion started by: snoman1
2 Replies

6. Programming

std::cout and gfortran print*, don't output to the screen

I am not sure where to post this other than here. I am trying to figure out why an app gives different output when compiled under Ubuntu 10.10 and CentOS 5.5. I am pretty sure that the issue is that the Cent version has gcc 4.1 installed, while Ubuntu has gcc 4.4. I am trying to print from some... (20 Replies)
Discussion started by: LMHmedchem
20 Replies

7. Programming

"cout = outFile" is not compiled

Hello, Compilation of the line "cout = outFile" throws error "Error: std::ios_base::operator=(const std::ios_base&) is not accessible from std::ios ::operator=(const std::ios &)." outFile is declared as "static ofstream". Thanks, Shafi (3 Replies)
Discussion started by: shafi2all
3 Replies

8. Shell Programming and Scripting

print first few lines, then apply regex on a specific column to print results.

abc.dat tty cpu tin tout us sy wt id 0 0 7 3 19 71 extended device statistics r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device 0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0 0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx I want to skip first 5 line... (4 Replies)
Discussion started by: kchinnam
4 Replies

9. Programming

library for cout question

Hi I am running Ubuntu 9.10 and I use QtCreator for my C++ Programms. I knwo that in wondows OS cout is located in iostream.h library. It seems that it isn;t the same library in Qtcrator... :( Anyway, iostream.h doesn't exist and iostream (without the .h) exists but doesn't seem to include the... (4 Replies)
Discussion started by: hakermania
4 Replies
Login or Register to Ask a Question