943,712 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 905
  • C RSS
Aug 24th, 2009
0

C language help claer all the doubts if possible

Expand Post »
  1. include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. char u,pp;
  6. printf("\n\n\t\t enter the username and password");
  7. scanf("%c%c",&u,&pp);
  8. getch();
  9. }

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.
Last edited by John A; Aug 25th, 2009 at 11:43 pm. Reason: added code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
feroz121 is offline Offline
3 posts
since Aug 2009
Aug 24th, 2009
2

Re: C language help claer all the doubts if possible

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.

Quote ...
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:
  1. 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:
  1. 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.
Quote ...
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.
Quote ...
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/c.../cstdio/scanf/
http://www.cplusplus.com/reference/c...stdio/getchar/
Last edited by tux4life; Aug 24th, 2009 at 8:40 am. Reason: add info, revise post to be friendlier
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Aug 24th, 2009
1

Re: C language help claer all the doubts if possible

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.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Aug 24th, 2009
0

Re: C language help claer all the doubts if possible

Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Aug 24th, 2009
5

Re: C language help claer all the doubts if possible

Quote ...
Now you can get the username by using scanf like this:
  1. 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:
  1. 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:
  1. char username[50];
  2.  
  3. /* Unsafe code */
  4. fgets(username, INT_MAX, stdin);
Quote ...
it's like using a chainsaw with no safety guards.
Welcome to C. There are no safety guards here, and scanf() is kind of low on the list of great ways to screw yourself over.
Quote ...
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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: FEC coding
Next Thread in C Forum Timeline: weird forking problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC