| | |
C language help claer all the doubts if possible
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2009
Posts: 3
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
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.
Last edited by John A; Aug 25th, 2009 at 11:43 pm. Reason: added code tags
Wait, I doubt whether this program will correctly terminate on every machine, it appears that it won't, because you're using
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
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:
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:
More info about character arrays and the scanf-function can be found at the links which are at the bottom* of this post.
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/
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.
c Syntax (Toggle Plain Text)
char username[50];
Now you can get the username by using scanf like this:
c Syntax (Toggle Plain Text)
scanf("%s", username);
•
•
•
•
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.
* - 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."
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.
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.
void main() is bad, references:http://www.gidnetwork.com/b-66.html
http://users.aber.ac.uk/auj/voidmain.shtml
http://faq.cprogramming.com/cgi-bin/...&id=1043284376
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
•
•
Now you can get the username by using scanf like this:
More info about character arrays and the scanf-function can be found at the links which are at the bottom* of this post.C Syntax (Toggle Plain Text)
scanf("%s", username);
c Syntax (Toggle Plain Text)
scanf("%49s", username);
c Syntax (Toggle Plain Text)
char username[50]; /* Unsafe code */ fgets(username, INT_MAX, stdin);
•
•
•
•
it's like using a chainsaw with no safety guards.
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.
-Tommy (For Great Justice!) Gunn
![]() |
Similar Threads
- Which language to learn? (Computer Science)
- Calling a function by a variable (C++)
- why assignment from incompatible pointer types? (C)
- python extending with c(doubts) (Python)
- Uninstalling IE 6 language packs? (Web Browsers)
- Macromedia Coldfusion 5 Language Reference (ColdFusion)
Other Threads in the C Forum
- Previous Thread: FEC coding
- Next Thread: weird forking problem
| Thread Tools | Search this Thread |
* adobe api append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax database directory dynamic execv feet fgets file floatingpointvalidation fork frequency function getlogicaldrivestrin givemetehcodez global gtkgcurlcompiling gtkwinlinux highest histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer license linked linkedlist linux list lowest matrix meter microsoft mqqueue multi mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scheduling segmentationfault send single socketprogramming stack standard strchr string suggestions system test testautomation testing unix urboc user whythiscodecausesegmentationfault win32api windows.h windowsapi






