C++ program # 6 help.......

Discussion in 'OT Technology' started by SuperMan91, Apr 26, 2008.

  1. SuperMan91

    SuperMan91 New Member

    Joined:
    Mar 2, 2008
    Messages:
    107
    Likes Received:
    0
    Location:
    Pa
    Theres no compiling errors but when i type in the correct information it takes me to the "You Login Has Failed".. Ne wayz heres the code...... { thats 3 friggin prgrams i messed up... :ugh: }




    // Designers network
    // Demonstrates logical operators

    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {
    cout << "\tGame Designers Network\n";
    int security = 0;

    string username;
    cout << "\nUser Name: ";
    cin >> username;

    string password;
    cout << "Password: ";
    cin >> password;

    if (username == "\nAccount1" && password == "Pass")
    {
    cout << "\nWelcome.";
    security = 5;
    }

    if (username == "\nAccount2" && password == "Pass")
    {
    cout << "\nHello there.";
    security = 5;
    }

    if (username == "\nAccount3" && password == "Pass")
    {
    cout << "\nHello there.";
    security = 5;
    }

    if (!security)

    cout << "\nYour Login Has Failed";



    system("pause");
    return 0;

    }
     
  2. Nocera

    Nocera ...

    Joined:
    Aug 9, 2000
    Messages:
    1,307
    Likes Received:
    0
    Location:
    Long Island, NY
    Remove the "\n" in the username checks.
     
  3. CodeX

    CodeX Guest

    Umm... I don't use the string type because I can use character arrays much more easily so I might be wrong but... does the comparison operator (==) even work on strings? I know it can be overloaded to work on anything you want but I don't think it works natively on string types. You should try using strcmp().

    And yes nocera is correct you likely don't want to be looking for the newline character in your user input... because using cin() you will not find it as it is used internally as a delimeter.
     
  4. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    It works. What good would a string class serve if it didn't make your life easier by handling some basic string manipulation like checking for equality?
     
  5. CodeX

    CodeX Guest

    if(!strcmp(string1, string2))

    if(string1 == string2)

    you're right... so much easier. (that was sarcasm, I fail to see a significant improvement)
     
  6. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY

    I fixed your program so it should work now.

    WOW please don't code anymore.. ;-)
     
  7. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    Learn to indent....code that's all mashed up on the left margin just reeks of unprofessionalism.
     
  8. CodeX

    CodeX Guest

    agreed

    Indent whenever there is a scope change
     
  9. SuperMan91

    SuperMan91 New Member

    Joined:
    Mar 2, 2008
    Messages:
    107
    Likes Received:
    0
    Location:
    Pa
    Im not gonna bother making ANOTHER thread about my problems, so yea..lol
    Program #14 got messed up........ It was gonna be like a lil 'game' for when i was bored but it got a compiling error... Ne ways, heres the source (again):




    // Word Jumble
    // The classic word jumble game where the player can ask for a hint

    #inlcude <iostream>
    #include <string>
    #include <cstdlib>
    #include <ctime>

    using namespace std;

    int main()
    {
    enum fields {WORD, HINT, NUM_FIELDS};
    const int NUM_WORDS = 3;
    const string WORDS[NUM_WORDS][NUM_FIELDS] =
    {
    {"wall", "Do you feel like you're banging your head against something?"},
    {"glasses", "These might help you see the answer."},
    {"labored", "Going slowly, is it?"}
    };

    srand(time(0));
    int choice = (rand() % NUM_WORDS);
    string theWord = WORDS[choice][WORD];
    string theHint = WORDS[choice][HINT];

    string jumble = theWord;
    int length = jumble.size();
    for (int i = 0; i < length; ++i)
    {
    int index1 = (rand() % length);
    int index2 = (rand() % length);
    char temp = jumble[index1];
    jumble[index1] = jumble[index2];
    jumble[index2] = temp;
    }

    cout <<"\t\t\tWelcome to Word Jumble!\n\n";
    cout <<"Unscramble the letters to make a word.\n";
    cout <<"Enter 'hint' for a hint.\n";
    cout <<"Enter 'quit' to quit the game.\n\n\n";
    cout <<"The jumble is: " << jumble;

    string guess;
    cout << "\n\nYour Guess: ";
    cin >> guess;

    while ((guess != theWord) && (guess !="quit"))
    {
    if (guess == "hint")
    cout << theHint;
    else
    cout << "Sorry, thats now the word.";
    cout <<"\n\nYour Guess: ";
    cin >> guess;
    }

    if (guess == theWord)
    cout <<"\nCongratulations! You guessed it!\n";

    cout <<"\nThanks for playing.\n";

    system("pause");

    return 0;
    }



    Ne help would be nice, Ne tips would be great.
     
  10. SuperMan91

    SuperMan91 New Member

    Joined:
    Mar 2, 2008
    Messages:
    107
    Likes Received:
    0
    Location:
    Pa

    I do indent on my programs...... It just doesn't copy right when i copy'n paste.
     
  11. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    use the code tags noob

    Just so you know, it makes it look like this
    Code:
    no indented
      ohh indented
        look ma, more indents
    back to noobsville
    
     
    Last edited: May 7, 2008
  12. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    You spelled include wrong in your iostream include directive. You've also got some implicit type conversions going on that you should either get rid of or explicitly cast.

    I would suggest not using \n everywhere in your statements. Instead, use the endl member of the cout class. Endl forces a stream flush, which, when you're working with long strings of text (which you're not in this particular program), can lead to debugging trouble down the road. Text written to a stream isn't displayed until the stream flushes, and if the program crashes before a flush occurs, you won't see all the text you actually wrote to the stream.
     
  13. SuperMan91

    SuperMan91 New Member

    Joined:
    Mar 2, 2008
    Messages:
    107
    Likes Received:
    0
    Location:
    Pa

    // Sarcasm
    Wow. Your really mature..
     
  14. SuperMan91

    SuperMan91 New Member

    Joined:
    Mar 2, 2008
    Messages:
    107
    Likes Received:
    0
    Location:
    Pa
    Also... Thanks critter783. I didn't see that spelling error. I usually don't make those so i didn't figure that was the problem.
     
  15. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    :rofl:
    I love being called immature...especially since I turn 41 in about 2 weeks.

    Thanks! :bigthumb:
    Since you still haven't figured out the code tags....and you're a programmer....I'll give you a hint, it's the button to the right of the Insert Image button...that looks like this #.

    lrn2program noob:hsd:
     
  16. whup

    whup I wish you had children and.. so that I could step

    Joined:
    Feb 12, 2007
    Messages:
    1,603
    Likes Received:
    0
    Seriously, you need to start trying a little harder.

    Running to a forum everytime you run into what are rudimentary errors, you will never learn how to program properly. You must learn how to diagnose and fix problems well and build up your experience in common problems and solutions.

    If you don't learn how to debug and figure out where the code is that's causing problems, and why, then you'll never be any good.

    Until you understand what you're doing, getting answers fed to you is going to hurt you in the long run.
     
  17. SuperMan91

    SuperMan91 New Member

    Joined:
    Mar 2, 2008
    Messages:
    107
    Likes Received:
    0
    Location:
    Pa

    Rofl....... Im just now learning C++ and you call me a programmer? Well there, Thank you very much.. And I Appreciate you trying to help. And your turning 41? Lol, so im guessing thats not you in the avatar. Well. Have a fun 41st B-day.
     
  18. aim2kill

    aim2kill New Member

    Joined:
    Mar 19, 2008
    Messages:
    631
    Likes Received:
    0
    Location:
    deployed.
    wow, i would like to understand this programming more. im more of an A+ type guy, i like hardware, but i think i would be well rounded at least knowing the basics. any suggestions on learning material for like self study (basic mind you)

    (sry for the thread hijacking SuperMan91) :big grin:
     
  19. Cobra Commander

    Cobra Commander OT Supporter

    Joined:
    Feb 18, 2008
    Messages:
    20,930
    Likes Received:
    0
    Location:
    UK
    dabble in c# if anything. Its the "new" C++ is great but not for our generation. Ask IT2 M about C++. He will tell you a little about it.
     
  20. Ameter

    Ameter Active Member

    Joined:
    Mar 31, 2005
    Messages:
    97,795
    Likes Received:
    1
    Location:
    Calgary, AB
    I was inclined to help you, but you strike me as lazy, arrogant, and a whole host of other negative character traits.

    You got a compiler error, one of which would be the misspelling of #include, but you couldn't even catch that.

    If you can't even figure out why it fails to compile when you've misspelled a keyword, you are in the wrong field. Change programs now
     
  21. red

    red New Member

    Joined:
    Jun 30, 2005
    Messages:
    87,095
    Likes Received:
    0
    Douchebag with 30 posts wants us to do his homework for him.

    What's my incentive here?
     
  22. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    :werd:

    The OP should read this post many times, over and over again until it starts to sink in.
     
  23. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    Even a noob is still a programmer, I'm just ahead of you on the learning curve.
    Yes, I'm turning 41. No that's not me in my AV, I'm a guy. I just love OU and big tits and chicks with glasses....so there she is for me to look at every time I post.
     
  24. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    :werd:
     
  25. SuperMan91

    SuperMan91 New Member

    Joined:
    Mar 2, 2008
    Messages:
    107
    Likes Received:
    0
    Location:
    Pa
    Wow... Your sooooo right...I should just quit. Even thou I made a small mistake and was too tired to figure it out so i posted my problem up on a forum.. Yep.. makes perfect sense.. Ne other helpful hints from such a generous person?...
    :hsd:
     

Share This Page