Scientific output with captial E rather than lowecase e?

Dec 17, 2011 at 11:30pm
Hello,
I am trying to make a script to open many files that differ only at the end. The files are named the same except for the end where it has a value in scientific notation with 5 digits after the decimal. However, the filename uses a capital E whereas c++ likes to use a lowercase e. I have this

double out_ergs[80] = {0};
...
for (int i=0; i<80; i++) {
stringstream filename;
filename.precision(5);
filename.setf(ios::scientific,ios::floatfield);
filename << "./m/nBackground_void_" << out_erg[i] << ".m";

...

where the file names are

nBackground_void_1.00000E-10.m
nBackground_void_9.00000E-9.m
...
and the values at the end are stored in the array out_ergs. right now, the stringstream "filename" is taking the values

nBackground_void_1.00000e-10.m
nBackground_void_9.00000e-9.m

and those filename are incorrect and can't be found because of the "e" rather than "E". Could anyone help me out on this? thanks
Dec 18, 2011 at 1:05am
Instead of printing the values from 'out_ergs[]' straight into your string stream you could create a second stringstream, print the values from 'out_ergs[]' into this second stream, convert it to a string, iterate through it replacing any lower case e's with upper case e's and then print this to your original string stream.

Eg:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for (int i=0; i<80; i++) {
	stringstream filename;
	filename.precision(5);
	filename.setf(ios::scientific,ios::floatfield); 
	
	stringstream number;
	number << out_erg[i];
	std::string numberString = number.str();
	for(int j = 0; j < numberString.length(); j++)
	{
		if(numberString.at(j) == 'e')
			numberString.at(j) = 'E';
	}
	
	filename << "./m/nBackground_void_" << numberString << ".m";
}


*Edit: Also you might be interested to know that the printf style functions have the ability to control the capitalization of the 'e' in exponents. See here for more information: http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/
Last edited on Dec 18, 2011 at 4:46pm
Dec 18, 2011 at 7:46pm
thanks, I was hoping to not have to do the search and replace method. But I guess that works. Thanks alot!
Dec 18, 2011 at 11:07pm
You can use the uppercase manipulator
http://www.cplusplus.com/reference/iostream/manipulators/uppercase/

But remember to turn it back off with nouppercase.

1
2
3
4
5
6
for (int i=0; i<80; i++) {
    stringstream filename;
    filename << setprecision(5) << scientific 
        << "./m/nBackground_void_"
        << uppercase << out_erg[i] << nouppercase << ".m";
    ...


Andy

PS If possible, I would code this with the stringstream outside of the loop and then reset it at the start of the loop, to avoid the repeated construction/destruction.

1
2
3
4
5
6
7
ostringstream filename; // using o- version as just for o/p
filename << setprecision(5) << scientific; 
for (int i=0; i<80; i++) {
    filename.str(""); // clear
    filename << "./m/nBackground_void_"
        << uppercase << out_erg[i] << nouppercase << ".m";
    ...
Last edited on Dec 18, 2011 at 11:17pm
Dec 19, 2011 at 5:17am
perfect, thats exactly what I needed! thanks
Dec 19, 2011 at 5:59am
Hi ,
I am getting runtime error when i try this .

1
2
3
4
5
6
7
8
9
10
11
  string strfilename = "BackGround_void_1956.txt";
        int length = 0 ; 
        size_t postion = 0 ;
        string strreverse ;
        strreverse = strfilename ; 
        reverse( strreverse.begin() , strreverse.end() ) ; 
        postion = strreverse.find('a') ; 
        length = strfilename.length()   ;       
        length = length -postion ; 
        strfilename.at( length - postion )  = 'A' ; 
        cout<<"\n The postion = "<<strfilename  ; 


i am not able to understand as why i am getting the errror . Please advice
thanks in advance .
Topic archived. No new replies allowed.