What can i do to my code?

This is a discussion on What can i do to my code? within the C++ Programming forums, part of the General Programming Boards category; Well I've spent a few sleepless nights (VERY RECENTLY -.o) I wanted to get people's opinion at my first semi-serious ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    9

    What can i do to my code?

    Well I've spent a few sleepless nights (VERY RECENTLY -.o) I wanted to get people's opinion at my first semi-serious program ever. (First thing i made after doing a few tutorials and a lot of mountain dew.

    Code:
    //This is the Customer.h file....
    #include <iostream>      /*        This is another way to make comments      */
    #include <iomanip>       /*    These are the headers we need, so remember to */
    #include <fstream>       /*  include them all in the calss. You will learn   */
    #include <sstream>       /*  past where I have gotten with these tutorials   */
    #include <string>        
    
    
    using namespace std; 
    
    class Customer //Class for the C++ Tutorial Register
    {
       private:    //Notice these are colons!
       public:
              Customer();
              ~Customer(); 
              char Customer::GetData(int id);
              int Customer::GetLines(char *file);
                
    }search;
    
    
    Customer::Customer() //Constructor for the C++ Class
    {         
    };
    Customer::~Customer() //destructor for the C++ Tutorial 
    { 
    }; 
    
    char Customer::GetData(int id) //This is a function to get and read whatever is
    {                              //in the corollating ID#.txt file. 
    ostringstream File_ID;
    File_ID<<id<<".txt";
    string line;
    ifstream Cust_File (File_ID.str().c_str());
      if (Cust_File.is_open())
      {
        while (! Cust_File.eof() )
        {
          getline (Cust_File,line);
          cout << line << endl;
        }
        Cust_File.close();
      }
    
      else cout << "Unable to open file"<<File_ID; 
    cin.get();
    
    return 0;
      
    };
    int Customer::GetLines(char * file) //This will get the number of items in
    {                                   //the "Items.txt" file which makes using
            int y = 0;                  //loops through the program easier.
            string line;
            
            ifstream productlist(file);
            if(productlist.is_open())
            while (! productlist.eof() )
                {
                  getline (productlist,line);
                  y++;
                }
                productlist.close();
    
            return y;
    };
    Thats the class with the two functions that I got working. Enough fiddeling gets anything working lol
    Code:
    ////////////////////////////////////////////////////////////////////////////////
    /////////This was created by Rum//////////////////////CRAZY////////
    /////////////////////////////////////2007, Januaru///////////////////////
    
    #include <iostream>      /*        This is another way to make comments      */
    #include <iomanip>       /*    These are the headers we need, so remember to */
    #include <fstream>       /*  use them all wisely. Hopefully you will learn   */
    #include <sstream>       /*  past where i have gotten with these tutorials   */
    #include <string>        /*    You are our programming legacy, feel honoured.*/
    #include <time.h>
    #include "Customer.h"
    using namespace std;               //Let's Start!
    //This will allow me to use my GetLines Function.
    
    
    int main()
    {
    
    
    string zipstr;                /* I like to categorize all my variables once I'm  */
    string string;                /*Done writing the code. So don't worry about your */
    /*Messy code when you first start writing the code */
    char dateStr [9];             /*All you should care about in the beginning is    */
    char timeStr [9];             /*Getting the program to work, worry about anything*/
    char zip[50];                 /*Else after that.                                 */
    char address[70];
    char name[50];
    char phone[20];
    char description[200];
    
    int x=0;              
    int correct = 2;
    int id=1000;
    int id2;
    int option;
    int Max_Items;
    
    double total_trnsctn = 0;
    double cash_tend=0.00;
    double change=0.00;
    double totaly=0.00;
    double x2=0;
    
    Customer search;
    
    Max_Items = search.GetLines("Items.txt."); //Get the number of lines in the Items.txt file. 
    
    struct Product{                     /*Now this is where we use a structure*/
    std::string name;
    double price;
    int quant;
    
    };
    struct Product item[90]={
    };                        //which are useful in keeping variables
    //logically stored and easily reachable
    
    fstream p_file("Items.txt");            //Declares p_file as a buffer to "Items.txt"
    x=0;
    while(x<Max_Items)//Gets the names of all the items in the "Items.txt" file.
    {
    
    getline(p_file,item[x].name,'\n');
    x++;
    }
    p_file.close();
    x=0;
    fstream i_file("Prices.txt");
    while(x<Max_Items)//Gets the prices of all the items in the "Prices.txt" file.
    {
    
    i_file>>item[x].price;
    x++;
    }
    x=0;
    i_file.close();
    
    
    
    //This is where the users opens up and will first see.
    
    
    
    for (int i=0;i<100;i++)
    {
    
    cout<<"\n";
    cout<<"_____________________________________________________\n";
    cout<<"| w               w                                 |\n";
    cout<<"|  w             w                                  |\n";
    cout<<"|   w     w     w                                   |\n";
    cout<<"|    w   w w   w    eee L   cccc  o o  m     m eee  |\n";
    cout<<"|     w w   w w     ee  L  c     o   o m m m m ee   |\n";
    cout<<"|      w     w      eee LLL cccc  o o  m  m  m eee  |\n";
    cout<<"|                                                   |\n";
    cout<<"|___________________________________________________|\n";
    cout<<"_____________________________________________________\n";
    cout<<"| ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! |\n";
    cout<<"|! ! ! ! ! ! ! ! ! Adam's Super Store! ! ! ! ! ! ! !|\n";
    cout<<"| ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! |\n";
    cout<<"|! !Providing the Southeastern Region of Vermont ! !|\n";
    cout<<"| ! ! ! ! ! !with all of its computer needs ! ! ! ! |\n";
    cout<<"|! ! ! ! ! ! ! !for 18 wonderful years ! ! ! ! ! ! !|\n";
    cout<<"| ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! |\n";
    cout<<"|!_!_!_!_!_!_!_!_!_!_!_!_!_!_!_!_!_!_!_!_!_!_!_!_!_!|\n";
    cout<<"-----------------------------------------------------\n";
    cout<<"\n";
    cout<<"Please chose an option:\n";
    cout<<"(1)New Customer\n(2)Previos Customer\n(3)Exit\n";
    cin>>option;
    system("cls");
    //doesn't compound.
    
    id++;
    
    switch(option)
    {
    case 1:
    while(correct==2)           //This loop will get the custome's info.                     
    {            //Adds one to the int id.
    
    cin.ignore();
    cout<<"Please enter name:\n";
    cin.getline(name, 50, '\n');
    
    
    cout<<"Please enter address:\n";
    cin.getline(address, 70, '\n');
    
    
    cout<<"Please enter zip code:\n";
    getline(cin, zipstr);
    
    
    cout<<"Please enter phone number:\n";
    cin.getline(phone, 20, '\n');
    
    cout<<"Please enter description:\n";
    cin.getline(description, 200, '\n');
    system("cls");
    
    cout<<endl<<"Customer ID # "<<id<<endl;
    cout<<"Name: "<<name<<endl;
    cout<<"Address: "<<address<<endl;
    cout<<"Zip Code: "<<zipstr<<endl;
    cout<<"Phone: "<<phone<<endl;
    cout<<"Description: "<<description<<endl;
    
    cout<<"\nIs this information correct?\n(1)Yes\n(2)No\n";
    cin>>correct;
    system("cls");
    }
    break;	
    
    case 2:
    correct=2;
    while(correct==2)
    {
    cout<<"Please enter the Customer's ID#:      "<<endl;
    cin>>id2;
    system("cls");
    cout<<"\n\n\n\n";
    cout<<"_____________________________________________________\n";
    cout<<"| w               w                                 |\n";
    cout<<"|  w             w                                  |\n";
    cout<<"|   w     w     w                                   |\n";
    cout<<"|    w   w w   w    eee L   cccc  o o  m     m eee  |\n";
    cout<<"|     w w   w w     ee  L  c     o   o m m m m ee   |\n";
    cout<<"|      w     w      eee LLL cccc  o o  m  m  m eee  |\n";
    cout<<"|                                                   |\n";
    cout<<"|___________________________________________________|\n";
    cout<<"\n\n\n\n";
    search.Customer::GetData(id2);
    cout<<"\nIs this information correct?\n(1)Yes\n(2)No\n";
    cin>>correct;
    
    }
    
    break;
    
    case 3:
    return 0;
    break;
    
    default:
    cout<<"Your entered an Invalid choice\n";
    break;
    }
    correct=2;
    while(correct!=1)
    {
    
    while(x<Max_Items)
    {
    
    cout<<"How many "<<item[x].name<<" were purchased :\n\n";
    cin>>item[x].quant;
    x++;
    }
    system("cls");
    x=0;
    while(x<Max_Items)
    {
    cout<<"Customer Prushased "<<item[x].quant<<" "<<item[x].name<<" at $"<<item[x].price<<" each"<<endl<<endl;
    x++;
    }
    x=0;
    cout<<"\n\nIs this Correct?\n(1)Yes\n(2)No\n\n";
    cin>>correct;
    system("cls");
    }
    while(x<Max_Items)//Fills the structure's object's total price.
    {
    total_trnsctn = total_trnsctn + (item[x].quant * item[x].price) ;
    x++;
    }
    x = 0;
    
    cout<<"\nTotal Cost: "<<total_trnsctn<<endl;
    cout<<"Cash Tended: \n";
    cin>>cash_tend;
    change=cash_tend-total_trnsctn;
    cout<<"Change: "<<change<<endl<<endl;
    
    _strdate( dateStr);
    cout<<"The current date is "<<dateStr<<endl;
    _strtime( timeStr );
    cout<<"The current time is "<<timeStr<<endl;
    if(id2!= 0)
    {
    id=id2;
    }
    ostringstream ID;                  //By redeclaring my ostringstream each loop
    ID<<id<<".txt";                    //it will act like flushing it. So the string
    if(id2 != 0)
    {
    ofstream a_file(ID.str().c_str());
    
    a_file<<"Customer ID#     : "<<id<<endl;
    a_file.flush();
    a_file<<"Customer Name    : "<<name<<endl;
    a_file.flush();
    a_file<<"Customer Address : "<<address<<endl;
    a_file.flush();
    a_file<<"Customer Zipcode : "<<zip<<endl;
    a_file.flush();
    a_file<<"Customer Phone   : "<<phone<<endl;
    a_file.flush();
    a_file<<"Customer Desc.   : "<<description<<endl<<endl;
    a_file.flush();
    
    a_file<<"Customer Orders : "<<endl<<endl;
    a_file.flush();
    while(x<Max_Items)
    {
    a_file<<"Customer Purchased : "<<item[x].quant<<" "<<item[x].name<<" at $"<<item[x].price<<" each\n";
    a_file.flush();
    x++;
    }
    x=0;
    a_file<<"Total: "<<total_trnsctn<<endl;
    a_file.flush();
    a_file<<"Cash Tended: "<<cash_tend<<endl;
    a_file.flush();
    a_file<<"Change: "<<change<<endl<<endl<<endl;
    a_file.flush();
    a_file<<"Transaction took place on "<<dateStr<<endl;
    a_file.flush();
    a_file<<"at "<<timeStr<<endl<<endl;
    a_file.flush();
    a_file.close();
    }    
    else
    {
    ofstream a_file( ID.str().c_str(), ios::app);
    
    a_file<<"Customer Orders : "<<endl<<endl;
    a_file.flush();
    while(x<Max_Items)
    {
    a_file<<"Customer Purchased : "<<item[x].quant<<" "<<item[x].name<<" at $"<<item[x].price<<" each\n";
    a_file.flush();
    x++;
    }
    x=0;
    a_file<<"Total: "<<total_trnsctn<<endl;
    a_file.flush();
    a_file<<"Cash Tended: "<<cash_tend<<endl;
    a_file.flush();
    a_file<<"Change: "<<change<<endl<<endl<<endl;
    a_file.flush();
    a_file<<"Transaction took place on "<<dateStr<<endl;
    a_file.flush();
    a_file<<"at "<<timeStr<<endl<<endl<<endl;
    a_file.flush();
    a_file.close();
    }
    correct=2;
    
    ifstream c_file("CustomerDataBase.txt");
    
    }
    
    
    return 0;
    
    }
    Shhhhhh It is a SECRETIVE program not really, i am just beyond happy that i got it working. Offer some critisism on what i could do to make it better!

  2. #2
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    For starter, please rearrange your tabs and whitespaces. My eyes hurt when I look at your code.

    Also, add some comments to help us (and the future you in case you want to redevelop it again) to understand what is what withou having to trace the program.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    You do not need all the 'includes' in the .cpp file after you have aready written them in the header file. Also, like above, indent your code.
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    endl automatically flushes the stream, no need to call flush. Also, it's not necessary to flush after every line in a long block.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    //This is the Customer.h file....
    #include <iostream>      /*        This is another way to make comments      */
    #include <iomanip>       /*    These are the headers we need, so remember to */
    #include <fstream>       /*  include them all in the calss. You will learn   */
    #include <sstream>       /*  past where I have gotten with these tutorials   */
    #include <string>        
    
    
    using namespace std; 
    By including this in your header file, you're forcing every other potential file that uses this header into also opening up the std namespace whether the user wants it or not. Such declarations should generally not be put into headers... you've also got another redundant line in the source file that takes care of this in any case.

    Code:
    class Customer //Class for the C++ Tutorial Register
    {
       private:    //Notice these are colons!
       public:
              Customer();
              ~Customer(); 
              char Customer::GetData(int id);
              int Customer::GetLines(char *file);
                
    }search;  // <- Get rid of this!


    Code:
    while (! Cust_File.eof() )
    {
        getline (Cust_File,line);
        cout << line << endl;
    }
    Cust_File.close();
    The usual way to loop through a file in such a manner is to place the getline call within the while loop conditional. The way you have things now would lead to problems in displaying the last line of the file twice. You also do not need to call the close member function for the file stream, there is no harm leaving it there, but the destructor will close the file itself at the end of the function. So, this is what I'd recommend:

    Code:
    while ( getline(Cust_File,line) )
    {
        cout << line << endl;
    }

    Code:
    Customer::Customer() //Constructor for the C++ Class
    {         
    };
    
    Customer::~Customer() //destructor for the C++ Tutorial 
    { 
    }; 
    
    char Customer::GetData(int id) //This is a function to get and read whatever is
    {                              //in the corollating ID#.txt file. 
        ...
    };
    
    int Customer::GetLines(char * file) //This will get the number of items in
    {                                   //the "Items.txt" file which makes using
        ...
    };
    You don't need all those semicolons.


    Code:
    struct Product{                     /*Now this is where we use a structure*/
    std::string name;
    double price;
    int quant;
    
    };
    struct Product item[90]={
    };
    Just combine those (the std:: in front of the string variable isn't necessary since you've already included the using namespace std clause at the top of the source file):
    Code:
    struct Product {                     /*Now this is where we use a structure*/
        string name;
        double price;
        int quant;
    } item[90];

    In general, you might want to think about moving the source for the classes member functions into a separate source file. You'd then have two source files and a header file. Each of the source files will include the header, and your project settings would need to compile the two source files and link them together.

    Didn't look any further.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21