programming challenge

This is a discussion on programming challenge within the C++ Programming forums, part of the General Programming Boards category; hey all, i'm new so give me a chance Code: int main() { int pass; char login; cout<<"hey please enter ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    6

    programming challenge

    hey all, i'm new so give me a chance

    Code:
    int main()
    {
      int pass;
      char login;
    
      cout<<"hey please enter your login: ";
      cin>> login;
      cin.ignore();
    
      if ( login == john ) {
      cin.get();
      cout<<"welcome john/n";
      }
      cin.get();
    }
    what i'm trying to do is make it so you have to have the correct login and if you don't it's says so, but i'm struggling so hard to make

    I think i could do it with numbers, but with letters it seems to be completely different, can someone please help me?

  2. #2
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Use a 'string' for the variable 'login', and include <string>.

    Wrap from right after you ask for the login name, to the end of the cin.get with a while(1) { }, then put a break; in the if statement, and put an else statement on that if statement that cout's "wrong username".

    Code:
    #include <string>
    
    int main()
    {
      string pass;
      string login;
    
      cout<<"Hello, please enter your login: ";
    
      while(1) {
        cin >> login;
    
        if (login == "john") {
          cout << "Welcome, John/n";
          break;
        } else {
          cout << "Invalid username, sorry./n";
        }
    
      }
    
      cin.ignore();
      cin.get();
    }
    You could also make the if statement: (login == "john" || login == "John" || login == "JOHN")
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    6

    Smile

    thankyou very much, i'm still new to this hehe, by anychance does string mean you can use both int and char in the same or code or am i wrong :S

  4. #4
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by grohyt
    thankyou very much, i'm still new to this hehe, by anychance does string mean you can use both int and char in the same or code or am i wrong :S
    Ints hold numbers that you can perform operations on. Chars hold characters (all the keys on your keyboard basicly, something like 255), which includes A-Z, 0-9, !-), <>, [], etc. but are only for display. To hold more than 1 char in a 'char', you declare it an array, char[100], etc. and in C theres some functions you can use to make it accept "blah blah blah 29473 blah 9jblah" as a string you want to hold - its called a c-style string. Then in C++ there is a 'string', which is like a c-style string, but much easier to use. It holds characters, and numbers (but cannot perform operations like 1+1 on the numbers).

    Theres a string tutorial on the site if you want to know more about strings, for C or C++. The C++ strings are fun because they are so easy to use, string3 = string1 + string2;, string1 = string1 + "additional words";, etc.
    Last edited by Dae; 09-19-2005 at 08:09 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. AI Challenge
    By unknown_111 in forum General AI Programming
    Replies: 0
    Last Post: 10-02-2007, 01:18 AM
  2. A Challenge in C++
    By Brad0407 in forum C++ Programming
    Replies: 38
    Last Post: 07-18-2007, 01:56 PM
  3. Programming Challenge (for my school)
    By Ezerhorden in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2006, 06:56 AM
  4. Challenge?
    By Mackology101 in forum C Programming
    Replies: 5
    Last Post: 11-23-2004, 02:30 PM
  5. Requesting a challenge
    By RealityFusion in forum C++ Programming
    Replies: 8
    Last Post: 08-18-2003, 09:24 PM

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