Bug at the end of my program
Feb 15, 2015 at 5:37pm UTC
I have no idea why this isn't working. I've tried so many things. It runs all the way through but says that the "program stopped working" error right as it exits. PLEASE, HELP!!!!
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
#include <iostream>
#include <climits>
#include <string>
#include <iomanip>
#include <vector>
using namespace std;
// Number of divisions
const int numOfDivs = 4;
// Prototypes for retrieving the sales and finding highest sales
double getSales(vector<string>&);
void findHighest(double [], string [], int );
int main(void )
{
vector<string> division(numOfDivs);
string branch[numOfDivs]; // Array to hold the names of each division
double sales[numOfDivs]; // Array to hold the sales numbers for each of the divisions
string firstDiv, // Strings to enter the name of each division
secondDiv,
thirdDiv,
fourthDiv;
for (int count = 0; count < numOfDivs; count++)
{
cout << "Division: " ;
if (count != 0)
cin.get();
getline(cin, division[count - count]);
sales[count] = getSales(division);
branch[count] = division[count - count];
division.clear();
}
cout << endl;
findHighest(sales, branch, numOfDivs); // Passes the sales array, branch array and the size of the amount of divsions
// to the findHighest function
system("PAUSE" );
return 0;
}
// Retrieves the sales figures for each division and returns them
double getSales(vector<string>& division)
{
// Amount of money
double sales;
do
{
cout << division[0] << " Division quarterly sales: " ;
cin >> sales;
if (!cin || sales < 0)
{
cin.clear();
cin.ignore(INT_MAX, '\n' );
cout << "Enter valid sales: " ;
cin >> sales;
}
} while (!cin || sales < 0);
if (!division.empty())
division.pop_back();
else
division.clear();
return sales;
}
// Finds the division with the highest sales and displays the name and the amount of money
void findHighest(double sales[], string region[], int divs)
{
double highest;
highest = sales[0];
for (int count = 0; count < divs; count++)
{
if (sales[count] < sales[count + 1])
{
highest = sales[count + 1];
}
}
for (int index = 0; index < divs; index++)
{
if (highest == sales[index])
{
cout << "Highest Grossing Division: " << region[index];
cout << "." << endl;
cout << fixed << showpoint << setprecision(2) << endl;
cout << "Sales: $" << highest << endl;
}
}
}
Feb 15, 2015 at 6:40pm UTC
I may be wrong but i think it lies in your use of !cin, needs to read into something... Cin >> variable
Feb 15, 2015 at 6:49pm UTC
That shouldn't be an issue. That's just saying if cin is not the right type of input.
Topic archived. No new replies allowed.