#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    2
    Rep Power
    0

    Smile help with simple if statements


    im attempting to teach myself c++ due to the fact that the current highschool i attend has dropped any sort of computer teaching classes due to the NYC cutbacks :mad: so, you guys (and gals ;) ) will hear alot more from me. anyways, heres my first question!

    i want this program to behave almost as a password system. if the proper name (patrick) isnt entered, then it tells you that you are not a legal user and shuts down. i've reached my first hurdle, i cant make the program say anything else except what is supposed to be said when the correct name is entered, even when the name is incorrect! what am i doing wrong? :(

    #include <iostream>

    int main()
    {
    int patrick,a,c;
    a=1,patrick=1,c=2;
    a=patrick;
    std::cout<<"Enter your name\n";
    std::cin>>a;
    if (a==patrick)
    {
    std::cout<<"This is your name\n";
    }
    else
    {
    std::cout<<"you are not patrick! >:( \n";
    }
    std::cout<<"Goodbye :)\n";
    int close;
    std::cout<<"please close the program.\n";
    std::cin>> close;
    return 0;
    }


    also, i want the program to prompt you to close the program by typing "close" but it closes on its own!

    all help will be apreciated! :)
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,086
    Rep Power
    2222
    I'm confused. You say you want the user to enter his name, yet the variables are integers! Are you entering numbers or names?

    Running it again as it is, try entering numbers and see what it does. Now, I'm not sure what cin does when it's told to expect a number and gets letters instead. The reason why it seems to blow through that cin<<close; could be that it thinks there are still characters in the input buffer.

    Also, you shouldn't need to preface every cout and cin with std:: . Namespaces came out after my time, but I think there's a using statement you can put at the start of the program.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    2
    Rep Power
    0
    i thought that i had to make the variables integers :confused: im not really sure what im doing, also im not sure what the buffer thing is (didnt get up to that chapter yet :p ) but, what am i supposed to do if the variables are all words (names) how do i define them? i didnt know how to do that so i defined them with numbers. im not sure about alot of things. how can i fix this so that i use only words, not numbers and that it will recognize the correct name?:confused: :confused: :confused:
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,592
    Rep Power
    4207
    Something like this perhaps.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(void)
    { 
        string name;
        cout<<"Enter your name\n";
        getline(cin, name);
        if (name=="patrick")
        {
            cout<<"This is your name\n";
        }
        else
        {
            cout<<"you are not patrick! >:( \n";
        }
         cout<<"Goodbye :)\n";
        return 0;
    }
    The using namespace std; statement saves you the trouble of typing std:: in front of cin, cout etc., because it qualifies the namespace for you. If you don't understand the rest, keep reading the book and it'll all make sense sooner or later. Hope this helps!

IMN logo majestic logo threadwatch logo seochat tools logo