Help Me Understand..


Recommended Posts

I was told the followng code has a lot of logical error in it... I know of some spelling errors, adn some messy ways of doing things like

cout << Poll[x-1] + 99; insted of cout << Poll[x] + 98;

but I don't feel that is a logical error, as its logical , just not well written

I'm posting thuis to get other openions..

//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 © 2005 by William S. Huskey                               *" << endl;
cout <<  setw(74) << "*   [email protected] 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 © 2005 by William S. Huskey                               *" << endl;
cout <<  setw(74) << "*   [email protected]                                                   *" << 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;

 
}

}

Edited by iccaros
Link to post
Share on other sites

There are a couple bugs.

If SalesDump() is called before any records have been stored to the data files, the input operation

salesfile >> record_name >> sales >>  commision >> ws;

will fail, leaving the variables in their default state. record_name is uninitialized, so you end up with a garbage record

 Eü·    aÿ·         04.85728e-270

. (The string may be different on your machine, but if you examine the program under a debugger it will correspond to the initial value of record_name.) The problem is that the EOF flag isn't set until an EOF is read from the stream, so the input loop will run at least once.

The same bug occurs in PrintToScreen(). The first iteration of the loop always runs and increments Poll[0], producing a phantom record. In addition, PrintToScreen() re-reads the entire set of records on every call but modifies the same data structure, so every call produces more phantom records.

Link to post
Share on other sites
the same bug occurs in PrintToScreen(). The first iteration of the loop always runs and increments Poll[0], producing a phantom record. In addition, PrintToScreen() re-reads the entire set of records on every call but modifies the same data structure, so every call produces more phantom records.

ya I noticed that.. after I posted.. and tested..

I have lots to learn.. (for a second I thought I was getting somewhere)

"If SalesDump() is called before any records have been stored to the data files, the input operation"

the only way I know to fix that is to force information into the file first...

I am still working on this.. as I see this as a great way to do the Adventure game talked about before..

I'll have some more question.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...