So in my current assignment I need to read a file with temperatures and then depending on the temp, cout X amount of stars.
" you should have each star represent a range of 3
degrees."
-20
-50
0
1
2
3
4
5
240
10
20
30
those are my values. Another question is shouldn't 0/1/2 have 1 star?
But then in the assignment my professor said
"In addition, note that 0 and 1 have no star while 2, 3 and 4 all have one
star while 5 have 2 stars. It is not shown in the output but 6 and 7 should
also have 2 stars.
"
So im trying to know how I am to set an IF loop to check the number and print 1++ star for every 3 degrees of change.
#include <iostream>
#include <fstream>
#include <iomanip>
int num_stars( int value )
{
if( value < 0 ) value = -value ;
return (value+1) / 3 ; // (1+1)/3 == 0, (2+1)/3 == 1, (4+1)/3 == 1, (5+1)/3 == 2 etc
}
int main()
{
std::ifstream file( "temps.txt" ) ;
std::cout.fill( '*' ) ; // need to do this only once
int temp ;
while( file >> temp )
{
constint n_stars = num_stars(temp) ;
// print a new line after n_stars asterisks (+1 is for the new line)
std::cout << std::setw( n_stars + 1 ) << '\n' ;
}
}
#include <iostream>
#include <fstream>
#include <iomanip>
int main()
{
std::ifstream file( "temps.txt" ) ;
std::cout.fill( '*' ) ; // need to do this only once
int temp ;
while( file >> temp )
{
// a -20 will still receive the same amount of stars as 20.
constint value = temp < 0 ? -temp : temp ;
constint n_stars = (value+1) / 3 ;
// print a space after printing n_stars asterisks (+1 is for the space)
std::cout << std::setw( n_stars + 1 ) << ' '
<< temp << '\n' ; // and then print temp and a new line
}
}