c++ help

so this is what is given to do :
A video game company has 5 different branches around the world.

Design a vidGamesSales class that keeps sales data for a branch. The class should have the following members:

An array with 4 elements to hold the 4 quarters of sales numbers for the branch.
A private static variable to hold the total company sales for all branches for the year.
A member function that takes 4 arguments. Each argument will represent the sales for a quarter. The value of the arguments should be copied into the array that holds the sales data. The total of the 4 arguments should be added to the static variable that holds the total sales of the entire year.
A function that takes an integer argument within the range of 0-3. The argument is to be used as a subscript into the division quarterly sales array. The function should also return the value of the array element with that subscript.
Write a program that creates an array of 5 vidGamesSales objects. The program should ask the user to enter the sales for the 4 quarters for each branch. After the data are entered, the program should display a table showing the branch sales for each quarter. The program should then display the total company sales for the year.

Input Validation: Accept only positive numbers for the quarterly sales.


using visual studio I'm getting an error and other platforms I am not. what am i missing ?

code I have :
//header files
#include<iostream>
using namespace std;
//class definition
class VideoGamesSales{
private:
//instance variable
int sales[4];
public:
//static variable
static int totalSales;
//function to assign quarterly sale
void quarterlySales(int q1, int q2, int q3, int q4){
sales[0]=q1;
sales[1]=q2;
sales[2]=q3;
sales[3]=q4;
totalSales=totalSales+(q1+q2+q3+q4);
}
//function to get quarterly sales
int getQuarterlySales(int q){
return sales[q];
}
};
//intializing the static variable
int VideoGamesSales::totalSales=0;
//main function
int main(){
//variable to store the branch count
int br=5;
//for storing the user input
int q1,q2,q3,q4;
//array object of the class
VideoGamesSales branch[br];
//asking quaterly sales for all the branches
for(int i=0;i<br;i++){
cout<<"Enter the quarterly sales for branch "<<(i+1)<<endl;
cout<<"Quarter 1: ";
cin>>q1;
cout<<"Quarter 2: ";
cin>>q2;
cout<<"Quarter 3: ";
cin>>q3;
cout<<"Quarter 4: ";
cin>>q4;
//calling function to assign quarterly sales
branch[i].quarterlySales(q1,q2,q3, q4);
}
//printing the table header
cout<<"\t Quarter 1\tQuarter 2\tQuarter 3\tQuarter 4"<<endl;
cout<<" ---------------------------------------------------------------"<<endl;
//iterating through all branches to show quarterly sales
for(int i=0;i<br;i++){
cout<<"Branch "<<(i+1)<<" |\t";
for(int j=0;j<4;j++){
//calling function to get the quarterly details
cout<<branch[i].getQuarterlySales(j)<<"\t\t";
}
cout<<endl;
}
}
using visual studio I'm getting an error and other platforms I am not. what am i missing ?


Well you forgot to include the complete error message and it would be extremely helpful if you added code tags to your code.

Hello Evelynmkm27,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


In line 38 when defining an array the size must be a constant value whether it is a number, 10, or a variable. Try replacing line 34 with constexpr int BR{ 5 }; and see what happens.

Some tips for you:
Use better variable names. "br" may mean something to you, but has no meaning to me or others. When defining the constant variable "MAXSIZE" usually works or you could come up with something different as long as it has the same meaning. "q1" etc. is short, but consider "quarter1" or "qtr1". It makes the program easier to follow without having to look to find what a variable is being used for.

I hope you are not writing your code to benefit the compiler. If you are you have failed because there is to much white space in your code.

Your best choice it to make the code as easy to read and follow as you can. To give an example to compare to your code:
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
//header files
#include<iostream>

using namespace std;

//class definition
class VideoGamesSales
{
    private:  // <--- A class is private by default. This redundant, but OK if you leave it.
        //instance variable
        int sales[4];

    public:
        //static variable
        static int totalSales;  // <--- You could also write "static int totalSales{};".
        
        //function to assign quarterly sale
        void quarterlySales(int q1, int q2, int q3, int q4)
        {
            sales[0] = q1;
            sales[1] = q2;
            sales[2] = q3;
            sales[3] = q4;
            totalSales = totalSales + (q1 + q2 + q3 + q4);
        }

        //function to get quarterly sales
        int getQuarterlySales(int q)
        {
            return sales[q];
        }
};

//intializing the static variable
int VideoGamesSales::totalSales = 0;  // <--- Can be done in the class.

//main function
int main()
{
    //variable to store the branch count
    //int br = 5;
    constexpr int br{ 5 };

    //for storing the user input
    int q1, q2, q3, q4;
    //array object of the class
    VideoGamesSales branch[br];

    //asking quaterly sales for all the branches
    for (int i = 0; i < br; i++)
    {
        cout << "Enter the quarterly sales for branch " << (i + 1) << endl;
        cout << "Quarter 1: ";
        cin >> q1;

        cout << "Quarter 2: ";
        cin >> q2;

        cout << "Quarter 3: ";
        cin >> q3;

        cout << "Quarter 4: ";
        cin >> q4;

        //calling function to assign quarterly sales
        branch[i].quarterlySales(q1, q2, q3, q4);
    }
    //printing the table header
    cout << "\t Quarter 1\tQuarter 2\tQuarter 3\tQuarter 4" << endl;
    cout << " ---------------------------------------------------------------" << endl;

    //iterating through all branches to show quarterly sales
    for (int i = 0; i < br; i++)
    {
        cout << "Branch " << (i + 1) << " |\t";

        for (int j = 0; j < 4; j++)
        {
            //calling function to get the quarterly details
            cout << branch[i].getQuarterlySales(j) << "\t\t";
        }

        cout << endl;
    }

    return 0;  // <--- Not required, but makes a good break point for testing.
}

I did not change everything, but prefer to use the new line, (\n), over the function "endl". These days the (\n) tends to work the same as "endl" and should a "cout" be followed by a "cin" the "cin" will flush the buffer before any input is allowed.

Be careful using the (\t) in the middle of output. It may not always space the way that you want. Using "setw()" from the "<iomanip>" header file is much better.

Fixing the problem with the array should allow the program to compile and run.

Andy
Hello Evelynmkm27,

Am I to take it that you have managed to get the program running?

After looking over your directions I noticed your code is missing the:
Input Validation: Accept only positive numbers for the quarterly sales.

I was also wondering if you should be using a "double" instead of the "int" for the numeric variables?

Also wondering if you know about the "<iomanip>" header file?

I have come up with a function for this. I do not know if you are up to functions yet.

My Input looks like this:


 Enter the quarterly sales for branch 1
  Quarter 1: a

     Invalid entry! Must be a number.

  Quarter 1: -10

     Invalid entry! Can not be a negative number.

  Quarter 1: 100
  Quarter 2: 200
  Quarter 3: 300
  Quarter 4: 400

 Enter the quarterly sales for branch 2
  Quarter 1: 1000
  Quarter 2: 2000
  Quarter 3: 3000
  Quarter 4: 4000


         Quarter 1      Quarter 2       Quarter 3       Quarter 4
 ---------------------------------------------------------------
Branch 1 |      100             200             300             400
Branch 2 |      1,000           2,000           3,000           4,000


Total Sales for all branches is: $11,000.00  // <--- This was not part of your original program.


And with a little changing I came up with this output:

              Quarter 1  Quarter 2  Quarter 3  Quarter 4
 ---------------------------------------------------------------
 Branch 1 |      100.00     200.00     200.00     200.00
 Branch 2 |    1,000.00   1,100.00   1,100.00   1,100.00


Total Sales for all branches is: $5,000.00

The commas are not necessary.

Andy
thank you guys so much this was super helpful!
Registered users can post here. Sign in or register to post.