Thread: I need help with my vertical bar graph program.

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    I need help with my vertical bar graph program.

    I know it's simple but I am missing some element. The graphs don;t come out in a column. Please if someone could point me in the right direction. You need a text file to test it.

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

    int main()
    {
    const int MAX=20;
    ifstream fin;
    string company; // name of company file
    char graph[MAX][70]; // columns of blocks
    float stock[MAX]; // stock price value
    float max=0;
    float scale; // scale of columns
    float space;
    int count;
    int i,j; // increments
    char block=219;

    //get filename
    cout << "Enter the name of the company file: ";
    cin >> company;
    //open file
    fin.open(company.c_str());
    //error checking
    while (!fin)
    {
    cout << "File does not exist.";
    return 0;
    }
    //read stock value into 1D array
    for (count=0; fin && count<MAX; count++)
    {
    fin>>stock[count];
    }
    max=stock[0];
    for (i=1; i<count; i++)
    if (stock[i]>max)
    max=stock[i];
    scale=20/max;
    space=80/count;
    //array[row][col]

    //give each array value

    for (i=0; i < count; i++)
    {
    for (j=0; j< stock[i]*scale; j++)
    {
    graph[i][j]=block;

    }
    }
    //print columns
    for (j=0; j < count; j++)
    {
    for (i=0; i < stock[j]*scale; i++)
    cout << setw(space) << graph[j][i];
    cout << endl;
    }


    return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
    const int MAX=20;
    ifstream fin;
    string company; // name of company file
    char graph[MAX][70]; // columns of blocks
    float stock[MAX]; // stock price value
    float max=0;
    float scale; // scale of columns
    float space;
    int count;
    int i,j; // increments
    char block=219; 
    
    //get filename
    cout << "Enter the name of the company file: ";
    cin >> company;
    //open file
    fin.open(company.c_str());
    //error checking
    if (!fin)
    {
       cout << "File does not exist.";
       return 0;
    }
    //read stock value into 1D array
    for (count=0; (fin >> stock[count]) && count<MAX; count++)
    {
    }
    max=stock[0];
    for (i=1; i<count; i++)
       if (stock[i]>max)
          max=stock[i];
    scale=20/max;
    space=80/count;
    //array[row][col]
    
    for (i=0; i < count; i++)
       for (j=0; j<70; j++)
          graph[i][j] = ' ';
    
    //give each array value
    
    for (i=0; i < count; i++)
    {
       for (j=0; j< stock[i]*scale; j++)
       {
          graph[i][j]=block; 
    
       }
    }
    
    cout << endl;
    for (j=69; j>=0; j--)
    {
       for (i=0; i < count; i++)
          cout << setw(space) << graph[i][j];
       cout << endl;
    }
    
    return 0;
    }
    If you want to print horizontally instead, substitute this for the print:
    Code:
    cout << endl;
    for (i=0; i < count; i++)
    {
       for (j=0; j<70; j++)
          cout << graph[i][j];
       cout << endl;
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    11
    thanks. that was driving me nuts.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  4. program won't create status bar
    By Unregistered in forum Windows Programming
    Replies: 7
    Last Post: 08-30-2001, 04:00 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM