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:

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.

2
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Ancient Dragon

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";

}``````
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.