C language help claer all the doubts if possible

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2009
Posts: 3
Reputation: feroz121 is an unknown quantity at this point 
Solved Threads: 0
feroz121 feroz121 is offline Offline
Newbie Poster

C language help claer all the doubts if possible

 
0
  #1
Aug 24th, 2009
  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: C language help claer all the doubts if possible

 
2
  #2
Aug 24th, 2009
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:
  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.
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/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
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,663
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is online now Online
Posting Virtuoso

Re: C language help claer all the doubts if possible

 
1
  #3
Aug 24th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: C language help claer all the doubts if possible

 
0
  #4
Aug 24th, 2009
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: C language help claer all the doubts if possible

 
5
  #5
Aug 24th, 2009
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);
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.
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.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC