problems with calculations

I have some major calculation problems. My program is suppose to take the price of the computer in stock and multiply it by how many in inventory.
Ex: Price of computer: 2000
Total in inventory: 2
Total price: 4000

Instead I'm getting: -1.3990 e+134

I am new to classes so I'm sure there is a lot wrong with this program, and i am willing to accept any help please!


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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* 
 Author: James Krause
 UserId #: c1010b27
 Creation Date: 04/26/2012
 Modification Date: 04/26/2012
 
 Course: CISP 1010 P02
 File name:  lab8.cpp
 
 Inputs: cpu, os, quantity, price, computer.
 Ouputs: cout
 Formulas: none
 Test Data: Use different Processors, different operating systems, and also make sure that it continues to add to previous stock counts
 
 Purpose: To gain familiarity with structures and classes.
 
 */

#include <iostream>
#include <string>
using namespace std;

// GLOBAL DECLARATION
const int MAX_COMPUTERS=5;

// class blue print for inventory (CPU type, OS, Quantity, Price)
class Inventory
{
public:
    static void orderPrice(const int MAX_COMPUTERS, double price[]); // Declare function to order arrays by price.
	string cpu; 		// Processor
	string os; 		// Operating System
	int quantity;		// How many in stock
	double price[MAX_COMPUTERS]; // Price of computer
};

void get_data (Inventory& computer);
// accepts one computer at a time and stores values into the member variables of the inventory structure

int main ()
{
    // Declare variables
    Inventory computer;
    int totalInventory = 0;
    double totalPrice = 0.00;
    double price[MAX_COMPUTERS];
    
	for (int i=0; i <= MAX_COMPUTERS; i++)
	{
        
        get_data(computer);
		totalInventory += computer.quantity; // Add new computer stock to total stock
		totalPrice += computer.quantity * computer.price[i]; // Add new computer price to total price
		cout << "Processor: " << computer.cpu << endl;
		cout << "Operating System: " << computer.os << endl;
		cout << "In Stock: " << computer.quantity << endl;
		cout << "Price: " << computer.price[i] << endl;
		cout << "Total inventory: " << totalInventory << endl;
		cout << "Total price: " << totalPrice << endl;
	}
    
    Inventory::orderPrice(MAX_COMPUTERS, price);
    
    cout << "Total inventory: " << totalInventory << endl;
    cout << "Total price: " << totalPrice << endl;
    
    return 0;
    
}

void get_data (Inventory& computer)
{
    cout << "Enter Processor Model: ";
	cin >> computer.cpu;
	cout << "Enter Operating System: ";
	cin >> computer.os;	
	cout << "Enter stock counts: ";
	cin >> computer.quantity;
	cout << "Enter Price: ";
	cin >> computer.price[MAX_COMPUTERS];
}

void Inventory::orderPrice(const int MAX_COMPUTERS, double price[])
{  
    int i, j, flag = 1;    // set flag to 1 to start first pass
    double temp;             // holding variable
    for(i = 1; (i <= MAX_COMPUTERS) && flag; i++)
    {
        flag = 0;
        for (j=0; j < (MAX_COMPUTERS -1); j++)
        {
            if (price[j+1] < price[j])    
            {
                temp = price[j];
                price[j] = price[j+1];
                price[j+1] = temp;
                
                flag = 1;               
            }
        }
    }
    return;
}
Last edited on
cin >> computer.price[MAX_COMPUTERS];

This always puts the price in computer.price[5] which is also out of bounds.
computer.price[0] is not populated.
computer.price[1] is not populated.
etc.
Okay do you think that is causing all the calculation errors too?
Topic archived. No new replies allowed.