How do I get the rows and colums to show specific values (not part of calculation)

Hi guys! New to the forums here. I am working on a C++ project and have been stuck on two parts:

1. Can someone help me understand why my setprecision is not working? Regardless of what I set the precision to the code always displays the same results.
2. Formatting rows and columns... all the values are correct (with the exception of the setprecision part) but before I get into displaying the calculations which are currently shown I want to display the temperature vertically (before each row) from -5 to 50 in increments of 5 and the wind speed horizontally (top of each column) from 5 to 45 in increments of 5.

Any ideas?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//This program displays windchill (temperature) related
//to the actual temperature and the windspeed

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{

	int wind_speed;
	int temperature;
	int wind_chill;

	cout << "Temp  :  ------------------------ Wind Speed ------------------------ :" << endl;
	cout << endl;

	for (int temperature = -5; temperature <= 50; temperature += 5) {
		for (int wind_speed = 5; wind_speed <= 45; wind_speed += 5) {
			wind_chill = 35.74 + 0.6215 * temperature - 35.75 * pow(wind_speed, 0.16) + 0.4275 * temperature * pow(wind_speed, 0.16);
			cout << setprecision(3) << wind_chill << "\t";
		}
		cout << endl;
	}

	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cmath>
#include <iomanip>

int main()
{
    // print the header lines
    std::cout << "Temp  |  ------------------------ Wind Speed --------------------------------------------------------\n      |" ;
    for( int wind_speed = 5; wind_speed <= 45; wind_speed += 5 )
    std::cout << std::setw(10) << wind_speed ;
    std::cout << "\n------|----------------------------------------------------------------------------------------------\n" ;

    std::cout << std::fixed << std::setprecision(3) ;

    for( int temperature = -5; temperature <= 50; temperature += 5 ) { // for each temperature

        std::cout << std::setw(5) << temperature << " |" ; // print the temperature first

        for( int wind_speed = 5; wind_speed <= 45; wind_speed += 5 ) { // print the wind chills

            const double wind_chill = 35.74 + 0.6215 * temperature - 35.75 * pow(wind_speed, 0.16) + 0.4275 * temperature * pow(wind_speed, 0.16);
            std::cout << std::setw(10) << wind_chill ;
        }
        std::cout << '\n' ; // go to the next line (for the next temperature)
    }
}

http://coliru.stacked-crooked.com/a/913d0b30a3eed419
Hello abeabukwaik,

Welcome to the forum.

First I would suggest this: std::cout << std::fixed << std::showpoint << std::setprecision(2);. This line has to be done only once, so I would put it above the for loop. The "setprecision()" will stay the same until another "setprecision()" changes this or the program ends and is started over again.

Beyond that it should work even in the for loop.

Give me a few minutes to test your program and I will know more. This will take about ten minutes or so because my old laptop is slow these days.

Hope that helps,

Andy
Awesome. Thank you Handy Andy and JLBorges. This has been very helpful! It looks like the major issue was having the setprecision in the wrong area. And looking at JLBorges code, I see why the initial temperatures and wind speeds were not showing up.

Thank you again!

-Abe
Topic archived. No new replies allowed.