Thread: My name program (need help)

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    52

    My name program (need help)

    I am really new to c++ & have a question about my code.
    I am tryin to get this password checker to work but I cant get it to run correctly.

    Code:
    #include <iostream.h>
    int program();
    int main()
    {
    	char name[20];
    	cout << "            Input name for identification." << endl;
    
    	cin >> name;
    
    	if (name != "tim")
    	{
    	cout << "                 Name not Registered." << endl;
    	return 0;
    	}
    	else {
    		if (name == "tim")
    	program();
    	}
    
    	return 0;
    }
    int program()
    { 
    	cout << "            Welcome To the Restricted Zone." << endl;
    	
    	return 0;
    }
    
    /* I get no errors but it does not run how I want it to. No matter what I
    put in it still sends back "Name not Registered". Does anyone see the 
    problem? Any help is gladly appreciated thanks!*/

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You need to use strcmp( ) to compare char arrays. Also, you don't need the else if, just else.
    Code:
    if( strcmp( name, "tim" ) != 0 )
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    52
    I dont get what it does but i put in what u said and this is waht happened.
    Code:
    #include <iostream.h>
    
    int strcmp();
    int program();
    int main()
    
    {
    	char name[20];
    	cout << "            Input name for identification." << endl;
    
    	cin >> name;
    
    	if( strcmp( name, "tim" ) != 0 )
    
    	{
    		cout << "                 Name not Registered." << endl;
    		return 0;
    	}
    	else
    	{
    		(name == "tim");
    		program();
    	}
    
    	return 0;
    }
    int program()
    { 
    	cout << "            Welcome To the Restricted Zone." << endl;
    	
    	return 0;
    }
    errors:
    (17) : error C2660: 'strcmp' : function does not take 2 parameters
    Last edited by Siggy; 06-29-2004 at 10:49 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > (17) : error C2660: 'strcmp' : function does not take 2 parameters
    Use
    #include <string.h>

    To get the prototype for strcmp()

    > int strcmp();
    This zero-parameter version is no good
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    52
    Thanks guys its workin now. So now i just have to read it over and over and over to encompus what the additions are. Thanks alot guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM