Thread: How can I improve this program?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    How can I improve this program?

    include <iostream>

    using namespace std;

    int main()
    {
    cout<<"I just deleted some important files. Now, you will do as I say.\n";
    int decision;
    cout<<"Now, do as I say or those files will be forever lost. Press 1 for yes and 0 for no.\n";
    cin>>decision;
    if (decision == 1) {
    cout<<"You have taken the right decision, my friend.\n";}
    else if (decision > 1) {
    cout<<"Are you a dumbass?\n";}
    else if (decision == 0) {
    cout<<"You will regret this!\n";}
    for (; decision == 0; ) {
    cout<<"I will give you another chance.\n";
    cin>>decision;}
    for (; decision > 1 {
    cout<<"Press 1 for yes and 0 for no.\n";
    cin>>decision;}
    }






    ===

    I am trying to figure out how to make the program return to an earlier line to make this work properly just as I intended.

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    haha, one of my first programs was similar, a roulette game, could only bet on red or black of course but i gave the player hell when they lost them dollars, you need to use a while loop, keep count and output the message according to how many wrong answers, ie what the count is. beware, that way lies madness!
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    edit- to fit your example the while loop should be controlled while not equal to right answer, inside keep the count and control the strength of your replies with that, if they max out and just too stoopid to get right answer, exit loop anyway and deliver a choice parting message, then close program
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    IMPROVED CODE(C++)

    #include <iostream>

    using namespace std;

    int main()
    {
    cout<<"I just deleted some important files. Now, you will do as I say.\n";
    int decision;
    int x;
    cout<<"Now, do as I say or those files will be forever lost. Press 1 for yes and 0 for no.\n";
    for (x=1 ;x == 1; x++ ) {
    int a;
    cin>>decision;
    if (decision == 1) {
    cout<<"You have taken the right decision, my friend.\n";
    a = 2;}
    else if (decision > 1) {
    cout<<"Are you a dumbass?\n";
    a = 1;}
    else if (decision == 0) {
    cout<<"You will regret this!\n";
    a = 1;}
    if (a == 1) {
    cout<<"You have been given another chance.\n";
    x = x - 1;
    cin.get();}
    }
    }



    ====


    I'd like to count how many times the user has been given a second chance, how do I do this without rewriting everything?

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    This seems fitting.

    Are you a dumbass?
    << !! Posting Code? Read this First !! >>

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout<<"I just deleted some important files. Now, you will do as I say.\n";
        int decision;
        int x;
        cout<<"Now, do as I say or those files will be forever lost. Press 1 for yes and 0 for no.\n";
        for (x=1 ;x == 1; x++ ) {
        int a;
        cin>>decision;
        if (decision == 1) {
        cout<<"You have taken the right decision, my friend.\n";
        a = 2;}
        else if (decision > 1) {
        cout<<"Are you a dumbass?\n";
        a = 1;}
        else if (decision == 0) {
        cout<<"You will regret this!\n";
        a = 1;}
    if (a == 1) {
    cout<<"You have been given another chance.\n";
    x = x - 1;
    cin.get();}
    }
    }

    ====


    I'd like to count how many times the user has been given a second chance, how do I do this without rewriting everything?[/QUOTE]

  8. #8
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by WHOLEGRAIN View Post
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout<<"I just deleted some important files. Now, you will do as I say.\n";
        int decision;
        int x;
        cout<<"Now, do as I say or those files will be forever lost. Press 1 for yes and 0 for no.\n";
        for (x=1 ;x == 1; x++ ) {
        int a;
        cin>>decision;
        if (decision == 1) {
        cout<<"You have taken the right decision, my friend.\n";
        a = 2;}
        else if (decision > 1) {
        cout<<"Are you a dumbass?\n";
        a = 1;}
        else if (decision == 0) {
        cout<<"You will regret this!\n";
        a = 1;}
    if (a == 1) {
    cout<<"You have been given another chance.\n";
    x = x - 1;
    cin.get();}
    }
    }

    ====


    I'd like to count how many times the user has been given a second chance, how do I do this without rewriting everything?
    How about this:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout<<"I just deleted some important files. Now, you will do as I say.\n";
        int decision;
        int count = 0;
        cout<<"Now, do as I say or those files will be forever lost. Press 1 for yes and 0 for no.\n";
        cin>>decision;
        if (decision == 1) {
           cout<<"You have taken the right decision, my friend.\n"
                      "Your files have been spared.\n\n";
           cout<< "Welcome to the world of programming.\n"
                       "Now go learn C++ to write a better program than this.\n";
           return 0;
        }
    
        else if (decision > 1) {
          cout<<"Are you a dumbass?\n";
          cout<< "You have failed the test...\n"
                      "You have been doomed to suffer deletion of all files on your computer.\n"
                      "Now PREPARE to SUFFER!!!" <<endl;
          return 1;
       }
    
        else if (decision == 0)  {
          cout<<"You will regret this!\n";
          while (decision == 0) {
            if (count == 10) {
               cout<< "Your chances have run out....\n"
                           "Prepare to suffer the consequences." <<endl;
                return 1;
            }
    
            else if (count > 0 && count < 10){
                cout<< "Warning! You are running out of chances.\n"
                            "Don't make me change my mind." <<endl;
            }
    
            cout<< "You have been given another chance.\n"
                        "Press 1 for yes, and 0 for no." <<endl;
            cin>>decision;
            count++;
          }
    
          cout<< "Your files have been restored.\n"
                 "Congratulations on coming to your senses." <<endl;
      }
       
      return 0;
    }
    Last edited by Programmer_P; 03-04-2011 at 11:18 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Wow, it's like all of a sudden:

    THE GOGGLES, THEY DID SOMETHING!
    I can finally see C++ code
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    hur hur

    haha, that made me laugh but am not sure why?... maybe its the fizzy lifting juice
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Reminds me of virus of doom program. You could try to make the program go to infinite loop as the punishment for not listening to it. I freaked out the first time I accidentaly coded it. And I didn't know the command to end program. Fortunately the Internet was there.

    PS Infinite loop looks like this:
    Code:
    while (1)
    {
        Do something really nasty here;
    }
    This will run as long as 1 isn't equal to 0 (forever), or until you exit the program.
    Last edited by High Overlord; 03-05-2011 at 09:54 AM.

  12. #12
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by rags_to_riches View Post
    This seems fitting.

    "Are you a dumbass?"

    << !! Posting Code? Read this First !! >>
    Hahaha. Rude but funny

  13. #13
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    for a nasty while loop use

    Code:
    while(1)
    {
         system("PROGRAM_NAME.exe")
    }

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Hey wow, the Windows version of a fork bomb!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Mr T has run out of pity for this sorry thread.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a Battleship Program
    By HBlakeH in forum C Programming
    Replies: 1
    Last Post: 12-05-2010, 11:13 PM
  2. Homework Help, simple program, unsure newbie :(
    By Aslan14 in forum C Programming
    Replies: 13
    Last Post: 11-14-2010, 05:07 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM