Help I'm New :)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Help I'm New :)

  1. #1
    Join Date
    Dec 2008
    Posts
    11

    Wink Help I'm New :)

    Hi i'm trying to learn some basic classes and i'm having trouble with my program, if someone could tell me where i have gone wrong i would be very appreciative.

    Many thanks.

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    class televisionprog
    {
          public:
                  char *programname;
                  char *channel;
                  char *timedate;
                  char programguide[];
          
                 televisionprog(char *programname, char *channel, char *timedate)
                 {
                                     programname = televisionprog::programname;
                                     channel = televisionprog::channel;
                                     timedate = televisionprog::timedate;
                 };
                 ~televisionprog(){};
                 void programguide()
                 {
                      strcat(televisionprog::programguide, *programname);
                      strcat(televisionprog::programguide, ", ");
                      strcat(televisionprog::programduide, *channel);
                      strcat(televisionprog::programguide, ", ");
                      strcat(televisionprog::programguide, *timedate);
                 }
                 void printprogguide()
                 {
                        cout<< televisionprog::programguide;
                 }
    } prog1, prog2, prog3, prog4;
    
    int main()
    {
        char progname[50];
        char channel[25];
        char timedate[25];
        
        cout<<"Please enter the program name: "<<endl;
        getline(cin, prog1.progname);
        cout<<"Please enter the program channel: "<<endl;
        getline(cin, prog1.channel);
        cout<<"Please enter the program time and date: "<<endl;
        getline(cin, prog1.channel);
        prog1.televisionprog(prog1.progname, prog1.channel, prog1.timedate);
        prog1.printprogguide()<<endl;
        cin.get();
        return 0;
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Help I'm New :)

    Well, you didn't tell us what the problem was. However, right off I notice that you're trying to use the version of getline() overloaded for std::string with a char array. You probably want cin.getline(progname,<max length>);.

    Also, you're attempted to read into char pointers that aren't directed at any valid memory. Always bad!

    Then there's the useless (probably backwards?) assignments in the televisionprog constructor....

    Your program would be much *closer* to being correct if you ripped out all the char*s and replaced them with std::strings. There would still be a few issues, but far fewer.

  3. #3
    Join Date
    Dec 2008
    Posts
    11

    Re: Help I'm New :)

    Here is the debugger output:
    Attached Images Attached Images  

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Help I'm New :)

    First rule of compiler errors: Don't talk about compiler errors.

    Second rule of compiler errors: Most compiler errors are caused by other compiler errors. Fix the first one, and then see how many go away.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,098

    Re: Help I'm New :)

    That's compiler output, not debugger.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Help I'm New :)

    Quote Originally Posted by Joshp93 View Post
    Hi i'm trying to learn some basic classes and i'm having trouble with my program, if someone could tell me where i have gone wrong i would be very appreciative.
    Are you and the person who started this thread the same person?

    http://www.codeguru.com/forum/showthread.php?t=466914

    The reason why I ask is that you're both using the same, bad style (using 'C' char routines instead of std::string). Even this is the same:
    Code:
                       programname = televisionprog::programname;
                       channel = televisionprog::channel;
                       timedate = televisionprog::timedate;
    No C++ programmer or book that I know of codes this way, which is why I'm asking if you're the same person. If not the same person, what book are you reading that teaches you this (or worse, what teacher is teaching you this?). Whatever it is, dump the book and/or teacher.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Help I'm New :)

    Take a look at these two:
    Code:
    char programguide[];
    Code:
    void programguide()
    {
    }
    Do you see what the problem is there?

    BTW, don't do this again!
    Code:
    class televisionprog
    {
    } prog1, prog2, prog3, prog4;
    Don't declare variables with the declaration of the type. In any real application classes are declared in headers. That would trigger multiple definition error by the linker, when you include the header in multiple cpp files.

    You should do this instead.
    Code:
    class televisionprog
    {
    };
    
    televisionprogprog1, prog2, prog3, prog4
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center