Thread: Whats wrong with this program?

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    156

    Whats wrong with this program?

    Someone told me that my programming methods are "weird" and not proper. I want some C++ veterans to look at my code and give me some pointers(not *).

    Code:
    #include <iostream>
    #include <fstream.h>
    #include <conio.c>
    #include <string>
    #include <windows.h>
    
    class streaming
    {
     public:
     void UserInfo();
     void Fout();
     void Fread();
     streaming();
     ~streaming();
    
     protected:
     ofstream x;
     ifstream y;
     char filename[20];
     string line;
     string word;
     string name;
     unsigned short int age;
     unsigned char response[20];
    }io;
    
    streaming::streaming()
    {
     line="";
     word="";
    }
    
    streaming::~streaming()
    {
    }
    
    void streaming::UserInfo()
    {
     cerr << "Input your character's name: ";
      cin >> name;
     cerr << "\nInput your character's age: ";
      cin >> age;
     io.Fout();
    }
    
    void streaming::Fout()
    {
     cerr << "\nInput the name of the file we will print out to: ";
      cin >> filename;
     char *file;
     file=new char[20];
     file=&filename[20];
     x.open(file);
     x << "Name: " << name << endl;
     x << "Age: " << age << endl;
     x.close();
     delete [] file;
     file=NULL;
    }
    
    void streaming::Fread()
    {
     char *file;
     file=new char[20];
     file=&filename[20];
     cerr << "\nHow would you like us to read the file, line by line or word by word?";
      cin >> response;
     if (response[0]=='l' || response[0]=='L' || (response[0]=='l' && response[1]=='i' && response[2]=='n' && response[3]=='e') || (response[0]=='L' && response[1]=='i' && response[2]=='n' && response[3]=='e') ) {
      y.open(file);
      while (y.good() ) {
      std::getline(y, line);
      if (!y.eof()) {
      cerr << line;
      y.clear(); }
      else {
      y.close(); }
      }
      y.close();
     }
    
     else if (response[0]=='w' || response[0]=='W' || (response[0]=='w' && response[1]=='o' && response[2]=='r' && response[3]=='d') || (response[0]=='W' && response[1]=='o' && response[2]=='r' && response[3]=='d') ) {
      y.open(file);
       while (y >> word) {
        if (!y.eof()) {
        cerr << word << " ";
        y.clear(); }
        else {
        y.close(); }
        }
      y.close();
     }
    
     else {
     cerr << "\nCouldn't understand your input, bailing out of here!!" << endl;
     cin.get(); }
    
     delete [] file;
     file=NULL;
    }
    
    int main()
    {
     streaming *x;
     x=new streaming;
     x->UserInfo();
     Sleep(1000);
     cerr << endl;
     x->Fread();
     delete x;
     x=NULL;
    
     getch();
     return 0;
    }
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: [email protected]

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    >Someone told me that my programming methods are "weird" and not proper

    Dont listen to that person....its all about style....

    >cerr

    this is one thing i am wondering about....why are you using cerr? cerr is generally used for displaying error messages, why not use cout?
    My Website

    "Circular logic is good because it is."

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    My reason for using cerr may shock you.....as you say its style................I view cerr and cout as the same thing(I know its not), I'm just trying to program a little different from people. As long as the function display text on screen, theres no problem using it.
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: [email protected]

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    317
    Very interesting style, however you realize that if you where to try to direct the program stream to a file anything diplayed with cerr will still go to the screen and not the file, correct?

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    Treveller is absolutely right
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: [email protected]

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I really don't see a problem with your style, you have good habits and some bad ones but overall it seems okay.

    >file=&filename[20];
    This isn't good. filename is defined as a char array with an index of 0-19 and you are passing the 20th element of filename to the 0th element of file. You can't be sure what is at filename[20] and it certainly isn't memory that you own, so this is bad.

    >file=NULL;
    I love you. This is a habit that I love seeing and everyone should adopt for safety reasons. If anyone tells you to stop NULLing out your pointers after freeing memory, don't listen to them.

    I do have a bit of a problem with your class methods though. Generally, it's considered better to not request and accept input inside the method, instead you should ask for and take input outside of the class workings and then pass that data as parameters to the method. This way you can maintain a distance from the class and it's data and the rest of the program. But there's nothing wrong with the way you have it, so it's up to you.

    >I'm just trying to program a little different from people.
    Being different for the sake of being different is a bad idea. If you have a different style from everyone else then you'd better have very good reasons for backing up your choices when asked for an explanation. For example, when asked I can detail exactly how my coding style works and why I do what I do in incredible detail. I can do this not only because I am asked to explain on occasion, but also because I put a lot of thought into my style so as to make it as easy to follow as possible while still being correct and aesthetically pleasing. Usually, different means difficult to follow, which is bad.

    -Prelude
    My best code is written with the delete key.

  7. #7
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Prelude... are you GOD???
    What is C++?

  8. #8
    Registered User Kupo's Avatar
    Join Date
    Dec 2001
    Posts
    36
    she is.

    get down and recognise!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  2. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  4. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 09:57 AM
  5. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 05:09 PM