help me to find the problem

This is a discussion on help me to find the problem within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <conio.h> void getpass(char*); int main(){ char tpass[9]; printf("\nPlease enter your password:"); getpass(tpass); while (tpass != "password"){ ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void getpass(char*);
    
    int main(){
    	char tpass[9];
    
    	printf("\nPlease enter your password:");
    	getpass(tpass);
    	while (tpass != "password"){
    		printf("\n\aWrong password! please try again:");
    		getpass(tpass);
    	}
    
    }
    
    /***********************************************/
    void getpass(char* pass){
    
    	/* function variables */
    	char ch;
    	int flag = 0, i = 0;
    
    	/* function process*/
    	while(flag == 0){
    		ch = getch();
    		if (ch == '\r'){
    			if (i != 0){
    				pass[i] = NULL;
    				flag = 1;
    				continue;
    			}
    			printf("\a");
    			continue;
    		}
    		else if (ch == '\b'){
    			printf("\b");
    			i--;
    			continue;
    		}
    		printf("*");
    		pass[i] = ch;
    		i++;
    	}
    
    }
    I dont know why it go to while loop when I enter the pass word correctly!

    this is a part of my whole code
    Last edited by behzad_shabani; 06-12-2008 at 01:23 AM.

  2. #2
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    while (tpass != "password"){

    should be

    while (strcmp(tpass, "password") != 0){

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    "password" is a string literal, which is const char*, so you try to compare an array (which becomes char*) to a const char*, which you understand, will never be true, since they are two distinct addresses.
    You need to use the strcmp string function for comparing strings in C. Welcome to C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    thanks all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 10:13 AM
  2. Off by a penny problem
    By ninety3gd in forum C++ Programming
    Replies: 2
    Last Post: 05-18-2009, 06:41 PM
  3. fullscreen toggling problem
    By hannibar in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2005, 08:06 PM
  4. Replies: 4
    Last Post: 08-15-2002, 12:35 PM
  5. Won't Return pointer, i can't find why...
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 08:50 PM

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