Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

My program - made of fail [C++]

Name: Anonymous 2007-01-18 11:01

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

class Sales
{
      public:
             void setSales( int d, double t );
             double calcCommission( double );
             int getNum() { return deptNum; }
             double getTotal() { return dayTotal; }
      private:
              int deptNum;
              double dayTotal;
};

              string deptnumtoname ( int dept );
             
              void Sales::setSales( int d, double t )
              {   
                   deptNum = d;
                   dayTotal = t; 
              }
             
              double Sales::calcCommission( double total )
              {
                     double percent;
                     if ( total <= 500 )
                     { 
                          percent = 0.05;
                     }
                     else if ( total > 500 && total <= 2000 )
                     {
                            percent = 0.2;
                     }
                     else if ( total > 2000 && total <= 4000 )
                     {
                            percent = 0.25;
                     }
                     else if ( total > 4000 )
                     {
                            percent = 0.3;
                     }
                     return ( total * percent );
              }
             
               void writetofile( int line, double comm )
              {
                 Sales salesdata;
                 string deptname;
                 int deptnumber = salesdata.getNum();
                 double totalsales = salesdata.getTotal();
                
                 deptname = deptnumtoname( deptnumber );
                
                 ofstream outfile;
                 outfile.open ("com_sales.txt");
                 if ( outfile.fail() )
                 {
                      cout << "error opening file" << endl;
                      outfile.close();
                      exit (1);
                 }
                 else {
                       outfile << line << deptname << totalsales << comm << "\n";
                 }
              }
             
              string deptnumtoname ( int dept )
              {
                 string deptname;
                
                 switch (dept){
                 case 1:
                 deptname = "Toys";
                 break;
                 case 2:
                 deptname = "Homeware";
                 break;
                 case 3:
                 deptname = "Electricals";
                 break;
                 case 4:
                 deptname = "AudioEquipment";
                 break;
                 case 5:
                 deptname = "Music";
                 break;
                 case 6:
                 deptname = "DIY";
                 break;
                 case 7:
                 deptname = "LadiesClothing";
                 break;
                 case 8:
                 deptname = "MensClothing";
                 break;
                 case 9:
                 deptname = "ChildrensClothing";
                 break;
                 case 10:
                 deptname = "Seasonal";
                 break;
                 default:
                 deptname = "Null";
                 }
                 return deptname;
              }
                  
              void highlow ( int selection, int& deptnumber, double& daytotal )
              {
                   Sales salesdata;
                  
                   int dept = salesdata.getNum();
                   double total = salesdata.getTotal();
                   double hightotal = 0;
                   int highdept;
                   double lowtotal = 500000;
                   int lowdept;
                  
                   if ( total > hightotal )
                   {
                        hightotal = total;
                        highdept = dept;
                   }
                  
                   else if ( total < lowtotal )
                   {
                          lowtotal = total;
                          lowdept = dept;
                   }
                  
                   if ( selection == 1 )
                   {
                        deptnumber = highdept;
                        daytotal = highdept;
                   }
                  
                      if ( selection == 2 )
                   {
                        deptnumber = lowdept;
                        daytotal = lowdept;
                   }
              }
                         
              void highlowoutput( int selection, int deptnum, double total )
              {
                   string deptname;
                  
                   if ( selection == 1 )
                   {
                        deptname = deptnumtoname( deptnum );
                        cout << "The department with the lowest sales is: " << deptname << ", its sales were: " << total;
                   }
                  
                   else if ( selection == 2 )
                   {
                        deptname = deptnumtoname( deptnum );
                        cout << "The department with the highest sales is: " << deptname << ", its sales were: " << total;
                   } 
              }
                            
              int main()
              {
                   Sales salesdata;
                   int dept, count = 1, selection, deptnum2;
                   double total, commission, total2;
                   int * deptpointer;
                   double * totalpointer;
                  
                   deptpointer = &deptnum2;
                   totalpointer = &total2;
                  
                   cout << "Would you like to see the highest sales deparment or the lowest : enter 1 for highest, 2 for lowest: ";
                   cin >> selection;
                  
                   ifstream infile;
                   infile.open("ica_sales.txt");
                   if ( infile.fail() )
                        {
                        cout << "error opening file" << endl;
                        infile.close();
                        exit (1);
                        }
                   else {
                         while ( ! infile.eof() )
                         {
                         infile >> dept;
                         infile >> total;
                        
                         salesdata.setSales( dept, total );
                         commission = salesdata.calcCommission( total );
                         highlow( selection, *deptpointer, *totalpointer );
                         writetofile ( count, commission );
                         count ++;
                         }
                        
                         highlowoutput(selection, deptnum2, total2);
               }
               }

If anyones utterly bored enough to read over this program and find out why im getting garbage output you shall receive many internets <3

its compiles with no errors just gives rubbish output HALP 4chan !

Name: Anonymous 2007-01-18 11:52 (sage)

>>1
What the fuck, that's C++--, GTFO my /prog/.

Name: Anonymous 2007-01-18 12:32 (sage)

It's returning garbage because you are creating a new "Sales salesdata" variable at the beginning of each function.  For example, at the beginning of writetofile():


Sales salesdata;
string deptname;
int deptnumber = salesdata.getNum();
double totalsales = salesdata.getTotal();


You have no default construction for the Sales class, so before you set the two variables inside of it, they will be uninitialized and could be equal to anything, which is what you are getting.

Also, why are you using pointers just to pass something by reference for highlow() ?

Name: Anonymous 2007-01-18 16:20

/* Comments:
    - you suck at C++.
    - "Sales" is a stupid object name.
    - WHY THE FUCK DO YOU PUT Sales::calcCommission INSIDE A CLASS
        WHEN IT DOES NOT NEED THIS FUCKING CLASS OR ANY OF ITS
        FUCKING MEMBERS???
    - writetofile( int line???? LINE??? You're a moron.
*/

double commission(double total)
{
    double percent;
   
    if (total <= 500)
        percent = 0.05;
    else if (total > 500 && total <= 2000)
        percent = 0.2;
    else if (total > 2000 && total <= 4000)
        percent = 0.25;
    else
        percent = 0.3;
   
    return total * percent;
}

#include <iostream>
using std::ostream;

#include <vector>
using std::vector;

class Department {
public:
    friend ostream& operator<<(ostream&, const Department&);
   
    enum Type {
        Toys, Homeware, Electricals, AudioEquipment,
        Music, DIY, LadiesClothing, MensClothing,
        ChildrensClothing, Seasonal, Undefined
    };
   
    Department(): m_type(Type::Undefined) {}

private:
    vector<double> m_sales;
    Type m_type;
};

friend ostream& operator<<(ostream& os, const Department& department)
{
    switch (department.m_type) {
        case Department::Type::Toys: os << "Toys"; break;
        // ... do the rest yourself
    }
   
    return os;
}

/* I'm fucking bored, do the rest yourself or explain your
problem in fucking English, not your shameful C++ */

Name: Anonymous 2007-01-19 5:20

Use C

Name: Anonymous 2007-01-20 18:06

what is needed is they python for the reason being a well made piece of coded object.

Name: Anonymous 2007-01-20 20:31

>>6
whut

Name: Anonymous 2007-01-26 3:43

>>6
LOL python turned you into a retard

Name: Anonymous 2007-10-04 13:32

............................................________
....................................,.-‘”...................``~.,
.............................,.-”...................................“-.,
.........................,/...............................................”:,
.....................,?......................................................\,
.................../...........................................................,}
................./......................................................,:`^`..}
.............../...................................................,:”........./
..............?.....__.........................................:`.........../
............./__.(.....“~-,_..............................,:`........../
.........../(_....”~,_........“~,_....................,:`........_/
..........{.._$;_......”=,_.......“-,_.......,.-~-,},.~”;/....}
...........((.....*~_.......”=-._......“;,,./`..../”............../
...,,,___.\`~,......“~.,....................`.....}............../
............(....`=-,,.......`........................(......;_,,-”
............/.`~,......`-...............................\....../\
.............\`~.*-,.....................................|,./.....\,__
,,_..........}.>-._\...................................|..............`=~-,
.....`=~-,_\_......`\,.................................\
...................`=~-,,.\,...............................\
................................`:,,...........................`\..............__
.....................................`=-,...................,%`>--==``
........................................_\..........._,-%.......`\
...................................,<`.._|_,-&``................`\

Name: Anonymous 2009-01-14 13:12

LISP

Name: Sgt.Kabu嵂催kiman萸싀 2012-05-28 21:55

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List