Results 1 to 4 of 4

Thread: Problem With Strings

  1. #1
    Guest

    Post

    For some reason this code just will not work, it just repeats itself no matter what you type...

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
    start:
            char* pass;
    
            system("cls");
            cout<<"Hero Password Protection v2"<<endl;
            cout<<"Copyright (C) 2000 Hero Inc. Software"<<endl;
            cout<<"By Jake Bush"<<endl;
            cout<<"   [email protected]"<<endl;
            cout<<""<<endl;
            cout<<"Password : ";
                    cin>>pass;
            
            if(pass == "hello")
            {
                    system("cls");
                    return 0;
            }
    
            goto start;
    }
    any suggestions?

  2. #2
    Guest
    At the time of this writing, no one has even looked at this thread. Does anyone even use C++?

  3. #3
    Lively Member
    Join Date
    Jul 2000
    Posts
    94
    couple problems with you code. First don't use gotos! Sorry about yelling like that. but in C/C++ goto are really frowned upon. Second you have a character pointer but no memory to point at. You need to actually make a character array. Third, You can't compare a string using == you should use strcmp of one of its brethren. Here is my reworking of your code:
    Code:
    #include <iostream.h>
    #include <string.h>//need this for string compare
    #include <stdlib.h>
    int main()
    {
    	char pass[100];
    	do
    	{
            
    
            system("cls");
            cout<<"Hero Password Protection v2"<<endl;
            cout<<"Copyright (C) 2000 Hero Inc. Software"<<endl;
            cout<<"By Jake Bush"<<endl;
            cout<<"   [email protected]"<<endl;
            cout<<""<<endl;
            cout<<"Password : ";
            cin>>pass;
                    
    	}while(strcmp(pass, "hello") !=0);
        system("cls");
        return 0;
    }

    This is one of many many ways to do this. This is the closest to your code.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    In C++, it's often easiest to use the string class from the STL (comes with VC and most other compilers). You need this:
    Code:
    #include <iostream> // NO .H!!!
    #include <string>
    
    using namespace std; // V. important
    
    void main() {
        string A = "Hello";
        string B = "Different";
    
        A = A + " " + B;
        cout << A << endl;
    }
    It's not so important at this stage, but when you get to more advanced things it really helps.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width