setprecision

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jthep
    New Member
    • Oct 2007
    • 34

    setprecision

    Hi, I'm converting codes that I worked with in C to C++ and having a problem with setprecision. At first the code worked, then I made some changes. When the changes didn't work I wrote it back to how it was before and now it's not working. I keep getting the error message where setprecision is undefined. Can anyone help plz.


    Code:
    #include <iostream>
    using namespace std;
    
    float convertFtoC(int temperature);
    int roundUpNextMultiple(int temperature);
    int getInput();
    
    
    float convertFtoC(int temperature)
    {
        float celcius (0.0);
        celcius = (temperature - 32.0) * 5.0/9.0;
        return celcius;
    }
    
    int foundUpNextMultiple(int temperature)
    {
        int addToNextMultiple = temperature % 5;
    
        if(addToNextMultiple != 0)
        {
            addToNextMultiple = 5 - addToNextMultiple;
            temperature = temperature + addToNextMultiple;
        }
        return temperature;
    }
    
    int getInput()
    {
        int input;
    
        cout << "Please enter a temperature greater than 0:\t";
        cin >> input;
    
        while(input <= 0)
        {
            cout << "Error, please re-enter:  ";
            cin >> input;
        }
    
        return input;
    
    }
    
    int main()
    {
        int input = getInput();
        int maxTemp = foundUpNextMultiple(input);
        int start = 0;
        float celcius(0.0); 
    
        cout << "Temperature in F" << "\t" << "Temperature in C\n";
        cout.setf(ios::fixed | ios::showpoint);
        cout.setprecision(2);
    
        while (start <= maxTemp)
        {
            celcius = convertFtoC(start);
            cout << "\t" << start << "\t\t\t" << celcius << "\n";
            start += 5;
        }   
    }
    Error message occurs at line 54.

    Regards,
    Jthep
  • jthep
    New Member
    • Oct 2007
    • 34

    #2
    Okay, there we go, solved it. I used setprecision vs just cout.precision. Don't know why I used that. I noticed I spelt one of my function calls incorrectly. Wonder why there wasn't an error when compiling.

    Regards,
    Jthep

    Comment

    Working...