IF Help

This is a discussion on IF Help within the C++ Programming forums, part of the General Programming Boards category; Code: void user_check() { cout << "Hello?"; if(user == "Andrew"){ system("cls"); cout << "Confirmed ID For " << user <<" ...

  1. #1
    Fallen AndyBomstad's Avatar
    Join Date
    Jan 2005
    Posts
    52

    IF Help

    Code:
    void user_check()
    {
        cout << "Hello?";
        if(user == "Andrew"){
            system("cls");
            cout << "Confirmed ID For " << user <<" ..."<<endl<<endl;
            level = 2;
            main();
        }
        else
            cout << "ID Confirmation Failed"<< endl<<endl;
        system("cls");
        main();
    }
    ok well theres the code my question is, that im trying tp build a login screen that will only allow you to enter if the user types the name which in this case is "Andrew" but i know the IF statement works for constants single characters but it not working that way for the string of letters making up Andrew, and ive hear alotta different things to use to check it... but yeah if you guys understood what im saying which is good cause i didnt...any help would be good lol

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Try strcmp(user,"Andrew").

    Edit: Ha...what I meant to say is that someone else will be along shortly with far more information.
    Last edited by pianorain; 02-03-2005 at 03:01 PM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) Don't ever call main. Update your compiler, you shouldn't even be allowed to do this any more.
    2) If you're using the string class, then you can use the equality operator == to test for, oddly enough, equality. Otherwise, if you're using "c strings", then you'll have to use something like strcmp to test for equality.
    3) Globals.... eeeew.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    When comparing two strings in C, you could use strcmp(). strcmp() is a function included in the C library that Compares two strings.

    It works like the following:
    > Compares string1 to string2 character by character.

    Code example:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char str[] = "Hello";
    
        if (!strcmp(str, "Hello"))
            printf("It matched!\n");
    
        return 0;
    }
    You may be asking why I asked if strcmp() failed, or equalled zero. Though maybe this will help:

    • <0 string1 is less than string2
    • 0 string1 is the same as string2
    • >0 string1 is greater than string2

    Meanwhile, comparing strings in C++ is inherently different than comparing numbers. Numbers have constant, universally meaningful values. To evaluate the relationship between the magnitude of two strings, you must make a lexical comparison. Lexical comparison means that when you test a character to see if it is "greater than" or "less than" another character, you are actually comparing the numeric representation of those characters as specified in the collating sequence of the character set being used.

    C++ provides several ways to compare strings, and each has their advantages. The simplest to use are the non member overloaded operator functions operator ==, operator != operator >, operator <, operator >=, and operator <=.
    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    int main() {
        // Strings to compare
        string s1("This ");
        string s2("That ");
        for (int i = 0; i < s1.size() && i < s2.size(); i++) {
            // See if the string elements are the same:
            if (s1[i] == s2[i]) {
                cout << s1[i] << "  " << i << endl;
                // Use the string inequality operators
                if (s1 != s2) { 
                    cout << "Strings aren't the same:" << " ";
                    if (s1 > s2)
                        cout << "s1 is > s2" << endl;
                    else
                        cout << "s2 is > s1" << endl;
                }
            }
        }
    
        return 0;
    }
    Most often, this will be the ASCII collating sequence, which assigns the printable characters for the English language numbers in the range from 32 to 127 decimal. In the ASCII collating sequence, the first "character" in the list is the space, followed by several common punctuation marks, and then uppercase and lowercase letters. With respect to the alphabet, this means that the letters nearer the front have lower ASCII values than those nearer the end.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    C++ provides several ways to compare strings, and each has their advantages. The simplest to use are the non member overloaded operator functions operator ==, operator != operator >, operator <, operator >=, and operator <=.
    No, the simplest to use is in fact the overloaded == operator, since you're using the string class. What's the point in looping through it by hand when you can just do one test?
    Code:
    string s1("This ");
    string s2("That ");
    
    if( s1 == s2 )
        cout << "They're equal.";
    else
        cout << "They're not.";
    I can appreciate your showing them how to loop through the individual characters (which can be done with 'c strings' as well), but to say it's the "simplest" is not correct.

    Quzah.
    Hope is the first step on the road to disappointment.

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