Look what i did!!

This is a discussion on Look what i did!! within the C++ Programming forums, part of the General Programming Boards category; I made a really simple program, but i did it without cheating! Code: #include <iostream> using namespace std; int main() ...

  1. #1
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91

    Talking Look what i did!!

    I made a really simple program, but i did it without cheating!
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() 
    {
      int nunb;  
      cout << "Number:";
      cin>> nunb;
      cin.ignore();
      if ( nunb = 20){
      cout << "You read my mind!";
      }
      else {
      cout << "Way off";
      }
      cin.get();
    }

  2. #2
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    This is actually starting to make a little sense

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    Actually, there is an error. In the if() statement, you need to change = to ==. In C++, = is an assignment operator therefore you will assign variable values with that operator. However, == is a comparative operator, it will tell you if both sides are equals.
    Code:
    int var = 0; // Assign var the value 0
    
    // check if var is equal to
    if(var == 0)
    {
        /* ... */
    }

  4. #4
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    it worked for me

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    Try inputting anything else than 20 and you will see.

  6. #6
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Let me guess you typed in 20 and it said you read my mind. Try typing in any number, I guarantee you it will still say that you read my mind.

    The way the original program is written the if statement is checking the return from the assignment which will always be true. Thus whether you type in 2 or 20 or 200 or 2000 you will always get 'you read my mind'.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    The only case where it will not is if you type = 0 (false == 0).

  8. #8
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    try it, you're wrong.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    I'm not.

  10. #10
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Oh, I get what you are saying. You mean if the assignment statement you set the value to 0. Well in that case you are right. My bad, I thought you meant typing in 0 for a guess.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    Sorry, I'm not a native englophone (capital 'e' ?) so I often make mistakes that change the meaning of my words.

  12. #12
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    How can I fix it?
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  13. #13
    Registered User
    Join Date
    Feb 2005
    Posts
    1
    By changing

    Code:
    if ( nunb = 20){
    to...

    Code:
    if ( nunb == 20){

  14. #14
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    o ok i didnt see that
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  15. #15
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    Wait it didn't work, i hit enter and it just adds a line
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() 
    {
      int nunb;  
      cout << "Number:";
      cin>> nunb;
      cin.ignore();
      if ( nunb == 20 ){
      cout << "You read my mind!";
      }
      if (false) {
      cout << "Way off";
      }
      cin.get();
    }
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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