c++ help sought/troubleshooter needed

Sharky Forums


Results 1 to 3 of 3

Thread: c++ help sought/troubleshooter needed

  1. #1
    Tiger Shark unclescrooge's Avatar
    Join Date
    Mar 2002
    Location
    Somewhere South of North and North of South
    Posts
    594

    c++ help sought/troubleshooter needed

    Hi,
    I cannot figure out why this program that is supposed to print the name, number, atbats, hits, and generate a batting average is printing the data as name, atbats, number, hits, and then the generated batting avg instead.

    any help appreciated.

    thanks
    scrooge


    #include <iostream>

    #include <iomanip>

    #include <string>

    using std::cin;

    using std::cout;

    using std::endl;

    using std::left;

    using std::setiosflags;

    using std::setw;

    using std::ios;

    using namespace std;

    //function prototypes

    void displayHead();

    void displayTopOfForm(string []);

    void displayDataArray(string [][2], int [][2]);

    int main()

    {

    //declare variables

    int player[10][2] = {0};

    int battAvg[10] = {0};

    string playerName[10][2] = {{"Adam", "1"}, {"Ben", "2"}, {"Carl", "3"}, {"Don", "4"}, {"Eric", "5"}, {"Frank", "6"}, {"Glenn", "7"},

    {"Han", "8"}, {"Ian", "9"}, {"Jon", "10"}};

    string header[5] = {"Player", "Number", "AtBats", "Hits", "BattAVG"};


    //calculate batting average




    //call display functions

    displayHead();

    cout << endl;


    displayTopOfForm(header);

    cout << endl << endl;


    displayDataArray(playerName, player);


    return 0;

    } //end of main function

    //*********************called functions**************************

    //begin displayHead function

    void displayHead()

    {

    cout << setw(52) << "Batting Average Program" << endl;

    } //end displayHead function

    //begin displayTopOfHeader function

    void displayTopOfForm(string topForm[])

    {

    for (int i=0; i<5; i++)

    {

    cout << setiosflags(ios::right) << setw(14) << topForm[i];

    }

    } //end displayTopOfForm function

    //displayArray function

    void displayDataArray(string name[][2], int stats[][2])

    {

    for (int i=0; i<10; i++)

    {

    for (int j=0; j<2; j++)

    {

    cout << setiosflags(ios::left) << setw(14) << name[i][j]<< setw(14);

    cout << stats[i][j] << setw(14);

    }

    cout << endl;

    } //end for

    } //end displayArray function
    "If you come to a fork in the road, take it"
    Yogi Berra.
    http://www.jchampion.com

  2. #2
    Hammerhead Shark
    Join Date
    Jun 2003
    Posts
    1,019
    should use the code tags... I'm tired but I'll look at this tomorrow morning
    Code:
    #include <iostream>
    
    #include <iomanip>
    
    #include <string>
    
    using std::cin;
    
    using std::cout;
    
    using std::endl;
    
    using std::left;
    
    using std::setiosflags;
    
    using std::setw;
    
    using std::ios;
    
    using namespace std;
    
    //function prototypes
    
    void displayHead();
    
    void displayTopOfForm(string []);
    
    void displayDataArray(string [][2], int [][2]);
    
    int main()
    
    {
    
    //declare variables
    
    int player[10][2] = {0};
    
    int battAvg[10] = {0};
    
    string playerName[10][2] = {{"Adam", "1"}, {"Ben", "2"}, {"Carl", "3"}, {"Don", "4"}, {"Eric", "5"}, {"Frank", "6"}, {"Glenn", "7"},
    
    {"Han", "8"}, {"Ian", "9"}, {"Jon", "10"}};
    
    string header[5] = {"Player", "Number", "AtBats", "Hits", "BattAVG"};
    
    
    //calculate batting average
    
    
    
    
    //call display functions
    
    displayHead();
    
    cout << endl;
    
    
    displayTopOfForm(header);
    
    cout << endl << endl;
    
    
    displayDataArray(playerName, player);
    
    
    return 0;
    
    } //end of main function
    
    //*********************called functions**************************
    
    //begin displayHead function
    
    void displayHead()
    
    {
    
    cout << setw(52) << "Batting Average Program" << endl;
    
    } //end displayHead function
    
    //begin displayTopOfHeader function
    
    void displayTopOfForm(string topForm[])
    
    {
    
    for (int i=0; i<5; i++)
    
    {
    
    cout << setiosflags(ios::right) << setw(14) << topForm[i];
    
    }
    
    } //end displayTopOfForm function
    
    //displayArray function
    
    void displayDataArray(string name[][2], int stats[][2])
    
    {
    
    for (int i=0; i<10; i++)
    
    {
    
    for (int j=0; j<2; j++)
    
    {
    
    cout << setiosflags(ios::left) << setw(14) << name[i][j]<< setw(14);
    
    cout << stats[i][j] << setw(14);
    
    }
    
    cout << endl;
    
    } //end for
    
    } //end displayArray function
    __________________

  3. #3
    Hammerhead Shark
    Join Date
    Jun 2003
    Posts
    1,019
    sry for delay
    you are passing in the playerName and player arrays to your display function. The player array is all zeros.
    In your display function it outputs the first column in the playerName function (player name), then the first column of the player array function (0). Then it does the second column (player number, 0). Finally it goes to the next row and repeats. If you want to change the order do this:
    Code:
    	for (int i=0; i<10; i++)
    	{
    	    cout << setiosflags(ios::left) << setw(14) << name[i][0] << setw(14)  << name[i][1] 
                                   << setw(14)  << stats[i][0] << setw(14) << stats[i][1] << setw(14);
    	    cout << endl;
            } //end for
    also the 2d arrays are unnecessary, you can just do 5 parallel arrays or even better create a class or struct and make a single array from that object
    Last edited by eon; 02-25-2010 at 02:11 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •