2

I am working on some programming homework and I am trying to use a for-loop in order to facilitate the process of the coding. Here is the loop:

#ifndef DIVSALES_H
#define DIVSALES_H

class DivSales
{
    public:
        DivSales(){ quarterSales[4] = {0}; };
        double getTotalSales() { return totalSales;}
        static void setTotalSales(double);
        static void addTotalSales(double);
        double getQuarterSales(int numQuarter) {return quarterSales[numQuarter];}
        void setQuarterSales(int numQuarter, double numAmount) { quarterSales[numQuarter] = numAmount;}
    private:
        static double totalSales;
        double quarterSales[];
};

double DivSales::totalSales = 0;
void DivSales::setTotalSales(double totalAmount) {totalSales = totalAmount; }
void DivSales::addTotalSales(double addAmount) {totalSales += addAmount; }
#endif // DIVSALES_H

#include <iostream>
#include "DivSales.h"

using namespace std;

int main()
{
    const int NUMDIVS = 6;
    const int NUMQUARTERS = 4;
    double amount = 0;

    DivSales divs[NUMDIVS];

    for(int division = 0; division < NUMDIVS; division++)
    {
        cout << "Division " << (division + 1) << endl;

        for(int quarter = 0; quarter < NUMQUARTERS; quarter++)
        {
            cout << "Quarter " << (quarter + 1) << ": ";
            cin >> amount;

            divs[division].setQuarterSales(quarter, amount);
            DivSales::addTotalSales(amount);
        }
    }


    return 0;
}

Example of the output:

Division 1
Quarter 1: 500
Quarter 2: 500
Quarter 3: 500
Quarter 2: 500
Quarter 3: 500
Quarter 2: 500
Quarter 3: 500
Quarter 2: 500
Quarter 3: 500
Quarter 2: 

What I am trying to do is make it so that when I have input the numbers for the 4 quarters of a division, that it will move onto the next division. However, after 4 inputs it is not incrementing the division variable of the for-loop instead it continues asking for more inputs. What is going on?

21
  • So it's infinite looping? Commented Oct 25, 2013 at 15:24
  • 1
    Would you mind showing us the rest of the code? Commented Oct 25, 2013 at 15:27
  • 2
    I can't see anything wrong with the code you pasted, so my guess is something's happening inside the setQuarterSales and setTotalSales functions. Have you tried stepping through the code with the debugger? Commented Oct 25, 2013 at 15:29
  • 1
    And/or traced it by hand? Commented Oct 25, 2013 at 15:30
  • 1
    I added the cout and also updated the question with the new code as well as example output with that code. Commented Oct 25, 2013 at 15:48

2 Answers 2

2

I have found what's causing that problem, it is in the file DivSales.h:

Change this line:

double quarterSales[];

For this line:

double quarterSales[4];

The problem was that you were not allocating memory for an array of 4 elements. To initialize it, change your constructor to this:

DivSales():quarterSales({0}){ };

You should also move the following line to DivSales.cpp, because otherwise I was getting a multiple definition error:

double DivSales::totalSales = 0;
Sign up to request clarification or add additional context in comments.

7 Comments

I couldn't even get it to compile without this change.
Would you mind including the code as to how to dynamically allocate the memory for the array? My professor can't explain this stuff to save her own life.
@user2920558 No need for the array to be dynamic. memo1288 gave details on how to statically allocate and initialize the array properly.
@memo1288 Does DivSales():quarterSales({0}){ }; initialize the entire array to 0? Which C++ version is that? MS compiler doesn't like it.
@Sarien Yes, it does. I tested it with MinGW 4.6.2 (dafault settings). Haven't tested it with the MS compiler, though.
|
0

Here:

divs[division].setTotalSales(amount);

you probably want:

divs[division].addTotalSales(amount);

But that is not what is causing your problem. Which I cannot reproduce.

1 Comment

Thank you for the suggestion. It had completely flown over my head but I fixed it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.