i do not want to use strcmp() so i made it myself.
here is my code:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>


int main()
 { 
    char pass[50];       
    int strcomp(char pass);
    cout<<" ENTER THE PASSWORD ";
    gets(pass);
    int m=strcomp(pass);
    if(m==1)
    cout<<" congrats! correct password ";
    else cout<<" sorry wrong password ";
  }
int strcomp(char pass)
{   int org="854856", flag=0;;
    for(int i=0;i<6;i++)
    {if(pass[i]==org[i])
    flag=1;
    else {flag=0 break;}
    }
    if(flag==0)
    return 0;
    else return 1;
}

plz tell in detail where i am wrong .

Recommended Answers

All 15 Replies

You should be passing a pointer to the first character of the string.

plz edit n post it.
dont know much about pointers.

Look, you're the one posting your code and saying "fix it". It's really getting to be past time for you to try to do some work rather than using this site as a "fix my homework" type of thing. I'm guessing there is a language barrier here, otherwise I wouldn't have been so lenient thus far.

Make an attempt to do this by yourself -- we'll show you any necessary corrections. If you don't understand what a pointer is, then don't try to write your own strcmp. And perhaps you should do some reading or re-reading of some reference section on pointers.

int strcomp(char pass)

Here, pass is not a pointer.

Hey Dave i made this program and all others i post . i make lots n lots of progs. i posted the ones i wasnt able to debug. wats wrong in it . for others it is a challenge n those who take it answer my probs.

You seem to be ignoring things such as "never use gets" and such things, which seems to indicate that you don't really want to learn, you just want someone else to make your assignments correct.

Your posts dump code, and say, "plz debug". Do you post compiler errors/warnings and ask for explanations? [edit]Once[/edit] Do you explain what attempts you have made in solving the problem yourself? Do you demonstrate that you are learning by acknoweldging it in your threads explicitly, or implicitly by using such advice in other threads? To most of these, I'd say what I have seen is generally "no".

[edit]Have you once had the courtesy to post code within [co[u][/u]de][/co[u][/u]de] tags?

see i want to learn . from next time i will give the compiler warnings . i will also include wat attempts i made.so tell me why not to use " gets " when my professor uses it very often.

now wat wud u say, i learnt pointers and debugged my prog myself, thats a proof for my eagerness to learn more.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>


int main()
{
char pass[50];
int strcomp(char* pass);
cout<<" ENTER THE PASSWORD ";
gets(pass);
int m=strcomp(pass);
if(m==1)
cout<<" congrats! correct password ";
else cout<<" sorry wrong password ";
getch();
}
int strcomp(char* pass)
{ char* org="854856", flag=0;;
for(int i=0;i<6;i++)
{if(pass==org)
flag=1;
else {flag=0; break;}
}
if(flag==0)
return 0;
else return 1;
}

see i want to learn . from next time i will give the compiler warnings . i will also include wat attempts i made.so tell me why not to use " gets " when my professor uses it very often.

He is one stupid proffesor..who should be kicked out of the school/college immidietely...See this link..u'll find your answer
http://www.gidforums.com/t-4272.html

It is never safe, that's why.

Is it because the user can overflow the buffer by entering more characters than the array or the pointer can hold? If that is the reason then scanf(), cin>> they are also not safe. So why blame gets() only?
Or is it because gets() takes a whole line, whereas, scanf() and cin>> takes one word at a time?

lol, on my linux distribution man gets says something in the lines of DO NOT USE THIS FUNCTION IT IS NOT SAFE... and no, many of the uses of scanf is unsafe (k, Im not an expert but atleast the string inputs are bad)... cin >> should be good aslong as proper objects are used, a while since I used it but I suppose cin >> (char * type) would be unsafe if used, getline( cin, (string type) linebuf) on the other hand is (safer I suppose you could still missuse it somehow).

The links given above says that there is no way to pass the number of characters gets() should read, so a mischievous user can intentionally overflow the buffer to cause havoc. And "THAT'S WHY" you should avoid using gets() and use fgets() instead. However, if that's the actual reason for not using gets() I fail to see why scanf() or cin>> is any safer. Why call gets() evil and be silent about scanf() and cin?
Based on that explanation should we not advise programmers to use only fgets() or getline() and to avoid scanf(), cin, gets() all of them?

I myself hardly use gets() to read strings. In C I use scanf("%[^\n]", cstring) and C++ I use getline(). That's why I havent bothered myself much about using gets() and since people kept echoing "don't use gets(), it's evil" I never thought of using it ever in my program. Now that I come to think of it I really dont think gets() is evil. If it is then so is scanf() and cin>>.

As I said, scanf("%s", cstring) is no better than gets(cstring), as well as cin >> cstring (I dont feel like checking if that is actually availible though)... cin >> (int type, or any other type that can safly handle the data that can come out of it) is however safe, as well as scanf("%d", &int_type)....

Then why dont we hear something along this line about cin and scanf() as we hear about gets():

He is one stupid proffesor..who should be kicked out of the school/college immidietely...

reason: the professor used gets().
Why not say the same when the professor uses scanf() or cin>>.
or may be this:

It is never safe, that's why.

Is scanf() and cin>> any safer?
why doesnt the man manual say this about scanf() or cin>> specially when dealing with c style strings:

DO NOT USE THIS FUNCTION(or object) IT IS NOT SAFE...

why the same faq calling gets(s) evil doesnt warn the reader that scanf("%s",s) and cin>>s is also evil?

Well there are proper uses to cin and scanf, it is only the get string part that is inherently bad... while on the other hand gets has no safe use whatsoever.
(Well I suppose one should include a warning for those uses as well, I am not sure it is not done I only remember seeing that in the man page of gets)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.