Like Tree1Likes

A little program I wrote for fun !

This is a discussion on A little program I wrote for fun ! within the C++ Programming forums, part of the General Programming Boards category; Hey (...) I know it could be considered somewhat arrogant to do this, but I'm just asking for some helpful ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    A little program I wrote for fun !

    Hey (...)
    I know it could be considered somewhat arrogant to do this, but I'm just asking for some helpful 'reviews' of the following short program.
    The program takes a yes/no Question and gives a random answer to it, remaining grammatically correct.
    It works as expected, but there could be 'bad' code within it...that's the reason.


    The functions used are:
    std::string auxiliary_p(std::string);

    std::string find_pronoun(std::string);
    bool answer(std::string);
    std::string modify(bool,std::string,std::string);
    bool quit_condition();
    Code:
    #include<iostream> 
    #include "functions.h"
    using namespace std;
    
    int main()
     {
            string input,pronoun,auxiliary,output;
            bool yes_or_no;
            do
            {
                cout<<"Input: ";
                getline(cin,input);
    
                auxiliary=auxiliary_p(input);  //Gets the auxiliary verb
    
                pronoun=find_pronoun(input);  // Gets the pronoun
    
                if(auxiliary=="NULL"||pronoun=="NULL")
                {
                    cout<<"Try Again ."<<endl;
                    continue;
                }
                yes_or_no = answer(input);
    
                output=modify(yes_or_no,pronoun,auxiliary);  //Attaches n't and/or puts yes/no 
    
                cout<<"Output: "<<output<<endl;
            } while(quit_condition());
    
    
            return 0;
     }
    The implementations(attached the file because it is about 80 lines) :
    functions.cpp
    Last edited by manasij7479; 07-21-2011 at 06:23 AM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,689
    Dude what the hell? That looks like crap. Actually format the thing.

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by whiteflags View Post
    Dude what the hell? That looks like crap. Actually format the thing.
    Ok, did so...
    (If you meant, formatting the attached file, then sorry, I don't think I can do better..!)
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,583
    I would throw exceptions than return "NULL".
    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
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So you've got some slashes in your attached file. Why?

    (For instance:
    Code:
    static list<string> pronouns=\
    {"I","you","we","they","he","she"};
    static list<string> auxs=\
    {"Can","Could","Will","Would",\
    "Shall","Should","Do","Does","Did",\
    It also doesn't compile.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,689
    Quote Originally Posted by Elysia View Post
    I would throw exceptions than return "NULL".
    Some sentences don't have pronouns.

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    So you've got some slashes in your attached file.
    To indicate that the next line is continues from the current one. I was told that it is a good practice, instead of leaving the compiler to figure it out.

    Also, it compiles in my case.,..[Code::Blocks in gcc-4.6 && Fedora 15 ] . Maybe ..you forgot to put the declarations into a functions.h ?


    Some sentences don't have pronouns
    O...forgot.... In that case, If a pronoun isn't found, I'd use the 2nd word in the question as the noun.
    Last edited by manasij7479; 07-21-2011 at 06:40 AM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by manasij7479 View Post
    To indicate that the next line is continues from the current one. I was told that it is a good practice, instead of leaving the compiler to figure it out.
    It's not. (It's required for macros, since macros must be "one line", and rather silly anywhere else.) You also don't need to put that on three lines, given that it fits on one line all by itself.
    Quote Originally Posted by manasij7479
    Also, it compiles in my case.,..[Code::Blocks in gcc-4.6 && Fedora 15 ] . Maybe ..you forgot to put the declarations into a functions.h ?
    I am using an old gcc, and I get errors that pronouns and auxs must be initialized by constructor, not by an initializer list, so that might just be old gcc. (Unfortunately it's all I've got on this machine.)

  9. #9
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    I am using an old gcc
    If it is >= 4.5 ...then adding -std=c++0x allows the initializer lists.


    I would throw exceptions than return "NULL".
    Is there a difference between putting the try block inside the .cpp file within the functions that might throw up and putting it around the code in main() ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,583
    Yes. The stack will unwind until a catch block is found.

    Quote Originally Posted by whiteflags View Post
    Some sentences don't have pronouns.
    True. But it seems that in this case, according to the code, if there isn't a pronoun, it shouldn't proceed. So a pronoun is required; thus if none is found, then it is an error.
    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.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,032
    I wouldn't use exceptions here. Exceptions are for exceptional cases, and that's not what you have here. Returning "NULL" is pretty weird though.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    2,388
    Quote Originally Posted by King Mir View Post
    I wouldn't use exceptions here. Exceptions are for exceptional cases, and that's not what you have here. Returning "NULL" is pretty weird though.
    only slightly off topic: what do you consider "exceptional?"

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,689
    Quote Originally Posted by manasij
    Is there a difference between putting the try block inside the .cpp file within the functions that might throw up and putting it around the code in main() ?
    You should put the try catch block wherever you want to catch the exception. The exception, when thrown, will unwind the function call stack until it finds a catch block. Of course, this means objects will be destroyed along the way: basically whatever is in the same scope as the catch block (but not to be confused with being inside the catch block) is what is still alive.

    But I wouldn't use exceptions since you indicated to me that finding a pronoun isn't a constraint.
    Quote Originally Posted by manasij7479 View Post
    O...forgot.... In that case, If a pronoun isn't found, I'd use the 2nd word in the question as the noun.

  14. #14
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    You should put the try catch block wherever you want to catch the exception. The exception, when thrown, will unwind the function call stack until it finds a catch block. Of course, this means objects will be destroyed along the way: basically whatever is in the same scope as the catch block (but not to be confused with being inside the catch block) is what is still alive.
    Did you mean the following? ...or to have the catches nested ?
    Code:
     
          try  //within main()
                {
                    auxiliary=auxiliary_p(input);   //The throws occour within this function.
                    pronoun=find_pronoun(input);
                }
    
    
                catch(string s)
                {
                    cout<<"Syntax Error !!!"<<endl;
                    cout<<s<<"."<<endl;
                    continue;
                }
    Last edited by manasij7479; 07-21-2011 at 03:43 PM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,689
    What you showed me looks fine. To explain what I mean again: Since auxiliary_p is where the exception occurs, it means you catch the exception as soon as auxiliary_p is popped off the call stack. Sometimes the exception is thrown deeper into the stack, but you still write the try catch where you want to catch the exception. (If you want to catch the exception and then throw it again, you can do that too in another try catch block.)

    You have to make sure that objects are properly destroyed so as to not leak memory. And figuring out what to do after the catch block is a problem too.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just wrote my first Curses program
    By guesst in forum Linux Programming
    Replies: 14
    Last Post: 04-02-2008, 10:44 AM
  2. filesplitting program I wrote
    By movl0x1 in forum C Programming
    Replies: 7
    Last Post: 05-30-2007, 03:24 PM
  3. Can someone look over the program I wrote?
    By brooklyn in forum C++ Programming
    Replies: 10
    Last Post: 04-16-2006, 07:23 AM
  4. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  5. wrote a program; please review/comment
    By spirited in forum C++ Programming
    Replies: 4
    Last Post: 05-22-2003, 01:37 PM

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