include<stdio.h>
#include<conio.h>
void main()
{
char u,pp;
printf("\n\n\t\t enter the username and password");
scanf("%c%c",&u,&pp);
getch();
}

what was wrong in this code, Its taking the username but its not properly taking the input of password. And my another doubt is how can i use string for this program instead of character so that i can give big names for username and password.

And my final doubt how to store this data which was entered by the user so that he would able to login again with the same username and password again and again.

give me a complete program using strings and adding file operations so that i can save the information and use them again and again to login.

Recommended Answers

All 4 Replies

Wait, I doubt whether this program will correctly terminate on every machine, it appears that it won't, because you're using void main() , the standard says that you must use int main() and not void main() (void main() is evil!!)

Further you're not using code tags (you should, as described in the forum announcement about code tags, so please use code-tags from now on).

Hmm... What else can I comment on?
Well, you're making use of an unportable library called conio.h but for what purpose? Only to ask for a key press just before the program closes? You could use the getchar() function instead of conio's getch() , so you can make your code portable.

what was wrong in this code, Its taking the username but its not properly taking the input of password.

Well, you want to store a username into a character variable, a character variable is only capable of holding one character, not a sequence of characters. If you want to store a sequence of characters, you should use a character array (aka: C-string), a character array is declared like this:

char username[50];

The above code reserves space for storing a username which can consist out of a maximum of 49 characters (remember: the last character is always used to store the nul-terminator)
Now you can get the username by using scanf like this:

scanf("%s", username);

More info about character arrays and the scanf-function can be found at the links which are at the bottom* of this post.

And my another doubt is how can i use string for this program instead of character so that i can give big names for username and password.

give me a complete program using strings and adding file operations so that i can save the information and use them again and again to login.

No, this doesn't conform to Daniweb's homework policy, we don't give free code here, we only provide you paths to the solution, not the entire solution itself.

* - This information should get you started:
http://www.cplusplus.com/doc/tutorial/ntcs/
http://www.cplusplus.com/reference/clibrary/cstdio/scanf/
http://www.cplusplus.com/reference/clibrary/cstdio/getchar/

commented: Hard work. But seriously, scanf for reading a string? ;) +18
commented: Another home run, this one's out of the ball park! +36

i agree that "void main" is to be avoided, but merely calling it "the evil" without explaining why is not going to impress many newbies, since it will always appear to them to work.

Anyhow, while "void main" may blow up a program in some future event, "scanf" will likely blow up a program here and now.

you should not advise beginners to use "scanf()" -- there's far too many pitfalls and unexpected behavior with that function. it's like using a chainsaw with no safety guards.

use "fgets()" instead.

commented: Thanks for the correction :) +20

Now you can get the username by using scanf like this:

scanf("%s", username);

More info about character arrays and the scanf-function can be found at the links which are at the bottom* of this post.

I have not seen any references that talk about reading strings the right way with scanf(). It's always the same "%s" format string, even though making it overflow safe is super easy:

scanf("%49s", username);

I think a big reason why fgets() is recommended is because everyone teaching scanf() uses it unsafely. I'm sure fgets() would be evil too if everyone learned and used it like this:

char username[50];

/* Unsafe code */
fgets(username, INT_MAX, stdin);

it's like using a chainsaw with no safety guards.

Welcome to C. :D There are no safety guards here, and scanf() is kind of low on the list of great ways to screw yourself over.

use "fgets()" instead.

That is not much better than saying void main is evil without explanation. Using fgets() to replace scanf() is good advice to avoid scanf() pitfalls, but then the same people who give the advice go on to recommend sscanf() for parsing the string... Imagine how confusing that would be to a beginner since scanf() and sscanf() are so similar. ;)

commented: Some other interesting stuff about C :P +20
commented: bork bork bork :) +15
commented: - that's another bookmark +27
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.