Question

In: Computer Science

what am I doing wrong here? thank you! #include <iostream> #include <string> using namespace std; class...

what am I doing wrong here? thank you!

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

class DivSales {
private:
   int quarterSales[4];
   static double totalSales;
public:
   void add(int, int, int, int);
   int sales(int);
   static double getValue()
   {
       return totalSales;
   };
   void DivSales::add(int s1, int s2, int s3, int s4)
   {
       quarterSales[0] = s1;
       quarterSales[1] = s2;
       quarterSales[2] = s3;
       quarterSales[3] = s4;
       totalSales = totalSales + s1 + s2 + s3 + s4;
   }
   int DivSales::sales(int n)
   {
       int value = quarterSales[n];
       return value;
   }
   void error()
   {
       std::cout << "THe input is incorrect";
       system("pause");
       exit(0);
   }
   double DivSales::totalSales = 0.00;
   int main() {
       DivSales ds[6];
       int i, j;
       for (i = 0; i < 6; i++)
       {
           int s1, s2, s3, s4;
           std::cout << "Input quaterly sales" << i + 1 << endl;
           std::cout << "Input Quarter One Sales";
           std::cin >> s1;
           if (s1 < 0)error();
           std::cout << "Input Quarter Two Sales";
           std::cin >> s2;
           if (s2 < 0)error();
           std::cout << "Input Quarter Three Sales";
           std::cin >> s3;
           if (s3 < 0)error();
           std::cout << "Input Quarter Four Sales";
           std::cin >> s4;
           if (s4 < 0)error();
           ds[i].add(s1, s2, s3, s4);
       }
       std::cout << "--------------------------------";
       std::cout << "\t" << "Qarter One" << "\t" << "Quarter Two" << "\t" << "Quarter Three" << "\t" << "Quarter Four" << endl;
       std::cout << "----------------------\n";
       for (i = 0; i < 6; i++)
       {
           std::cout << "Div" << i + 1 << "|";
           for (j = 0; j < 4; j++)
               std::cout << "\t" << ds[i].sales(j);
           std::cout << endl;
       }
       std::cout << "----------------------------------\n";
       std::cout << "\n Total" << ds[0].getValue() <<
       system("pause");
   }
}

Solutions

Expert Solution

Your code has some syntax errors.

Here's the corrected code:

.

#include <iostream>

#include <string>

using namespace std;

class DivSales

{

private:

    int quarterSales[4];

    static double totalSales;

public:

    void add(int, int, int, int);

    int sales(int);

    static double getValue()

    {

        return totalSales;

    }

};

void DivSales::add(int s1, int s2, int s3, int s4)

{

    quarterSales[0] = s1;

    quarterSales[1] = s2;

    quarterSales[2] = s3;

    quarterSales[3] = s4;

    totalSales = totalSales + s1 + s2 + s3 + s4;

}

int DivSales::sales(int n)

{

    int value = quarterSales[n];

    return value;

}

void error()

{

    std::cout << "THe input is incorrect";

    system("pause");

    exit(0);

}

double DivSales::totalSales = 0.00;

int main()

{

    DivSales ds[6];

    int i, j;

    for (i = 0; i < 6; i++)

    {

        int s1, s2, s3, s4;

        std::cout << "\nInput quaterly sales " << i + 1 << endl;

        std::cout << "Input Quarter One Sales: ";

        std::cin >> s1;

        if (s1 < 0)

            error();

        std::cout << "Input Quarter Two Sales: ";

        std::cin >> s2;

        if (s2 < 0)

            error();

        std::cout << "Input Quarter Three Sales: ";

        std::cin >> s3;

        if (s3 < 0)

            error();

        std::cout << "Input Quarter Four Sales: ";

        std::cin >> s4;

        if (s4 < 0)

            error();

        ds[i].add(s1, s2, s3, s4);

    }

    std::cout << "--------------------------------\n";

    std::cout << "Quarter\tOne\tTwo\tThree\tFour" << endl;

    std::cout << "----------------------------------\n";

    for (i = 0; i < 6; i++)

    {

        std::cout << "Div: " << i + 1 << "|";

        for (j = 0; j < 4; j++)

            std::cout << "\t" << ds[i].sales(j);

        std::cout << endl;

    }

    std::cout << "----------------------------------\n";

    std::cout << "\n Total: " << ds[0].getValue() << endl

              << system("pause");

}

.

For testing i have entered 1 2 3 4 only

Output:

.


Related Solutions

What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std;...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std; float series(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum += r[i];    return sum; } float parallel(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum = sum + (1 / r[i]);...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact {...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact { public: Contact(string init_name = "", string init_phone = "000-000-0000"); void setName(string name); void setPhone(string phone); string getName()const; string getPhone()const; friend ostream& operator << (ostream& os, const Contact& c); friend bool operator == (const Contact& c1, const Contact& c2); friend bool operator != (const Contact& c1, const Contact& c2); private: string name, phone; }; Contact::Contact(string init_name, string init_phone) { name = init_name; phone = init_phone;...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string t, string a, double d); //parametrized constructor string getTitle()const; // return title string getAuthor()const; // return author double getDurationMin() const; // return duration in minutes double getDurationSec() const; // return song's duration in seconds void setTitle(string t); //set title to t void setAuthor(string a); //set author to a void setDurationMin(double d); //set durationMin to d private: string title; //title of the song string author;...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; class Prescription {    friend ostream&...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; class Prescription {    friend ostream& operator<<(ostream&, const Prescription&);    friend istream& operator>>(istream&, Prescription&);    private: int idNum; int numPills; double price;    public: Prescription(const int = 0, const int = 0, const double = 0.0); }; Prescription::Prescription(const int id, const int pills, const double p) {    id = id;    numPills = pills;    price = p; } ostream& operator<<(ostream& out, const Prescription pre) {    out <<...
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string userInput; getline(cin, userInput);...
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string userInput; getline(cin, userInput); // Declaring base int N = 30; if (userInput.length() > 10) { cout << 0 << endl; } else { int finalTotal = 0; //Iterates through userInput for(int i = 0; i < 10; i++){ char convertedInput = userInput[i]; // ASCII decimal value of each character int asciiDec = int(convertedInput); //Casts char value from input to int value stringstream chr; chr << convertedInput; int...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle {...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle { //defining public variables public: double pi; double radius; public: //default constructor to initialise variables Circle(){ pi = 3.14159; radius = 0; } Circle(double r){ pi = 3.14159; radius = r; } // defining getter and setters void setRadius(double r){ radius = r; } double getRadius(){ return radius; } // method to get Area double getArea(){ return pi*radius*radius; } }; //main method int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to store user input bool cont = true; //implement a loop so that it will continue asking until the user provides a positive integer // the following provides ONLY part of the loop body, which you should complete { cout <<"How many words are in your message? \n"; cout <<"Enter value: "; // get user input integer here    cout <<"\nInvalid value. Please Re-enter a...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT