Array ?

This is a discussion on Array ? within the C++ Programming forums, part of the General Programming Boards category; Hello Everyone, I am having trouble all lweek trying to figure how to ask the user to input product number, ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    90

    Array ?

    Hello Everyone,

    I am having trouble all lweek trying to figure how to ask the user to input product number, total of sale, and -1 to end input which should do the math. I made it to this point on my own (accually from the book redone and I understand this part well) but I don't know where to start the second stage of this program. Please forgive me for any bad indentations. I have to write it this way so I can see where Im going until completed. This program works to a certain point. I can do the math but I don't have it corrected here yet. It add the size of the column to the total for some strange reason. I had a great start with this one and just knew I could do it, but I cannot

    Thanks in advance

    Code:
    #include <string>		//  for .h
    #include <iostream>
    #include <iomanip>// parameterized stream manipulators
    
    using namespace std;
    
    
    // ....................
    // .................... .h
    // ....................  class definition
    class Constructor0
    {
    public:
       // constants
       static const int ROW_S = 4;//  number of Salesmen
       static const int COLUMN_S = 5;//  number of column (numbers or SALES)
    
       // constructor initializes company name and array groups
       Constructor0( string, const int [][ COLUMN_S ] );
    
       void setName( string );
       string getSubLine();
       void printMessage();
       void process_ARRAYS();
       int getMinimum();
       int getMaximum();
       double getTOTAL_0( const int [], const int );
       void bar_Chart_1();
       void output_M_N();
    private:
       string subName;//  sub name for this group
       int _M_N[ ROW_S ][ COLUMN_S ];//  two-dimensional array of _M_N
    };
    
    // ....................
    // .................... cpp
    // ....................  Member-function definitions
    
    // two-argument constructor initializes subName and arrayList
    
    Constructor0::Constructor0( string name, const int array1[][ COLUMN_S ] )
    {
       setName( name );
    
       for ( int rows = 0;  rows < ROW_S;  rows++ )//  copy array1 info
    
       for ( int j = 0;  j < COLUMN_S;  j++ )
             _M_N[ rows ][ j ] = array1[ rows ][ j ];
    }
    ///////
    ///////
    void Constructor0::setName( string name )
    {
       subName = name;
    }
    ///////
    ///////
    string Constructor0::getSubLine()
    {
       return subName;
    }
    ///////
    ///////
    void Constructor0::printMessage()
    {
       cout << endl;
       cout << "Enter the salesperson ( 1 - 4 ), product number ( 1 - 5 ),\
    and total sales" << endl << getSubLine() << "."
          << endl;
    }
    /////////
    /////////
    /////////
    /////////
    /////////
    /////////   perform various operations on the data
    /////////
    
    void Constructor0::process_ARRAYS()
    {
       output_M_N();                 //  call output-_M_N-array
    //    << "\nHighest grade in the grade book is " << getMaximum() << endl;
    }
    /////////pass row of _M_N and the value of SALES
    /////////   determine TOTAL_0
    /////////
    double Constructor0::getTOTAL_0( const int setOf_ARRAYS[], const int COL )
    {
     int total = 0;
    
       for ( int j = 0; j < COL; j++ )//  add each column context
          total += setOf_ARRAYS[ j ];
    
    //  return static_cast< double >( total ) + _M_N;
        return double( total ) + COL;// return averages
    }
    /////////
    /////////   output the contents of the _M_N array
    /////////
    void Constructor0::output_M_N()
    {
     cout << "\nThe _M_N are:\n\n";
     cout << "      ";//  align column heads.
    //  Create col heading
     for ( int j = 0; j < COLUMN_S; j++ )//  for each (_5) col
       cout << "     Item-" << j + 1 << "  ";//  Main col heading
    
       cout << "  Total" << endl;//  col heading at end
    
                                                //  create rows/cols of
                                                //  text representing array
                                                //  _M_N. (_5) rows.
    for ( int rows = 0; rows < ROW_S; rows++ )
     {
         cout << "SP-" << rows + 1;
    
      for ( int j = 0; j < COLUMN_S; j++ )// out stud sales
         cout << setw( 10 ) << _M_N[ rows ][ j ] << ".00";// inside of table
    
        //cout << setw( 10 ) << setprecision( 2 ) << fixed << TOTAL_0 << endl;
    
    
        // call member function getTOTAL_0 to calculate saleperson's (row) TOTAL_0;
        // pass row of _M_N and the value of SALES as the arguments
        // Push last column right = 9 -- 84.33
        double TOTAL_0 = getTOTAL_0( _M_N[ rows ], COLUMN_S );
        cout << setw( 9 ) << setprecision( 2 ) << fixed << TOTAL_0 << endl;
     } // end outer for
    }
    
    
    // ....................
    // .................... cpp
    // ....................
    
    int main()
    {
    // two-dimensional array of saleperson (row) _M_N
       int array1[ Constructor0::ROW_S ][ Constructor0::COLUMN_S ] =
    { { 100, 0, 500, 0, 0  },
      { 0, 200, 0, 500, 600  },
      { 0, 50, 500, 0, 750  },
      { 0, 0, 0, 60, 70  } };
    
    Constructor0 myMulti_2D( "Enter -1 for the salesperson to end input.", array1 );
       myMulti_2D.printMessage();
    
    
    // cin >> array1[i][j];
    
       myMulti_2D.process_ARRAYS();
    
       cout << endl;
       cout << endl;
    
    system("pause");
    system ("CLS");
    return main();
    }
    Last edited by sharris; 10-31-2010 at 10:05 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > return double( total ) + COL;// return averages
    Do you know how to calculate an average?

    > return main();
    This just makes the code loop indefinitely, eating up stack until it all goes boom.
    Try just
    return 0;

    A bit more attention to indentation would help everyone.
    SourceForge.net: Indentation - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    Code:
    return double( total ) + COL;// return averages
    I just tried this and did not work but I'll be trying it more through-out the day. Thanks but it would help to see more. I'm a real noob to C++ who also spend most of his day working for a living slanging bricks instead of writing code. I wish I could be an expert in 10 weeks of learning C++ in on-line.

    The reason why I have "return main" is so I can do it all over again with-out restarting the program. This sould not be an problem for other coders and it has nothing to do with the question.

    About indentation, reading code should not be a issue for anyone of experence. If you find some un-indented code that worked and you badly needed it because you could not figure it out, would you be able to read it?

    Indentation should not be a reason not to provide help to other users who has their own style of writing unless it's completly insane. I might be in shool learning C++ but its far from obtaining a job in the field. I'm learning C++ so I can do a few things on my own machines, not just to earn an {A} in classroom.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,699
    Well anyway, an average in math is the sum of the elements divided by the number of elements. Since in your original post you wrote
    >> It add the size of the column to the total for some strange reason.
    And in the code we have the line
    Code:
    return double( total ) + COL;// return averages
    Then, I think I've pointed out the problem.

    >> The reason why I have "return main" is so I can do it all over again with-out restarting the program. This sould not be an problem for other coders and it has nothing to do with the question.
    Just trying to help out. Returning main() just causes main() to recursively call itself forever. If you want to use the entire program again without restarting, try using a loop.
    Last edited by whiteflags; 11-01-2010 at 10:23 AM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,753
    The reason why I have "return main" is so I can do it all over again with-out restarting the program. This sould not be an problem for other coders and it has nothing to do with the question.
    In C/C++ main is the entry point for your program. You can not call main in any function, including main.

    You need to use loops to repeat your code.


    About indentation, reading code should not be a issue for anyone of experence. If you find some un-indented code that worked and you badly needed it because you could not figure it out, would you be able to read it?
    Proper indentation makes the code easier to read. With proper indentation you will get a lot more replies as it doesn't take as long to read. It will also help you in finding many of your problems in the future.

    Where did you try?


    Code:
    return double( total ) + COL;// return averages
    I just tried this and did not work but I'll be trying it more through-out the day. Thanks but it would help to see more. I'm a real noob to C++ who also spend most of his day working for a living slanging bricks instead of writing code. I wish I could be an expert in 10 weeks of learning C++ in on-line.
    Why did it not work?

    What kind of error messages did you get when you compiled?

    If it compiled what were the results?

    What did you expect?

    Jim

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,290
    Quote Originally Posted by jimblumberg View Post
    In C/C++ main is the entry point for your program. You can not call main in any function, including main.
    The C++ standard specifically disallows calling main().

    The C standard does not disallow calling main() but, practically, doing so is considered highly inadvisable.

    Quote Originally Posted by sharris View Post
    The reason why I have "return main" is so I can do it all over again with-out restarting the program. This sould not be an problem for other coders and it has nothing to do with the question.
    What you're not realising is that calling main() is a sign of a serious problem in your code. That's the reason others are flagging the concern.

    It also does not have the effect of "restarting the program".
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,753
    Quote Originally Posted by grumpy View Post
    The C++ standard specifically disallows calling main().

    The C standard does not disallow calling main() but, practically, doing so is considered highly inadvisable.
    Thanks grumpy, I didn't know that C does not disallow calling main().

    Jim

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    Trust me, I been told big-time, "Indentation is important". As I learn I do intent to respect that. But right now I write to please MY eyes and they have not complain yet. heehee

    My problem is being pressed for time just like anyone else, while still knowing that I need to fix the indentation when it's time to ask for help, so I must than prepare to please other people "EYE'S" also. For instance; I did that once (fixing my bad indentation), and by time I was finished, I forgot the question I wanted to ask, than had to rush out the door to get to work, where I was late. By time I got back home to get back on the code, my indentation was still not perfect. Now I dump the entire idea and went another way. It paid off but still I feel I missed something I needed to know.

    If you re-read my original question, math was an issue that I said I need to address latter but right now:

    I am having trouble all week trying to figure how to ask the user to input product number, total of sale, and -1 to end input which should do the math. I made it to this point on my own (accually from the book redone and I understand this part well) but I don't know where to start the second stage of this program.
    Stupit of me because we learn some of that all the way up to just last week but now I don't know how to included all I learned in this week project. It's a whole new ball game. It's the perfect lesson to include what one should have already know ... but I STILL don't know

    I also begged for forgiveness in advance, hoping that all would save the indentation SPEECH..

    Please forgive me for any bad indentations. I have to write it this way so I can see where Im going until completed.
    Love you C++ guys and gals. Don't think I don't appreciate this forum. It's the best in the world I seen. It's just that the answers are kind of like on the other side of the tracks right now.

    The first thing I learn and did was "RETURN MAIN" for the reason stated in my second post. It's the kind of things that is not standard but "it works" up to 70% in most cases, "BUT NOT ALL". Indentation, well, I'll try do better next round. It's hard to get an answer to an original problem when we also have other mistakes in our code which cause other to focus on that only while trying to help. I always had a problem at trying to get my point across. It's no one fault but my bad ENGLISH and now indentation. Take it from the top and you see what I mean.

    PS: But you never use "RETURN MAIN" in your final product. Just because it works now don't me it will work as the project grows. My guest is, that's why it is recommended not to be used in the first place.
    Last edited by sharris; 11-01-2010 at 07:39 PM.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,699
    >> I am having trouble all week trying to figure how to ask the user to input product number, total of sale, and -1 to end input which should do the math. I made it to this point on my own (accually from the book redone and I understand this part well) but I don't know where to start the second stage of this program.

    OK well to be honest I have a lot to say about the code itself. It looks like that you took another program which was not object oriented and then tried to make it object oriented. There also weren't any input routines in the previous program. To be completely honest the things that you now have to retrofit means as a consequence a major redesign.

    This is only going to help if my assumptions are correct. I assume that the multidimensional array that you currently have is the type of input that you need to accept.

    Basically to do that, you have to consider how the input routine will look.
    Enter -1 for the salesperson to end input.
    Enter salesperson ID: 0
    Enter sales counts. A negative sale count is not allowed and signals the end of input.
    Week 1: 100
    Week 2: 0
    Week 3: 500
    Week 4: 0
    Week 5: 0
    Week 6: -1
    I assume this type of setup is ideal, and forgive me if I made a mistake in details that you've neglected to share. The point is that if we analyze this we can discover what tools we need to use and the types of variables we need.

    The input and output stuff is handled by cin and cout respectively. Besides the fact that you count weeks, the rest of the output is hard coded strings. We are taking in numbers for the most part, so that is another task we need to analyze as a smaller problem.

    Putting that aside, we need to organize the data into a structure that represents a salesperson. In the old program, this structure was a multidimensional array, but in the new program we should be using an object.

    This is by no means a good tutorial on object orientedness so I will preface that now. Take responsibility for that area of your education. Basically, we can look at the current problem and notice that there are data we can organize and call a salesperson. Every salesperson carries an ID number and a number of sales. So we write an object that makes the idea of a single salesperson concrete. You just need to decide how to store the sales and id information. We also need to implement how we use that information.
    Code:
    class Salesperson
    {
       private:
       int sales;
       int weeksWorked;
       int id;
    
       public:
       Salesperson( int idIn, int salesIn = 0, int weeksIn = 0 ): id( idIn ), sales( salesIn ), weeksWorked( weeksIn )
       {
          // nothing
       }
        
       void sales( int weeklyCount )
       {
          sales += weeklyCount;
          ++weeksWorked;
       }
       
        void display( )
        {
           cout << "Salesperson " << id << " = " << sales << " sales" << " / average = " << sales / weeksWorked << endl;
        }
    };
    That's at least a little less of a failure than what we had before. In reality there might be a lot more data to hold onto and a lot more stuff that an object needs to do. But at least now we have an object that represents what we know about the beast called a Salesperson, and we can represent a number of them with a flat, one-dimensional array. If this particular example doesn't make sense, then you need to hit the books.

    so now we get to the meat of the problem, how to get user input into the array after you've declared a group of salespeople. Well the sales function adds new sales all the time, so you just need to get a single integer, several times perhaps, for each indicated salesperson.

    For the "get a single integer" part, use cin and a spare integer to pass to sales(int); ... and for the "several times" part, you just need to use a loop.

    So we can make a series of steps:
    1. Prompt for and input the salesperson id.
    2. While entering weekly sales (count >= 0):
    3. Prompt for a specific week.
    4. cin >> count;
    5. people[id].sales(count);
    6. Repeat until id <= 0.


    There's your input algorithm. It might be a little harder to implement because you're using a different (wrong) approach, but you can do it. Just break it down into baby steps. Depending on what kind of statistics you need we may be missing something mathematically, but you can change the algorithm in the thinking stage, before you write anything new, to accommodate that.

    Also if I had a dollar for every excuse you made I could buy my next novel at no cost.
    Last edited by whiteflags; 11-01-2010 at 10:57 PM.

  10. #10
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    Code:
    I will post the code with-in a week. Take a look at the
    attachment for the record.  I take names like col, coll and
    student and change it to col, COL, sales_person.  It works
    for me.  I catch on quicker this way.  You should find them
    to be an exact match, other than names.
    
    Yes I am a aggressive.  It's a form of sneaking in the word
    "P L E A S E" while never using the word.
    
    The project is over and I send in what I posted above,
    50% empty.  So I'll be more than happy to due battle to
    keep my future border-line {B}.   But I be darn if I don't
    follow your tutor/outline and complete this project for 
    me and this forum.
    
    Thank you and everyone
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21