A problem with sprintf

Hi, I'm a newbie in all this C++ stuff and I couldn't handle this problem.
The objective of the source code is to print this message:

1
2
3
4
5
6
7
8
Col.0: ( 0  5.254560 ) ( 1  5.354560) ( 2  5.654560 ) ( 4  5.754560 ) ( 6  5.254580 )
Col.1: ( 1  5.254000 ) ( 4  6.000000)
Col.2: ( 1  5.254100 ) ( 6  5.300000)
Col.3:
Col.4: ( 2  5.354570 )
Col.5: ( 4  5.354570 )
Col.6: ( 0  5.254570 )
Col.7: ( 0  5.254580 ) ( 2  5.255560 ) ( 6  5.254590 )


For that I have this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void sparse_matrix_t::mostrarMatrizDispersa (void)
{
   char aux[120];
for (int i=0; i<get_n(); i++){
	cout << "Col." << i << ": ";
	for (int j=matbeg_[i]; j<(matbeg_[i]+matcnt_[i]); j++){
		if(matval_[j]!=0.0){
		cout << "( " << matind_[j];
		sprintf(aux,"  %10.6lf ) ",matval_[j]); /*THIS ONE*/
	    }
    }
    cout << endl;	
}
}


The values of every vector are right (matcnt, matbeg...), but for a reason the sprintf doesn't work, and this is what it prints:

1
2
3
4
5
6
7
8
9
10

Col.0: ( 0( 1( 2( 4( 6
Col.1: ( 1( 4
Col.2: ( 1( 6
Col.3: 
Col.4: ( 2
Col.5: ( 4
Col.6: ( 0
Col.7: ( 0( 2( 6
sprintf doesn't print anything it just formats a string
http://www.cplusplus.com/reference/cstdio/sprintf/


maybe your thinking of printf
http://www.cplusplus.com/reference/cstdio/printf/?kw=printf
Last edited on
Thanks Yanson, i didn't became aware of it, this is the final code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void  sparse_matrix_t::mostrarMatrizDispersa(void)
{
   char aux[120];
for (int i=0; i<get_n(); i++){
	cout << "Col." << i << ": ";
	for (int j=matbeg_[i]; j<(matbeg_[i]+matcnt_[i]); j++){
		if(matval_[j]!=0.0){
		sprintf(aux,"%10.6lf",matval_[j]);
		cout << "( " << matind_[j] << "  " << aux << " ) ";
	    }
    }
    cout << endl;	
}
}


(I'm using sprintf only for set the precision as i like)
Topic archived. No new replies allowed.