My first program

This is a discussion on My first program within the C++ Programming forums, part of the General Programming Boards category; It's a simple program. You first type your favorite movie, then your favorite game, if your favorite game and movie ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    56

    My first program

    It's a simple program. You first type your favorite movie, then your favorite game,
    if your favorite game and movie matches with the programmer's favorites,
    you then see a message "Wow! It seems we have so many things in common.".

    It works fine, but I'd like to know if i coded this correctly, or if there's anything
    that should be done to make the code cleaner.

    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
    
        char fvrtMovie[100];
        char fvrtGame[100];
    
        int points = 0;
    
        cout<<"Type your favorite movie: ";
    
        cin.getline(fvrtMovie,100);
    
        cout<<"\n\n";
    
    
        if(strcmp(fvrtMovie,"Godfather") == 0 || strcmp(fvrtMovie,"The Godfather") == 0)
        {
        cout<<"Really? That's my favorite movie, too!";
        points++;
        }
        else
        {
        cout<<"Hmmm, I see..." ;
        }
    
        cin.get();
    
    
        cout<<"\n\nNow, what's your favorite game? ";
    
        cin.getline(fvrtGame,100);
    
        if(strcmp(fvrtGame,"Street Fighter") == 0)
        {
        cout<<"\n\nThat's your favorite game, too?";
        points++;
        }
        else
        {
        cout<<"\n\nOh, "<<fvrtGame<<". I see...";
        }
    
        if(points >= 2)
        {
            cin.get();
            cout<<"Wow! It seems we have so many things in common.";
        }
    
        cin.get();
    
        return 0;
    
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Use the available C++ string class, not messy C-style char arrays.

    #include <string>

    and say
    string fvrtMovie;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    They do the same thing. They store a string. Use OO as a tool.

    This debate isn't worth discussing. Forums continue to prove it's just people babbling back and forth and following the leader. Good luck with your programs and keep it up.
    Last edited by dxfoo; 08-01-2010 at 10:25 AM.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    WTF is this nonsense!?!
    You realize that what you're saying is complete nuts and totally wrong, right?

    Quote Originally Posted by dxfoo View Post
    They do the same thing. If you're not requiring OO features for strings in your particular program, ignore these silly suggestions.
    OO is the bread and butter of a C++ programmer. Telling someone to ignore it is silly.

    Tip: In fact, using std::string is actually bad because you can't add your own methods to it, nor can you improve what's already in there.
    Oh, this one is right. Can you add functionality to C-style strings? Can you improve what's already there? strcpy? strcmp? We're in the same boat then, aren't we?

    You'll also be using c-string functions outside of std::string.
    Well yes, of course. Not everything is contained inside std::string. Most algorithms in the standard library are free functions that work with strings.
    And let me tell you this: even with parsing, I have had little or no need for C functions. Yes, that's right. C functions are not required.
    In fact, by not relying on C-style strings, I (and the standard library) have been able to create algorithms that work on all containers that support the proper concept. Not just strings. That's called generic programming.

    You'll end up writing your own string class sooner or later.
    Nonsense. I have had no need to write a string class of my own so far.

    So LEARN how to work with c-strings like you're doing now because you'll get better at working with dynamic arrays and such for your own string class later.
    Nonsense. We have tools to handle such things for us. And even if you would write a string class later, it doesn't excuse the use of C-style strings. Always use a string class. If not your own, then std::string.

    People that teach the most abstract way first are probably the worst programmers out there because a) you won't learn anything, and b) you won't want to go back.
    Complete and utter and ridiculous nonsense!
    Go back and learn C. Leave C++ programmers alone. Let them create safe and maintainable code while you mess around with low-level stuff.
    There is more to just learning how things work in hardware. You also have to learn logic, to apply algorithms, and to actually accomplish what you want to do.
    What you are suggesting undermines the whole C++ language. Abstraction is key! And there are lots and lots and lots of good tools already out there. So use them!
    If and if you need to roll your own should you do so. But that is not a priority! The priority is writing good programs that accomplishes the task given quickly and efficiently. If you're going to hand-roll everything, you can kiss that goodbye.

    You should remember how effective it is when a teacher teaches the hard way first and then the easy way, and these good teachers don't hang out on forums often with 22k posts (they got salaries).
    What are you, dumb? What are you are saying is completely the opposite approach of things. Usually the teachers show the easy way first and gradually you dive down into the hard way. Do you think there's a reason they do it that way? Or are they just dumb forum members with 22k posts?
    And yes, btw, there are a couple of programmers here who has actual jobs, yet hang out on the forum with a massive post count. Because they choose to spend their free time here on the forum helping people with advice instead of belittling others.

    So learn c-strings, get good at it, and then do whatever you want.
    The opposite. Avoid them like the pest they are. Forget they exist.
    Use them only later if you have to. Right now, you don't.
    Get good at utilizing the tools at your hand instead of trying to learn nonsense which you won't use 99% of the time.

    You'll be using arrays the rest of your life one way or the other.
    But arrays != C-style strings.
    'Nuff said.

    Now, if there's nothing more, perhaps you should go back and stay in your C corner.
    Last edited by Elysia; 08-01-2010 at 01:16 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,198
    Hahahahaha dxfoo strikes again with another completely utter piece of crap ignorant post... Opposed to what dxfoo says i would take my advices from Elysia and Salem any day, what i would not do is take advice from dxfoo, I mean with statements such as

    "The first step in becoming a game programmer is take out the 'game' and throw in the 'graphics,' since that often is what you do."

    The man clearly knows what he is talking about...
    Last edited by Shakti; 08-01-2010 at 03:33 AM.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by dxfoo View Post
    People that teach the most abstract way first are probably the worst programmers out there
    Here's a link to the lecture slides used by Bjarne Stroustrup (you know, the inventor of C++), at the Texas A&M University.

    I think this proves that you really are a clueless individual and should hold absolutely no credence on this board.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by dxfoo View Post
    People that teach the most abstract way first are probably the worst programmers out there
    Here's a link to the lecture slides used by Bjarne Stroustrup (you know, the inventor of C++), at the Texas A&M University. Srtoustrup Lectures

    Whilst teaching c-style strings, and low level programming, he does so after first introducing much more useful abstract methods.

    I think this proves that you really are a clueless individual and should hold absolutely no credence on this board.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,450
    Quote Originally Posted by dxfoo
    If you're not requiring OO features for strings in your particular program, then ignore these silly suggestions. (...) Using std::string is actually bad because you can't add your own methods to it, nor can you improve what's already in there.
    You are apparently rather unaware of the idea of extending an interface by writing non-member non-friend functions, which makes your "credentials" rather suspect
    Last edited by laserlight; 08-01-2010 at 07:43 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I can't believe you spent so much time on that guy, Elysia. I think he must be right: you must have too much time, and so you can't have a job ;-).

    Let me just say this:
    You'll end up writing your own string class sooner or later.
    Yes. If you are too stupid to understand how to properly use an std::string you will. Otherwise you won't. Ever.
    Tell me one reason anyone would ever need to write their own string class? I bet I can tell you a better way of doing it.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,450
    Quote Originally Posted by EVOEx
    Tell me one reason anyone would ever need to write their own string class?
    To practice writing one's own string class, of course
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Quote Originally Posted by EVOEx View Post
    I can't believe you spent so much time on that guy, Elysia. I think he must be right: you must have too much time, and so you can't have a job ;-).
    Of course not. I'm a student. And it's summer, so I do have a lot of free time. For now.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    2,300
    Quote Originally Posted by Shakti View Post
    Opposed to what dxfoo says i would take my advices from Elysia and Salem any day
    I agree. I've been hanging out on this board for around 3 years now, and have always found their advice to be spot-on. sometimes it's a bit hard to swallow, because it requires so much change to the way I've personally been doing things, but in the end, it usually leads to better performing, easier to maintain, and clearer code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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