i have this code where i have to take user input of sales and display it as a chart.
(see code/sample run below)
my problem is if the user enters like $923.89, it should round down to $900 -but with the code i have, it moves it up to 1000. please help me, as i dont know what im missing-all i know is that i think my if statement is wrong.

#include <iostream>
using namespace std;

int main()
{
  double sale1, sale2, sale3, sale4, sale5, total;
  double s1, s2, s3, s4, s5;

  

  cout << endl;

  char choice = 'y';
	while (choice == 'y' || choice == 'Y')
	{


  cout << "This program will ask for your store's sales for the  day\n";    
  cout << "After all sales have been put in, a chart will be printed out\n";

  cout << endl;

  cout << "Enter today's sale for store 1: $";
  cin >> sale1;
  

  cout << "Enter today's sale for store 2: $";
  cin >> sale2;
  

  cout << "Enter today's sale for store 3: $";
  cin >> sale3;


  cout << "Enter today's sale for store 4: $";
  cin >> sale4;


  cout << "Enter today's sale for store 5: $";
  cin >> sale5;


  total = sale1 + sale2 + sale3 + sale4 + sale5;
  cout << endl;
  cout << "Total: $" << total << endl;

  
  cout << endl;
  cout << "SALES BAR CHART (* = $100)" << endl;
  cout << "--------------------------" << endl;

  s1 = sale1/100;
  s2 = sale2/100;
  s3 = sale3/100;
  s4 = sale4/100;
  s5 = sale5/100;
  int row;
  
  if( (sale1 - s1*100 ) > 49.99  )
    s1 = s1 + 1;
  cout << "Store 1: ";
  for ( row = 0; row < s1; row++)
    cout << '*';

  if( (sale2 - s2*100 ) > 49.99  )
    s2 = s2 + 1;
  cout << "\nStore 2: ";
  for ( row = 0; row < s2; row++)
    cout << '*';

  if( (sale3 - s3*100 ) > 49.99  )
    s3 = s3 + 1;
  cout << "\nStore 3: ";
  for (int row = 0; row < s3; row++)
    cout << '*';

  if( (sale4 - s4*100 ) > 49.99  )
    s4 = s4 + 1;
  cout << "\nStore 4: ";
  for (int row = 0; row < s4; row++)
    cout << '*';

  if( (sale5 - s5*100 ) > 49.99  )
    s1 = s1 + 1;
  cout << "\nStore 5: ";
  for (int row = 0; row < s5; row++)
    cout << '*';

  

 
  
  cin.get();
  cin.ignore();

  cout << endl;
  cout << "Repeat Program? ";
  cin >> choice;

  
	
  }

  return 0;
}

this was my sample run:

This program will ask for your store's sales for the day.
After all sales have been put in, a chart will be printed out.

Enter today's sale for store 1: $820
Enter today's sale for store 2: $900
Enter today's sale for store 3: $860
Enter today's sale for store 4: $900
Enter today's sale for store 5: $920

Total: $4400

SALES BAR CHART (* = $100)
--------------------------
Store 1: *********
Store 2: *********
Store 3: *********
Store 4: *********
Store 5: **********

as you can see from above, 820 was promoted to 900
and then for some strange reason 920 is promoted to 1000 instead of 900. ive been working on this all day and i cant seem to break it.

Dani AI

Generated

Two things are happening in the posted code. First, using a double as the loop limit invites floating‑point rounding error: decimal values like 8.2 are not exact in binary, so sale/100 can be slightly above the expected integer (8.1999999...), and the for (row = 0; row < s1; row++) check will allow an extra iteration. Second, there is a copy/paste bug in the Store 5 block that increments s1 instead of s5, which will mutate the wrong counter.

A robust, homework‑friendly fix is to compute the number of stars with integer arithmetic (truncate to whole dollars, use integer division and remainder, then an if to round). This avoids floating comparisons entirely:

int stars = static_cast<int>(sale) / 100;
int rem = static_cast<int>(sale) % 100;
if (rem >= 50)
    ++stars;
for (int i = 0; i < stars; ++i)
    std::cout << '*';

Notes and quick checks: use static_cast<int> to drop cents before the / and % so rounding is predictable; use rem >= 50 rather than > 49.99 to avoid floating comparisons. If cents must affect rounding (e.g., 949.99 -> up), convert to cents first (int cents = int(sale * 100 + 0.5)) and do the same integer math. Finally, fix the s5 increment (change the mistaken s1 = s1 + 1; to s5 = s5 + 1;) and print the computed stars during testing to confirm expected results.

Recommended Answers

All 3 Replies

Here is how to round down

#include <iostream>
#include <cmath>
using std::cout;

int main()
{
    double n = 923.45;
    double intpart;
    double fractpart = modf(n/100, &intpart);
    cout << intpart * 100 << "\n";
	return 0;
}

^^is there another way to do it besides using the math function?
im suppose to just use a if or for loop only,
(this is why im having trouble!)

convert to integer

int main()
{
    double n = 923.15;
    int x = (int)n/100 * 100;
    cout << x << "\n";

}
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.