Can someone explain to me this code?

This is a discussion on Can someone explain to me this code? within the C++ Programming forums, part of the General Programming Boards category; Hi everyone. I ran across this code researching the Monty Hall problem. I tried compiling it in Code-Blocks and got ...

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

    Can someone explain to me this code?

    Hi everyone.
    I ran across this code researching the Monty Hall problem. I tried compiling it in Code-Blocks and got several errors. Two of the errors were from the last two include statements. The other I am having a problem understanding is the "assign" statement. Can anyone explain it to me? I appreciate it.
    Code:
    #include <stdio.h>
    #include <cstdlib>
    #include <time.h>
    #include "after.h"
    #include "open.h"
    
    using namespace std;
    string s1, s2, s3, s4, s5, str, s1a, s2a, s3a, s4a, s5a, str2;
    
    int reveal_non_chosen_loser(const int & winner);
    int decide_non_chosen_loser(const int & winner, const int & chosen);
    int higher(const int & a, const int & b);
    int lower(const int & a, const int & b);
    
    int wins, times; double percentage_won; char jazz = 'y';
    bool first = true;
    char x[1000]; char y[1000];
    
    int game()
    {
        int door = rand() % 3 + 1;
        ++times;
        //printf("Door = %d", door); printf("\n");
        int choice;
        if (first) { first = false; printf("Hello\nAnd Welcome\n"); }
        printf("Please choose a door: 1, 2 or 3.\nIf you choose correctly you will win.\n");
        scanf("%d", &choice);
        printf("You choose DOOR #%d %s", choice, "\n");
        int non_chosen_loser = decide_non_chosen_loser(door, choice);
        decide_non_chosen_loser(door, choice);
        printf("Lets have a look at whats behind door #%d", non_chosen_loser); printf(" shall we? \n");//, non_chosen_loser);
        int sw = rand() % 5; switch(sw){case 1: str = s1; str2 = s1a; break; case 2: str = s2; str2 = s2a; break; case 3: str = s3; str2 = s3a; break; case 4: str = s4; str2 = s4a; break; case 5: str = s5; str2 = s5a; break;    default: break; }
        char tch [1000];
        assign (str, tch);
        printf("%s", tch);
        printf("\nDo you want to switch to door #%d, or do you feel lucky today? \n", decide_non_chosen_loser(non_chosen_loser, choice));
        printf("Y/N\n");
        char jas;
        scanf("%d", &jas);
        if (jas == 'y' || jas == 'Y')
            choice = decide_non_chosen_loser(choice, non_chosen_loser);
        if (choice == door)
            {
                ++wins;
                printf("You win!!!!!!!!!\n");
            }
        else {  printf("!!!!You lose.\n");
                int itch = (sw + (rand() % 4) ) % 5 + 1;
                switch(itch) { case 1: str = s1; str2 = s1a; break; case 2: str = s2; str2 = s2a; break; case 3: str = s3; str2 = s3a; break; case 4: str = s4; str2 = s4a; break; case 5: str = s5; str2 = s5a; break;    default: break; }
                assign (str, x); assign (str2, y);
                printf("But don't worry you've still won a ");
                printf("\n%s%s", x, y );
                printf("     ");
                printf("\n");
        }
        return false;
    }
    
    int main()
    {
        open window;
        srand(time(NULL));
        s1 = "A pile of cow dung. "; s2 = "A goat."; s3 = "A blank sheet of paper.  "; s4 = "Absolutely Nothing.  "; s5 = "A box.  ";
        s1a = "Ya know, manure makes excellent fertilizer. \n";
        s2a = "They are anything but pickey eaters and can be kept as a pet, if you want. \n";
        s3a = "You can write on it, or use it as spare firewood if you need to. \n";
        s4a = "Better luck next time. \n";
        s5a = "Maybe there's something in it. You never know. \n";
        bool again = false;
        do
        {
            game();
            printf("Do you want to play again? Y/N ");
            printf("\n");
            scanf("%c", &jazz);
            scanf("%c", &jazz);
            scanf("%c", &jazz);
            if (jazz == 'Y' || jazz == 'y')
                again = true;
            else again = false;
        }while (again);
        percentage_won = (double)wins / (double)times;
        printf("wins = %d", wins); printf(" times = %d", times);
        printf("\nYou winning percentage = %d", percentage_won);
    
    
        return 0;
    }
    
    int decide_non_chosen_loser(const int & winner, const int & chosen)
    {
        int loser, temp = rand() % 2;
        if (winner != chosen)
            loser = (3 - (higher(winner, chosen) + lower(winner, chosen) ) % 3);
        else loser = ( ( (winner + temp) % 3) + 1);
        return loser;
    }
    
    int higher(const int &a, const int & b)
    {
        if (a > b)
            return a;
        else return b;
    }
    
    int lower(const int & a, const int & b)
    {
        if (a > b)
            return b;
        else return a;
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,532
    Unfortunately, you need to have the two missing header files as part of your project.

    The assign statement looks wrong to me. string::assign should look like this

    str.assign(tch);

    or one of the overloads. That just copies tch as the value of str. The call in your code could always be something else from one of the headers though. It's hard to say for sure.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Well you could go through your code and put in a newline after every ;

    Rather than having stupidly long lines that cause scroll bars in browsers.
    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.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    What I would like to ask is why there is printf, scanf and char in a C++ program?
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    175
    I cannot tell you why, I did NOT write this code. I am trying to understand a few things about it that is all. I am aware of the lack cout and cinn statements. I started on C++, my problem was the 'assign' statement. I googled the hell out of it and the only thing that came close is either fortran or verilog. My only thought is the writer had it in the last two include statements that are not with the program.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    Well, as long as you understand that it's bad, then it's fine.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    175
    Quote Originally Posted by Elysia View Post
    Well, as long as you understand that it's bad, then it's fine.
    Have you ever ran across that 'assign' statement? It looks like the writer is trying to assign a variable that has been assigned a string to an array. What would be a better way if you do not mind sharing with me?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    If the author had bothered to use C++ facilities in the first place, the conversion to an array would not be necessary.
    However, barring that, std::string has a member function c_str() which will return a pointer to a C-style string which you can pass to C functions.
    If you need to modify the string, then it's better to do

    std::vector<char> vec(mystr.begin(), mystr.end());
    vec.push_back('\0');

    Then to get a modifiable array, you simply do

    char* array = &vec[0];
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    175
    Quote Originally Posted by Elysia View Post
    If the author had bothered to use C++ facilities in the first place, the conversion to an array would not be necessary.
    However, barring that, std::string has a member function c_str() which will return a pointer to a C-style string which you can pass to C functions.
    If you need to modify the string, then it's better to do

    std::vector<char> vec(mystr.begin(), mystr.end());
    vec.push_back('\0');

    Then to get a modifiable array, you simply do

    char* array = &vec[0];
    Sweet! Thanks! Much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please explain me this C code fragment
    By stefboombastic in forum C Programming
    Replies: 6
    Last Post: 12-16-2010, 12:29 PM
  2. Help please explain what code does
    By cybersasho in forum C Programming
    Replies: 3
    Last Post: 10-08-2010, 05:27 AM
  3. Can someone explain a line of code for me please?
    By drkidd22 in forum C Programming
    Replies: 8
    Last Post: 12-09-2009, 09:36 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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