Thread: Can someone help me with setprecision?

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41

    Can someone help me with setprecision?

    I am trying to use setprecision but I keep getting the error
    Code:
    RasMolMaker.cpp:124: error: ‘precision’ was not declared in this scope
    I don't understand why I am getting this error when I include the <iomanip> header. Can someone tell me where I am going wrong? Thanks

    BTW, the problem line is at the bottom of the main section at the end of the while loop.

    My header file is:
    Code:
    #include <fstream>
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <ios>
    #include <cstdlib>
    
    
    void writeBash(char* file);
    void writeCleanup();
    void Clearspace();
    void clearSpace();
    and my code is:
    Code:
    #include "RasMolMaker.h"
    
    int main(const int argc, const char** argv)
    {
    	char ch,holder[100];
    	const char* c_str();
    	std::string str,suffix,RasFile;
    	//std::string filename;
    	suffix = ".pdb";
    	
    	/*
    	// Get filename and open stream
    	std::cout << "Enter filename:\n";
    	std::cin >> filename;
    	std::cout << "Opening " << filename << "...\n";	
    	std::ifstream infile;
    	//infile.open(filename.c_str());
    	*/
    	
    	// Open config file for input
    	std::ifstream infile, infile2;
    	infile.open("config");
    
    	// Write formatting bash script
    	char* filename = "config";
    	writeBash(filename);
    
    	// Open scratch file for input
    	infile2.open(".17834039BOSUQWU239MBKA89");
    
    	/*
    	// Create RasMol file and open stream
    	RasFile = filename + suffix;
    	std::cout << "Output will be to file " << RasFile << "\n";
    	std::ofstream outfile;
    	//outfile.open(RasFile.c_str());
    	*/
    	
    	// Open config.pdb for output
    	std::ofstream outfile;
    	outfile.open("config.pdb");
    	
    	// Check files opened correctly
    	if (infile.fail()) 
    	{
    		std::cout << "Input file failed to open correctly\n";
    	}
    	if (outfile.fail()) 
    	{
    		std::cout << "Output file failed to open correctly\n";
    	}
    	if (infile2.fail())
    	{
    		std::cout << "Scratch file failed to open correctly\n";
    	}
    
    	// Find the size of the file
    	infile2.seekg(0, std::ios::end);
    	long len = infile2.tellg();
    	infile2.seekg(0, std::ios::beg);
    
    	double x,y,z;
    	int index = 0;
    	while(infile2.tellg() <= (len-2) && infile2.tellg() >=0)
    	{
    		
    		if(index > 0 && index % 3 == 0)
    		{
    			outfile << "\n";
    		};
    		if (index % 3 == 0 )
    		{
    			if (index/3 < 10)
    			{
    				if (index/3 % 20 < 10) {
    					outfile << "ATOM      " << index/3 << "  N" << "                 ";
    				}
    				else {
    					outfile << "ATOM      " << index/3 << "  O" << "                 ";
    				}				
    			}
    			if (index/3 >= 10 && index/3 < 100)
    			{
    				if (index/3 % 20 < 10) {
    					outfile << "ATOM     " << index/3 << "  N" << "                 ";
    				}
    				else {
    					outfile << "ATOM     " << index/3 << "  O" << "                 ";
    				}
    
    			}
    			if (index/3 >= 100 && index/3 < 1000)
    			{
    				if (index/3 % 20 < 10) {
    					outfile << "ATOM    " << index/3 << "  N" << "                 ";
    				}
    				else {
    					outfile << "ATOM    " << index/3 << "  O" << "                 ";
    				}
    
    			}
    			if (index/3 >= 1000 && index/3 < 10000)
    			{
    				if (index/3 % 20 < 10) {
    					outfile << "ATOM   " << index/3 << "  C" << "                 ";
    				}
    				else {
    					outfile << "ATOM   " << index/3 << "  C" << "                 ";
    				}
    
    			}
    			
    		}
    		infile2 >> holder;
    		outfile << setprecision(4) << holder << "   ";
    		index++;
    	}
    	// Check file position before closing
    	std::cout << "File length: " << len << "\n";
    	std::cout << "File position before closing: " << infile.tellg() << "\n";
    	infile.close();
    	outfile.close();
    	writeCleanup();
    	clearSpace();
    
    	return 0;
    }
    
    void writeBash(char* file)
    {
    	// Open output file stream
    	std::ofstream out;
    	out.open("cleaner.sh");
    	// Write bash script
    	out << "grep -v 'molecule' " << file << "| grep -v 'BOUNDARY' | grep -v 'MOLECULES' | grep -v 'species' | grep -v 'nMolecule' | grep -v 'orthorhombi' | grep -v 'cubic' | grep -v 'tetragonal' > .17834039BOSUQWU239MBKA89";
    	// Make script executable
    	std::system("chmod +x cleaner.sh");
    	// Close filestream
    	out.close();
    	// Execute script
    	std::system("./cleaner.sh");
    }
    
    void writeCleanup()
    {
    	// Open output filestream
    	std::ofstream out;
    	out.open("wipe.sh");
    	// Write bash script
    	out << "rm .17834039BOSUQWU239MBKA89";
    	// Make script executable
    	std::system("chmod +x wipe.sh");
    	// Close filestream
    	out.close();
    	// Execute script
    	std::system("./wipe.sh");
    }
    
    void clearSpace()
    {
    	std::system("rm wipe.sh cleaner.sh");
    }
    Last edited by waterborne; 06-30-2010 at 01:07 PM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Try std::setprecision.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41
    I could have sworn I tried that earlier and it didn't work but sure enough that fixed the error. However, although it is running, it doesn't look like it is working correctly. My output is;
    Code:
    ATOM      0  N                 6.1515327344859010   -4.7598548633590330   -3.8371135483309899   
    ATOM      1  N                 6.9312075665350044   -5.3860358389776426   -3.8392447551554660   
    ATOM      2  N                 7.7381034623045375   -4.7985204546993785   -3.7780509104760647
    I was expecting:
    Code:
    ATOM      0  N                 6.151   -4.759   -3.837   
    ATOM      1  N                 6.931   -5.386   -3.839  
    ATOM      2  N                 7.738   -4.798   -3.778

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Try also
    Code:
    outfile.precision(4);
    though it should make a difference.

    As a minor note, you might want to use std::fixed as well. No idea why it doesn't work for you

  5. #5
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41
    I tried that earlier (outfile.precision(4)) and it didn't work. I'll give fixed a shot.

    EDIT:That didn't work either. I had this thought though. I read the data into holder which is a character array. I am guessing the setprecision isn't working like I want because the computer doesn't think it is dealing with numbers. Does this make any sense?
    Last edited by waterborne; 06-30-2010 at 02:05 PM.

  6. #6
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41
    The problem was using setprecision on a char array. I changed the trouble line to:
    Code:
    outfile  << std::setprecision(4) << atof(holder) << "   ";
    and that fixed the problem. Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. setprecision()
    By hallo007 in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2006, 09:38 AM
  2. setprecision??
    By rachael033 in forum C++ Programming
    Replies: 5
    Last Post: 03-22-2006, 02:33 AM
  3. setprecision() - can I count on it rounding or not?
    By major_small in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2005, 02:26 PM
  4. setprecision()
    By OnionKnight in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2005, 09:08 PM
  5. setprecision() Help
    By frgmstr in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2002, 02:24 PM