C++ Help

edited April 2006 in Internet & Media
There should be a main program and only 2 functions. the first function should be called 4 times and the name of the divison should be passed to every time it is called. the second function should have 4 divisons sales figures passed to it and return the winning divison. can not use global or reference variables

I took program along time ago. I lost my job and it gave me a chance to go back to school again and i am at a loss all i got was a redo in my email with no explation why maybe some one could help. it is an o/e class done over the net so professor help is out of the question. thanks




#include <iostream>
#include <string>


using namespace std;

class region_stats{
public:
region_stats(float north_east = 0, float south_east = 0, float north_west = 0, float south_west = 0){
division_array[0] = north_east;
division_array[1] = south_east;
division_array[2] = north_west;
division_array[3] = south_west;
}

public: //class functionality
float gen_sale_stats(string &division);

private:
float division_array[4];
};

//simple method of sorting
float region_stats::gen_sale_stats(string &division){
cout << division << endl;

float result = 0;

for (int i = 0; i <= 3; i++) {
if (result < division_array){
result = division_array;

}
}

if(result == division_array[0])
division = "North East";
else if(result == division_array[1])
division = "South East";
else if(result == division_array[2])
division = "North West";
else if(result == division_array[3])
division = "South West";
else (division_array[3],division_array[2],division_array[1],division_array[0]<=0);



//Decides what to return;
return result;
}

int main() {

//Load Varibles into class
float a, b, c, d;
cout << "Enter Sales For north east : ";
cin >> a;
cout << "Enter Sales For south east : ";
cin >> b;
cout << "Enter Sales For north west : ";
cin >> c;
cout << "Enter Sales For south west : ";
cin >> d;


//Call Constructor
region_stats sales_qtr(a, b, c, d);

//Notifying User Of Process:
cout << "generating leading divison and its sales figures for the quarter..." << endl;

//calc func
string div = "";
float results = sales_qtr.gen_sale_stats(div);
cout << "The " << div << " Division, has a total sales figure: " << results << endl;

return 0;

}

Comments

  • MiracleManSMiracleManS Chambersburg, PA Icrontian
    edited March 2006
    The first thing I'm going to ask is what you're including. You probably need to include iostream, which doesn't seem to be included properly in your program.

    Also, did you try running it before submitting it? There are some helpful (free) tools around to allow you to debug your software.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2006
    if you post your code inside the [php]
    code here
    
    [/php] tags, it'll make it much easier to troubleshoot - the indents and syntax will stay
  • edited March 2006
    i ahd to change it does any know how i would go about displaying the highest sales . thanks




    // displays highest amounts and the name of the highest.
    #include <iostream>
    #include <iomanip>
    using namespace std;

    int main()
    {

    const int airlines = 1 ;
    int count;
    double sales[airlines], // To hold sales amounts
    highest; // To hold the highest sales


    // Get the sales data.
    cout << "Enter the sales for this week.\n";
    int array_offset = 0;
    for (count = 0; count < airlines; count++)
    {
    cout << "northeast:";
    cin >> sales[count + array_offset];
    array_offset++;

    cout << "southeast:";
    cin >> sales[count + array_offset];
    array_offset++;

    cout << "northwest:";
    cin >> sales[count + array_offset];
    array_offset++;

    cout << "southwest:";
    cin >> sales [count + array_offset];
    array_offset++;
    }


    // Find the highest sales amount.
    highest = sales[0];
    for (count = 1; count < airlines; count++)
    {
    if (sales[count] > highest)
    highest = sales[count];
    }



    // Display the results.
    cout << fixed << showpoint << setprecision(2);


    cout << "The highest sales amount is $" << highest << endl;


    return 0;
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2006
    pinkfloyd: Put your code inside the [ php ]tags.

    eg:
    [ php ]
    #include <iostream>

    int main() {
    int x = 1;
    int y = 2;
    char middleInitial = 'B';

    return 0;
    }
    [ /php ]

    looks like:
    [php]
    #include <iostream>

    int main() {
    int x = 1;
    int y = 2;
    char middleInitial = 'B';

    return 0;
    }
    [/php]
  • edited March 2006
    i guess i just dont get it but this is what i have so far


    // displays highest amounts and the name of the highest.
    #include <iostream>
    #include <iomanip>
    using namespace std;

    int main()
    {

    const int airlines = 1 ;
    int count;
    double sales[airlines * 4], // To hold sales amounts
    highest; // To hold the highest sales


    // Get the sales data.
    cout << "Enter the sales for this week.\n";
    int array_offset = 0;
    for (count = 0; count < airlines; count++)
    {
    cout << "northeast:";
    cin >> sales[count + array_offset];
    array_offset++;

    cout << "southeast:";
    cin >> sales[count + array_offset];
    array_offset++;

    cout << "northwest:";
    cin >> sales[count + array_offset];
    array_offset++;

    cout << "southwest:";
    cin >> sales [count + array_offset];
    array_offset++;
    }


    // Find the highest sales amount.
    highest = 0;
    for (count = 0; count < airlines; count++)
    {
    for (int array_var = 0; array_var < 4; array_var++) {
    if (sales[array_var + count*4] > highest)
    highest = sales[array_var + count*4];
    }
    cout << "Highest for airline is " << highest<<endl;
    highest = 0;
    }




    return 0;
    }
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2006
    Try this . . .

    [php]
    // displays highest amounts and the name of the highest.
    #include <iostream>
    #include <iomanip>
    using namespace std;

    int main()
    {
    const int airlines = 1;
    const int t_sales = airlines * 4;
    int count;
    double sales[t_sales]; // To hold sales amounts
    double highest; // To hold the highest sales

    // Get the sales data.
    cout << "Enter the sales for this week.\n";
    for (int count = 0; count < t_sales; count++) {
    cout << "northeast:";
    cin >> sales[count];

    cout << "southeast:";
    cin >> sales[count];

    cout << "northwest:";
    cin >> sales[count];

    cout << "southwest:";
    cin >> sales [count];
    }


    // Find the highest sales amount.
    highest = 0;
    char name[10] = 0;
    for (count = 0; count < t_sales; count++) {
    if (sales[count] > highest) {
    switch (count) {
    case 1: name = "northeast"; break;
    case 2: name = "southeast"; break;
    case 3: name = "northwest"; break;
    case 4: name = "southwest"; break;
    }
    highest = sales[count];
    }
    }
    cout << "Highest for" << name << " is " << highest << endl;

    return 0;
    }
    [/php]
  • edited March 2006
    i tried that on a2jfreak , and thanks for the help
    But keep getting an error at char name[10] = 0;
    for (count = 0; count < t_sales; count++) {

    C:\Program Files\Microsoft Visual Studio\MyProjects\cpp(33) : error C2440: 'initializing' : cannot convert from 'const int' to 'char [10]'

    any one know what wrong with it.
  • mondimondi Icrontian
    edited March 2006
    0 is an integer, char[10] is a pointer to a block of memory, you cannot assign one to the other.

    You can ignore the assignment, its not nescessary. just use:
    [PHP]
    char name[10];
    [/PHP]
    also in the next section you might need to change the
    [PHP]name = "northeast";[/PHP]
    etc ...
    to
    [PHP]strcpy (name, "northeast");[/PHP]
    and so on..
  • airbornflghtairbornflght Houston, TX Icrontian
    edited March 2006
    when you are doing your pound inlcudes should they not look like this:

    #include <iostream.h>
    

    ?? hey, i think it was html rendering that was taking his header file reference out because html is turned on, cause it just took mine out. lemme post it with the php tag.

    [PHP]#include <iostream.h>
    #include <iomanip.h>[/PHP]
  • edited March 2006
    ok this is what i got for an output which isnt right, thanks mondi that fixed the errors but now this is what its doing.

    Enter the sales for this week
    northeast:23
    southeast:45
    northwest:56
    southwest:567
    northeast:55
    southeast:456
    northwest:456
    southwest:4567
    northeast:5676
    southeast:56
    northwest:43
    southwest:456
    northeast:455
    southeast:67
    northwest:554
    southwest:56
    Highest fornortheast is 0
    Press any key to continue

    thanks every one for the help
  • mondimondi Icrontian
    edited March 2006
    Ill have a look at it in a bit, I didnt actually check the original source. I should have something up later tonight.

    M
  • airbornflghtairbornflght Houston, TX Icrontian
    edited March 2006
    well, just from like 2 seconds looking at it, this:

    [PHP]cout << "Highest for" << name << " is " << highest << endl;[/PHP]

    should look like this:

    [PHP]cout << "Highest for " << name << " is " << highest << endl;[/PHP]

    I'll look at the actuall code now, instead of just fixing the output statement.
  • mondimondi Icrontian
    edited March 2006
    Here you go.

    This asks for the 4 divisions figures and returns the winner. Im confused as to why you'd have to call the first function four times though as the switch takes care of the actual naming .. if this isnt what you need post back and let me know.

    [PHP]
    #include <iostream>

    using namespace std;


    class region_stats{
    public:
    region_stats(float north_east = 0, float south_east = 0, float north_west = 0, float south_west = 0){
    division_array[0] = north_east;
    division_array[1] = south_east;
    division_array[2] = north_west;
    division_array[3] = south_west;
    }

    public: //class functionality
    float gen_sale_stats(char* &division);

    private:
    float division_array[4];
    };

    //simple method of sorting
    float region_stats::gen_sale_stats(char* &division){
    cout << division << endl;

    float result = 0;

    for (int i = 0; i <= 3; i++) {
    if (result < division_array){
    result = division_array;

    }
    }

    if(result == division_array[0])
    division = "North East";
    else if(result == division_array[1])
    division = "South East";
    else if(result == division_array[2])
    division = "North West";
    else if(result == division_array[3])
    division = "South West";
    else (division_array[3],division_array[2],division_array[1],division_array[0]<=0);



    //Decides what to return;
    return result;
    }

    int main() {

    //Load Varibles into class
    float a, b, c, d;
    cout << "Enter Sales For north east : ";
    cin >> a;
    cout << "Enter Sales For south east : ";
    cin >> b;
    cout << "Enter Sales For north west : ";
    cin >> c;
    cout << "Enter Sales For south west : ";
    cin >> d;


    //Call Constructor
    region_stats sales_qtr(a, b, c, d);

    //Notifying User Of Process:
    cout << "generating leading divison and its sales figures for the quarter..." << endl;

    //calc func
    char* div = "";
    float results = sales_qtr.gen_sale_stats(div);
    cout << "The " << div << " Division, has a total sales figure: " << results << endl;

    return 0;

    }
    [/PHP]
  • airbornflghtairbornflght Houston, TX Icrontian
    edited March 2006
    ok, maybe im not that advanced in C++ (im not) but why do you have your cin's in a for loop?
  • mondimondi Icrontian
    edited March 2006
    ok, maybe im not that advanced in C++ (im not) but why do you have your cin's in a for loop?

    is that directed at me? I dont think they are ..

    edit:// looks like youre talking about a2j's code ... thats a good question, theres no reason for it given the parameters
  • airbornflghtairbornflght Houston, TX Icrontian
    edited March 2006
    coo, i was just wondering what the befiets would be, as i dont have a great a mount of programming time in C++ only 10-20 hours
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2006
    My C is EXTREMELY rusty . . . I apologize for any errors in the code I posted.
  • edited April 2006
    nothing can be as bad as my c
Sign In or Register to comment.