Can someone help me figure out how to either round up or down down the numbers? The numbers are used with the asterisks in the bar graph. I'm not sure if I should be using fmod or the round function. Any help would be appreciated.

#include<iostream>

#include<iomanip>

#include<cmath>

 

using namespace std;

string printAsterisks(double);//String function declaration

const int MONTHS = 12;



int main()

{



     double avgRain[] = {1.81, 1.04, 0.27, 7.25, 7.79, 2.88, 9.71, 5.04, 3.59, 8.80, 3.67, 2.07};

     double actRain[] = {9.57, 4.42, 4.78, 3.14, 8.72, 3.24, 6.01, 6.31, 9.76, 6.10, 8.37, 6.29};

     string months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

     int month;

     int asterisk_count[MONTHS];//Variable for asterisk count





     

     

     for ( int i = 0; i < 12; i++)

     {    

          double j = avgRain[i]/1.0;

          cout.setf(ios::showpoint);

          cout.setf(ios::fixed);

          cout.precision(1);

          cout << setw(15) << left << months[i] << j << " " << endl;

     }

     







     cout << endl;
     cout << "\n *** RainFall Bargraph *** " << endl << endl;// Bar graph title bar.
     
     for (int i=0;i< MONTHS;i++)//For loop count down for months, average, actual 
     {   

          cout << endl;   
          cout << months[i]<<" " <<"Average: ";//Months output for average  
          cout << printAsterisks(avgRain[i]);//Prints out the asterisks for the totals.
          cout << endl;
          cout << months[i]<<" " <<"Actual:  ";//Months output for average 
          cout << printAsterisks(actRain[i]);//Prints out the asterisks for the totals.
          cout << endl;
       
     }
    

     

     return 0;

}

string printAsterisks(double n)//Asterisk printing function
{
     string s1 (n, 42);//Variable for the string function.
     for(int count=1; count<=n; count++)//For loop that counts the number of asterisks.
         return s1;
}

Firstly:
Thanks for the effort you've put in.

Secondly:
I've made you a little program to demonstrate rounding, using floor() and ceil() [needs cmath, but you already have that]. Here's the code:

//Usual intro :P
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;

//Usual main...
int main(int argc, char *argv[])
{
    //a will be rounding result, x will be float to round
    int a;
    float x;
    
    //get input
    cout<<"Enter float for 'x'"<<endl;
    cin>>x;
    
    //if x + 0.5 is less than x rounded up, decimal place must be less than (.)5
    if ((x+0.5)<ceil(x)){a = floor(x);}
    else{a = ceil(x);}
    
    //print results and pause to view...
    cout<<"You entered:    "<<x<<endl;
    cout<<"Result:         "<<a<<endl;
    system("pause");
    
    //usual return. finito
    return 0;
}

Read through the comments to get the gist of what I'm doing. I have seen some older threads on various C++ forums where people do horrible, long, complicated thingamajigs. This is just a short simple program, and you only need a few lines of it anyway!

Example outputs:

Enter a float for 'x'
[eg.] 23.2
You entered: 23.2
Result: 23

Enter a float for 'x'
[eg.] 18.6758284
You entered: 18.6
Result: 19

Note:
You cannot change the precision with this method, so you can't round, say, 123.34934 to 123.35. There are other methods of doing this, but for a simple graphing program you should only need ints.

Hope this helps. Just reply in this thread if you need any more help :)

Thanks for your help. It worked great.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.