View Single Post
Old 05-19-2005, 09:04 PM   #1 (permalink)
iccaros
Registered User
 
Join Date: Apr 2005
Posts: 24
iccaros is on a distinguished road
questions on arrays

here is my first question on this board..

I have been learning c++ for 6 weeks now so I'll take all the advice I can get. so most of this code is experimenting with diffrent things.. like the license output is jsut so I caould play with diffrent ways of doing it. not that this crappy code needs copywrite protection .

I have a program that uses an array to collect Poll data. and I have two output methods
one looking like this

Code:
200 - 299              1
300 - 399              0
400 - 499              6
500 - 599              1
600 - 699              2
700 - 799              0

and one that looks like this..

Code:
200 - 299              *
300 - 399              
400 - 499              ******
500 - 599              *
600 - 699              **
700 - 799

my problem is I need to clear the array between displays or it just re-adds the data.

not I am populating a file with the initial data, and the file is read and calculations are done to the file.


here is the whole program to help.
warrnig its long, and I have not cleared out unused variables yet.

Code:
//csi 245 C++ 
//William S. Huskey
//A way overboard version of assinment 

//header files
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>

//declare name space
using namespace std;

//Function Prototype

//  needs to pass to compiler options , for Gcc its -DUNIX or -DWIN32 VC++ ?? no clue
void clear_screen();


//information functions
int PrompUser (const string&);


//output functions
void PrintLicense ();
void SalesInfo ();
void StarBar();
void GraphOut(const int[], const int[], const int);
void PrintToScreen( int[], const int[], const int);
void WelcomeScreen();
void SalesDump();


//global variables
const char* file_name = "salesinfo.txt"; //file where sales persons information will be stored
const char* LicenseFile = "license.txt"; //GNU GPL target


//main funtion
int main ()
{
    //local variables
    int UserInput = 0, holder = 0, MenuInput = 0;
    const int array_size = 9, poll_size = 10;
    bool Loop = true;
    //arrays
    int salaryRange [array_size] = {200,300,400,500,600,700,800,900,1000};
    int Polldata [poll_size] = {0};
    
    //create file salesinfo.txt
    ofstream newfile (::file_name);
    if (newfile.is_open())
     {
                             newfile <<  " ";
                             newfile.close();
     }                       
     
     
     //menu
     while (Loop == true) 
     {
     clear_screen();
     //header for menu
     WelcomeScreen();
     
     //menu chooices
     clear_screen();
StarBar();
cout <<  setw(74) << "*   Copyright (C) 2005 by William S. Huskey                               *" << endl;
cout <<  setw(74) << "*   www@iccaros-linux.org under the GPL                                   *" << endl;
cout <<  setw(74) << "*                                                                         *" << endl;
cout <<  setw(74) << "*   Employee Sales Tracking Program                                       *" << endl;
cout <<  setw(74) << "*   Please Choose From an Option Below                                    *" << endl;
cout <<  setw(74) << "*                                                                         *" << endl;
cout <<  setw(74) << "*                                                                         *" << endl;
cout <<  setw(74) << "*                                                                         *" << endl;
cout <<  setw(74) << "*   (1) Enter Sales's Persons information                                 *" << endl;
cout <<  setw(74) << "*                                                                         *" << endl;
cout <<  setw(74) << "*   (2) Show chart of Salary Ranges                                       *" << endl;
cout <<  setw(74) << "*                                                                         *" << endl;
cout <<  setw(74) << "*   (3) Show Graph of Salary range (note run this after option 2)         *" << endl;
cout <<  setw(74) << "*                                                                         *" << endl;
cout <<  setw(74) << "*   (4) List Sales People and Salary                                      *" << endl;
cout <<  setw(74) << "*                                                                         *" << endl;
cout <<  setw(74) << "*   (5) Print License                                                     *" << endl;
cout <<  setw(74) << "*                                                                         *" << endl;
cout <<  setw(74) << "*   (6) Exit Program                                                      *" << endl;
StarBar();
     
     
          cout <<": ";
     MenuInput = PrompUser ("<Choice> ");
     cin.ignore();
          //switch and case MenuInput
     switch (MenuInput) 
     {
            case 1:
                 SalesInfo();
                 break;
            case 2:
                 PrintToScreen(Polldata, salaryRange, array_size);
                 break;
            case 3:
                 GraphOut(Polldata, salaryRange, array_size);
                 break;
            case 4:
                 SalesDump();
                 break;
            case 5:
                 PrintLicense();
                 break;
            case 6:
                 Loop = false;
                 cout << setw(48) << "Good-Bye !!" << endl;
                 break;
            default:
                    cout << setw(48) << "You did not enter a valid responce (1-6)";
                    break;
                    }
                    
                    }
return 0;
}

//Functions

//Clear screen function
void clear_screen()
{
              #if defined WIN32
              system("cls");
              #elif defined UNIX
              system("clear");
              #endif
}

//SalesInfo will ask for Sales person information and store it in the salesinfo.txt file for retrivel latter. this file could be made beforehand
void SalesInfo()
{
     int Sales = 0;
     double commision = 0;
     char record_name[32];
     ofstream salesfile (::file_name, ios::app);
     
     if (salesfile.is_open())
     {
                             cout << "Enter Sales Persons Lastname first Initial \nexample William Huskey would be: Huskeyw \n:";
                             cin >> record_name;
                             Sales = PrompUser ( "Enter the amount of sales for this week: ");
                             
			     commision = Sales * .09;
                             salesfile << record_name << " " << Sales <<" " << commision <<" \n";
                             salesfile.close();
     }                       
}


//PrintToScreen will print a Poll information showing how many sales people salaries per range there were
void PrintToScreen( int Poll[] , const int Range[], const int array_size )
{
     int holder = 0, salary = 200, sales = 0, a = 0, b = 0 , c = 0, j = 9, WeekSalary = 0;
     double commision;
     char record_name[32];
     
          
     ifstream salesfile;
     
     salesfile.open(::file_name);
     if (! salesfile)
     {
           cout << "error opening file: " << ::file_name << endl;
           cout << "please use option (1) to create file" << endl;
           }
      
      while ( ! salesfile.eof())
      {
            salesfile >> record_name >> sales >>  commision >> ws;
            
            WeekSalary = salary + static_cast <int> (commision);
            
            if (WeekSalary >= 1000)
            holder = 9;
            
            else 
            holder =  ((WeekSalary / 100) -1);
            
            ++Poll[holder];
            
               }
               salesfile.close();
               //output 
               //clear_screen();
               StarBar();
	       for (int x = 1; x <= array_size -1; x++)
               {
               b = Range[x -1] + 99;
	       
               cout << setw(10) << Range[x-1] << " - " <<  b  << setw(10) << " " << setw(10) << Poll[x] <<endl;
               }
	        
	       cout << setw(10) << Range[j-1] << " - " << "above" << setw(8) << " " << setw(10) << Poll[j] << endl;  
	       
	       StarBar();
	       cout << "Press Enter to exit to menu";
               getchar (); // wait for input
  
               }


//GraphOut is the same as PrintToScreen, but insted of numbers it places stars next to each range      
void GraphOut(const int Poll[], const int Range [], const int array_size)
{
    int b, c;
    StarBar();
      for ( int x = 1 ; x <= array_size; x++)
      {
        b = Range[x-1] + 99;
        cout << setw(20) << Range[x-1] << " - " << b << setw(10) << " ";
	
	if ( Poll[x] >= 1) {
	c = Poll[x];
	for ( int y = 1 ; y <= c; y++)
	cout << "*";
	}
	
	cout << endl;
	}
	StarBar();

	       cout << "Press Enter to exit to menu";
               getchar (); // wait for input
         }


//SalesDump will list all sales people and the salary they have earned
void SalesDump()
{
     int holder = 0, salary = 200, sales = 0, a = 0, b = 0 , c = 0, WeekSalary = 0;
     double commision;
     char record_name[32];
     
          
     ifstream salesfile;
     
     salesfile.open(::file_name);
     if (! salesfile)
     {
           cout << "error opening file: " << ::file_name << endl;
           cout << "please use option (1) to create file" << endl;
           }
      StarBar();      
      while ( ! salesfile.eof())
      {
            salesfile >> record_name >> sales >>  commision >> ws;
            
	   std::cout << setw(10) << record_name << setw(10) << sales << setw(10) << commision << endl;
                     
              }
              
	       salesfile.close();
               //output 
               //clear_screen();
              
	       StarBar();
	       cout << "Press Enter to exit to menu";
               getchar (); // wait for input
  
               }
	             

//PrintLicense will output the GNU GPL license covering this program
void PrintLicense()
{
clear_screen();
StarBar();
cout <<  setw(74) << "*   Copyright (C) 2005 by William S. Huskey                               *" << endl;
cout <<  setw(74) << "*   www@iccaros-linux.org                                                   *" << endl;
cout <<  setw(74) << "*                                                                         *" << endl;
cout <<  setw(74) << "*   This program is free software; you can redistribute it and/or modify  *" << endl;
cout <<  setw(74) << "*   it under the terms of the GNU General Public License as published by  *" << endl;
cout <<  setw(74) << "*   the Free Software Foundation; either version 2 of the License, or     *" << endl;
cout <<  setw(74) << "*   (at your option) any later version.                                   *" << endl;
cout <<  setw(74) << "*                                                                         *" << endl;
cout <<  setw(74) << "*   This program is distributed in the hope that it will be useful,       *" << endl;
cout <<  setw(74) << "*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *" << endl;
cout <<  setw(74) << "*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *" << endl;
cout <<  setw(74) << "*   GNU General Public License for more details.                          *" << endl;
cout <<  setw(74) << "*                                                                         *" << endl;
cout <<  setw(74) << "*   You should have received a copy of the GNU General Public License     *" << endl;
cout <<  setw(74) << "*   along with this program; if not, write to the                         *" << endl;
cout <<  setw(74) << "*   Free Software Foundation, Inc.,                                       *" << endl;
cout <<  setw(74) << "*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *" << endl;
StarBar();

//anykey exit
cout << "Press any key to exit to menu" << endl;
getchar (); // wait for input

              }
              
//StarBar is a quick bar output function 
void StarBar()
{
      for (int x = 0; x <= 74; x++)
      cout << "*";
      cout << endl;
         }
//output welcome text
void WelcomeScreen()
{
StarBar();
cout.width(50);
cout  << " CIS 245 C++ Assinment 5" << endl;
cout.width(50);
cout << " William Huskey may 2005" << endl;
 
 }

//PrompUser is a Error checking for number inputs.. This is to stop someone from entering the wrong type of numbers
int PrompUser (const string& question)
{
 while (true) {
          int user_input;
          cout << question << flush;
          cin >> user_input;
          if (cin.fail()) {
                          cerr << "invalid input \n";
                          cin.clear();
                          cin.ignore();
                          }
          else
            return user_input;

   
}

}
iccaros is offline   Reply With Quote