Math not quite adding up

I am trying to output something that looks like this:

Drug Name                Amount in Inventory
Coumadin (1MG)                   0
Coumadin (2MG)                   0
Coumadin (2.5MG)                0
Coumadin (3MG)                   350
Coumadin (4MG)                   0
Coumadin (5MG)                   0
Coumadin (6MG)                   0
Coumadin (7.5MG)                954
Coumadin (10MG)                  0
Warfarin (1MG)                   870
Warfarin (2MG)                   0
Warfarin (2.5MG)                224
Warfarin (3MG)                   0
Warfarin (4MG)                   0
Warfarin (5MG)                   0
Warfarin (6MG)                   900
Warfarin (7.5MG)                0
Warfarin (10MG)                  0
                7.95081e+070

Program will now terminate.
Press any key to continue . . .



However, the math in the right column above is not right, according to the numbers. And what that "7.95081e+070" is doing at the end, I have no idea. Looking for a little help. Pay no attention to the unnecessary "#includes" below. Some of the script is not pasted here, but what is necessary, is here. Any help is GREATLY appreciated!



#include <iostream>
#include <iomanip>
#include "conio.h"
#include <array>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cctype>
#include <cmath>

using namespace std;

int main()
{

	char date[9];
	_strdate_s(date);
	const int SIZE = 19;
	int stock[SIZE] = {0,5,15,21,7,5,5,25,18,12,15,21,7,5,5,25,18,12,33};


	double strength[] = {" "[0],"1"[1],"2"[2],"2.5"[3],"3"[4],"4"[5],"5"[6],"6"[7],"7.5"[8],"10"[9],
		"1"[10],"2"[11],"2.5"[12],"3"[13],"4"[14],"5"[15],"6"[16],"7.5"[17],"10"[18]};
	const int MAXITEMS = 19;
	string inventory[MAXITEMS] = {" ","Coumadin (1MG)   ",
		"Coumadin (2MG)   ",
		"Coumadin (2.5MG)",
		"Coumadin (3MG)   ",
		"Coumadin (4MG)   ",
		"Coumadin (5MG)   ",
		"Coumadin (6MG)   ",
		"Coumadin (7.5MG)",
		"Coumadin (10MG)  ",
		"Warfarin (1MG)   ",
		"Warfarin (2MG)   ",
		"Warfarin (2.5MG)",
		"Warfarin (3MG)   ",
		"Warfarin (4MG)   ",
		"Warfarin (5MG)   ",
		"Warfarin (6MG)   ",
		"Warfarin (7.5MG)",
		"Warfarin (10MG)  "};

	int menu;

	cout <<"*********************************** \n";
	cout <<"*********************************** \n";
	cout <<"**      Hazardous Material       ** \n";
	cout <<"**    Retrieval and Disposal     ** \n";
	cout <<"*********************************** \n";
	cout <<"*********************************** \n";
	cout <<"**                               ** \n";
	cout <<"**     1. View Drug Inventory    ** \n";
	cout <<"**                               ** \n";
	cout <<"**     2. Quit                   ** \n";
	cout <<"**                               ** \n";
	cout <<"*********************************** \n";
	cout <<"*********************************** \n";
	cout <<" \n";

	cin >> menu;

	switch (menu)
		{
	case 1:
		{

			cout << "Drug Name                Amount in Inventory" << endl;
			int x;
			for (x = 1; x <= 18; x++)
			{
				double milligrams = ((stock[x]) * (strength[x]));
				cout << inventory[x] << "                " << milligrams << endl;
			}
		}
	case 2:
		{			
			{            
				cout << " " << endl;
				cout << "Program will now terminate." << endl;                
			}
			system("pause");
		}
		}
	}
I should note: The goal is for the right column show above, to equal the result of an int multiplied by a double.
1
2
double strength[] = {" "[0],"1"[1],"2"[2],"2.5"[3],"3"[4],"4"[5],"5"[6],"6"[7],"7.5"[8],"10"[9],
    "1"[10],"2"[11],"2.5"[12],"3"[13],"4"[14],"5"[15],"6"[16],"7.5"[17],"10"[18]};


That's a pretty odd format for an array declaration.

EDIT: Hah, beat you shacktar!
Last edited on
1
2
double strength[] = {" "[0],"1"[1],"2"[2],"2.5"[3],"3"[4],"4"[5],"5"[6],"6"[7],"7.5"[8],"10"[9],
		"1"[10],"2"[11],"2.5"[12],"3"[13],"4"[14],"5"[15],"6"[16],"7.5"[17],"10"[18]};


The above is not doing what you want. In each case the nth character (where n is in the square brackets) in the char array is assigned to a double. In most cases this is garbage since the nth character is beyond the end of the array.

Did you want to do this instead?

1
2
double strength[] = {0.0, 1.0, 2.0, 2.5, 3.0, 4.0, 5.0, 6.0, 7.5, 10.0, 1.0, 2.0, 2.5,
           3.0, 4.0, 5.0, 6.0, 7.5, 10.0};


EDIT: I accept defeat...this time
Last edited on
i think u guys didnt quite answer the question, the reason the math is coming out weird is that float/double/long double math is an approximation, which means a lot of the time, some off digit is going to come up in the end, so this is how i would fix that (or just one suggestion)

1
2
3
4
5
6
7
8
9
#include <math>

const int PRECISION = 5

double Precision (double d)
{
    double precision = static_cast<double>(pow(10, PRECISION))
    return (static_cast<double>(static_cast<int>(d * precision)))/precision
}


which will chop off the digits past the 5th decimal point, ie: 105.64354780000002 -> 105.64354
@GFreak45: Are you suggesting that 5x1 is approximately 0? Or maybe 15x2 is approximately 0? Maybe 33x10 is approximately 0?
The question of approximation will likely come up after we're multiplying the right numbers ;)
by approximation i dont mean 5x1 would = 0 i mean 5.0 x 1.0 would come up with something like 5.000000000001 in some cases, and i didnt really go through the code that much i just realized it was an issue dealing with float math so i figured i would mention a very common but not very well known float issue
His output indicates that he's getting 0 for almost all of his results. That would indicate that it's not an approximation problem.
Did you want to do this instead?

Shaktar get the props on the code below. It's both clean, and works beautifully! :-)

double strength[] = {0.0, 1.0, 2.0, 2.5, 3.0, 4.0, 5.0, 6.0, 7.5, 10.0, 1.0, 2.0, 2.5,
           3.0, 4.0, 5.0, 6.0, 7.5, 10.0};


I am still getting the extra printing at the bottom. Any word on the cause of that?

7.95081e+070


Thanks in advance!
Nevermind. I figured it out.
I had the array set to 19 max, forgetting about the "0"!

Oops! :-)
Last edited on
Topic archived. No new replies allowed.