Increment + validation ?

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.
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
29
30
31
32
33
34
35
36
37
  #include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int inFile ();

int main()
{
    inFile();
    
    return 0;
}

int inFile()
{
  int temps;
  ifstream inFile;
  //ofstream outputFile;
  //outputFile.open("temps2.txt");
  inFile.open("temps.txt");
  if (!inFile)
{
    cout << "\nError opening file\n";
    return -1;
}
  while (inFile >> temps)
{
    cout << temps << endl;
  if (temps >= 2 && temps <= 4)
  {
     cout << '\n' << setw(temps+1) << setfill('*') << '\n';
  }
}


}

I know that cout in the IF is wrong since it just outputs stars = to number.
Last edited on
What are you supposed to do for negative values?
a -20 will still receive the same amount of stars as 20.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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 )
    {
        const int 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' ;
    }
}
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int inFile ();

int main()
{
    inFile();
    
    return 0;
}

int inFile()
{
  int temp, counter;
  ifstream inFile;
  //ofstream outputFile;
  //outputFile.open("temps2.txt");
  inFile.open("temps.txt");
  if (!inFile)
{
    cout << "\nError opening file\n";
    return -1;
}
  while (inFile >> temp)
{
    cout << temp << endl;
    counter = 0;
  while((temp % 3 )> 3)
{
  for (int i=0; i <temp; i++)
{
    cout << "*";
    cout << "\n";

}
}
}


}


Why do no stars display for my code?
> Why do no stars display for my code?

This is dead code: while( ( temp%3 ) > 3 ) { /* ... */ }
The condition in the while statement would never evaluate to true
How do I change my code to have it print the correct amount of stars and all on one line?
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int inFile ();

int main()
{
    inFile();
    
    return 0;
}

int inFile()
{
  int temp, counter;
  ifstream inFile;
  //ofstream outputFile;
  //outputFile.open("temps2.txt");
  inFile.open("temps.txt");
  if (!inFile)
{
    cout << "\nError opening file\n";
    return -1;
}
  while (inFile >> temp)
{
    cout << temp << endl;
    counter = 0;
  while( temp < 0 ) temp = -temp; 
{
  for (int i=0; i <temp; i++)
{
    cout << "temp";
    cout << "\n";

}
}
}


}

because currently it just prints stars = to read value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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.
        const int value = temp < 0 ? -temp : temp ;
        const int 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
    }
}
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cassert>
using namespace std;

int main()
{
   const int maxneg = 20;
   int temperature;
   int numneg, numpos;

   cout << setw( 5 ) << "T" << string( maxneg, ' ' ) << "0\n";

   ifstream in( "temps.txt" );         assert( in );
   while ( in >> temperature )
   {
      numneg = numpos = 0;
      if ( temperature < 0 ) numneg = (-temperature + 1 ) / 3;
      if ( temperature > 0 ) numpos = ( temperature + 1 ) / 3;
      cout << setw( 5 ) << temperature << string( maxneg - numneg , ' ' ) << string( numneg, '*' ) << '|' << string( numpos, '*' ) << '\n';
   }
   in.close();
}


    T                    0
  -20             *******|
  -50   *****************|
    0                    |
    1                    |
    2                    |*
    3                    |*
    4                    |*
    5                    |**
  240                    |********************************************************************************
   10                    |***
   20                    |*******
   30                    |**********

Topic archived. No new replies allowed.