i can't execute after typing password. it gives me a force closed on windows. i'm not good at C. Anyone can help me?
int i;
char user, pass[4], pwd, a;
printf("insert Username : ");
scanf("%s", &user);
printf("insert Password <4 char> : ");
scanf("%c", &pass);
for(i=0;i<4;i++)
{
pwd = getch();
pass[i] = pwd;
pwd = '*' ;
printf("%c",pwd);
}
pass[i] = '\0';
printf("\n");
system("pause");
system("CLS");
printf("username %s", user);
getch();
system("pause");
When output is to a terminal, stdio is line-buffered by default. So you won't see the output of printf() until you print a newline (or print enough to fill the buffer), or explicitly flush the buffer. So change to:
printf("username %s\n", user);
or call fflush(stdout) after that printf.
The CLS has nothing to do with the problem.
Your string pass[4] is too short. After the loop
for(i=0;i<4;i++)
the var i will be 4. You then write beyond the end of the array with
pass[i] = '\0';
which is Undefined Behaviour and can easily break the system. Also your formatting methods are reversed between char and string. You have
scanf("%s", &user);
...
scanf("%c", &pass);
These should be
scanf("%c", &user);
...
scanf("%s", pass); // don't use the & here
// char user[6];
scanf("%s", &user); // scanf("%5s",user);
user is char variable . It can't contain more than one character . Use a char array.
scanf("%c", &pass); // use this scanf("%3s",pass);
pass is a char array not a char variable. Use specifier %s for this .
These are causing problems.
Related
I'm new to C, and I was trying to create a password system with 2 questions. 1. "What is your password?" I prompted the user to answer and recorded the argument. Any input was assigned to the variable. 2."Are you sure? Do you want this password? Enter Yes or No." It would decide whether to use the answer or discard it. Everything was working smoothly until this part. I couldn't type Yes or No. It automatically assumed that I had typed yes and saved it. Can somebody please give me some advice?
#include <stdio.h>
int
main ()
{
char password;
printf ("Hello User.");
printf ("Please type in your password:");
scanf ("%d", & password);
char answer;
printf ("\nAre you sure? Do you want this password? Enter Yes or No: \n");
scanf ("%c", answer);
printf ("\nAnswer is %c");
if (answer == 'Yes')
printf ("Confirmed.");
else (answer == 'No');
printf ("OK. Thank you.");
password = 0;
return 0;
}
As noted in the comments, you likely didn't intend to store a password in a single character. Nor did you likely intend to scanf the password as an integer using, which is what you're saying with %d.
You may want to use getline instead to read an entire line, and store it in password if you declare it as char array.
Let's say you gave it room for 100 chars.
char password[100];
You might read your password in with:
getline(password, 100, stdin);
You can do the same for the second question, but should always use strcmp to compare strings in C. When the two strings are equal, the return value is 0. Be careful as this is opposite of the way that "true" is usually represented by integers in C.
if (strcmp(response, "yes") == 0) {
...
}
Hopefully these tips will point you in the right direction.
Research and study the functions and syntax.
#include <stdio.h>
#include <string.h>
//function: Clears password
void clearPassword(char p[])
{
for(int i = strlen( p ); i>=0; i--)
{
p[i]='\0';
}
}
int main ()
{
char password[101]; //C version of a String
char line[100]; //C version of a String
char answer;
printf ("Hello User.");
printf ("Please type in your password: ");
fgets(line, sizeof(line), stdin); // read input
sscanf(line, "%s", &password); // store password
printf ("\nAre you sure? Do you want this password? Enter Y or N: \n");
fgets(line, sizeof(line), stdin); //read input
sscanf(line, "%c", &answer); // store answer
printf ("\nAnswer is %c \n", answer); // print answer
if (answer == 'Y') //
printf ("Confirmed.");
else
{
clearPassword(password);
printf ("OK. Thank you.");
}
return 0;
}
I just started programming in C and I was wondering why I can't store a string with multiple words with scanf().
For example, I enter: "That's an example" and it's stores only the first word "That's"
My code:
int main(void) {
char string[100];
printf("Please enter something: ");
scanf("%s", &string);
printf("You entered: %s", string);
return (0);
}
You can let scanf() read more than one word with the character class conversion specifier: %[^\n] will stop at the newline and leave it pending in the input stream. Note that you must tell scanf the maximum number of characters to store into the destination array to avoid undefined behavior on long input lines. When passing an array to scanf(), you should not pass its address as &string, but just pass string as arrays decays into a pointer to their first element when passed as a function argument.
Here is a modified version:
#include <stdio.h>
int main(void) {
char string[100];
int c;
for (;;) {
printf("Please enter something: ");
/* initialize `string` in case the `scanf()` conversion fails on an empty line */
*string = '\0';
if (scanf("%99[^\n]", string) == EOF)
break;
printf("You entered: %s\n", string);
/* read the next byte (should be the newline) */
c = getchar();
if (c == EOF) /* end of file */
break;
if (c != '\n')
ungetc(c, stdin); /* not a newline: push it back */
}
return 0;
}
Note however that it is much simpler to use fgets() for this task:
#include <stdio.h>
int main(void) {
char string[100];
for (;;) {
printf("Please enter something: ");
if (!fgets(string, sizeof string, stdin))
break;
/* strip the trailing newline, if any */
string[strcspn(string, "\n")] = '\0';
printf("You entered: %s\n", string);
}
return 0;
}
#include <stdio.h>
#define BUFF_SIZE 512
int main(void) {
char string[BUFF_SIZE];
printf("Enter something: ");
fgets(string, BUFF_SIZE, stdin);
printf("You entered: %s", string);
return (0);
}
fgets() is the best option
I think there's a problem in you scanf(); I recommend you to remove & from it. then your code should see like that:
int main(void) {
char string[100];
printf("Please enter something: ");
scanf("%s", string);
printf("You entered: %s", string);
return (0);
}
In the c language, there is no data type called a string.
A string is stored as an array of characters.
Moreover, the variable itself points to the first element of the array. Therefore, there is no need to use the '&' operator to pass the address.
So, all you have to do is the following:
int main(void) {
char string[100];
printf("Please enter something: ");
scanf("%s", string);
printf("You entered: %s", string);
return (0);
}
Don't use '&' in scanf function.
int main()
{
char string[100];
printf("Please enter something: ");
scanf("%[^\n]%*c",string);
printf("You entered: %s", string);
return 0;
}
According to https://man7.org/linux/man-pages/man3/scanf.3.html, %s will ignore white-space characters. To capture spaces you would have to use %c with the additional size of the input argument, or use %[ format. Check if scanf will add \0 byte to the end or not.
When I compile and run this the output is:
press n to continue
n
Enter the filename: [ �h�� ]
But, if I call the new(); directly it run perfectly. But when I call new(); in if statement or switch statement, it shows the above output.
I tried scanf, fgets and gets in the new() fucntion but still not working.
#include<stdio.h>
#include<stdlib.h>
int menu();
int new();
int main(){
menu();
return 0;
}
int menu(){
printf("press n to continue\n");
//char c = getc(stdin);
char c = getchar();
if(c=='n'){
new();
}
else if(c==27){
return 0;
}
}
int new(){
char filename[50];
printf("Enter the filename: ");
//fgets(filename, 50, stdin);
scanf("%[^\n]s", filename);
printf("[ %s ]\n\n", filename);
return 0;
}
getchar() will read one character from stdin and leave the \n. So when you call scanf - it stops immediately and you got nothing. To skip whitespaces and start reading from non-space character add space before format.
scanf(" %49[^\n]", filename);
Do not mix %[] and %s
Always specify max number of chars to read (leaving one additional char for nul-terminator)
And compile with highest warning level - so you do not leave menu function without return.
Oh. and check the return value of scanf
if(scanf(" %49[^\n]", filename) == 1)
printf("[ %s ]", filename);
I am having trouble getting this program to print the strings I enter properly. It keeps telling me that I have not entered data, even when I have. I also can't get the strings to compare to run my if statement. Thank for any help.
#include <stdio.h>
//function prototype
void enterPerson();
void enterChoice();
//global variables
char person[30];
char choice;
int main(void) {
enterPerson();
enterChoice();
printf("Please try the Precipitation Program again.\n");
return 0;
}
void enterPerson(){
// Ask for person name
printf("Please enter name:\n");
scanf("%s", &person);
//-------------------------------------------
printf("person is %s\n", person);
//-------------------------------------------
}
void enterChoice(){
//initialize choice
choice = "M";
//ask what they choose
printf("Do you choose test or rate? (Enter T for test R for rate)\n");
scanf("%c", &choice);
printf("Xchoice is: %c\n", choice);
if ((choice == 'T')||(choice == 'R')){
printf("choice is: %c\n", choice);
}
else{
printf("Incorrect or no data was input at this time\n");
}
}
As mentioned in comments, there are at least 3 problems:
scanf("%s", person); - do not take the address of char array.
scanf(" %c", &choice); - insert space to ignore whitespace.
choice = 'M'; - "M" is a string literal, while choice is char.
There is a linefeed (0xa) character left in the input buffer. You can see it by printing the choice variable after your scanf line with:
scanf("%c", &choice);
printf("c: %x\n", choice);
There are several options to get rid of this. Easiest is explained here.
Also there is a problem in:
scanf("%s", &person);
Character array name in C points to the first character, so you should fix this with:
scanf("%s", person);
I know about the difference and the advantage/disatvantage of using scanf and fgets.
I don't understand the relations between printf and this two C standard functions.
I have this simple code:
void print_choice(char * list, char * choice)
{
/* check parameters */
if(!list || !choice)
return;
printf("list of users: %s\n", list);
printf("Choice -- ? ");
/* scanf("%s", &choice); */
/* fgets(choice, 20, stdin); */
}
int main()
{
char choice[20];
char * list = "marco:dario:roberto:franco";
print_choice(list, choice);
printf("choice = %s\n", choice);
return 0;
}
if I use fgets, printf print the result correctly on stdout;
If I use scanf, printf` doesn't print anything on stdout.
Why this behaviour?
You used scanf("%s", &choice); which passes a char ** to scanf() when it expects a char *.
Drop the &.
If your compiler wasn't complaining, you either haven't turned on enough warnings or you need a better compiler.
Change
scanf("%s", &choice);
to
scanf("%s", choice);
you have to use
scanf("%s", choice);
instead of
scanf("%s", &choice);
Changing this scanf("%s", &choice); to this scanf("%s", choice); will cause scanf and fgets to show almost similar behavior.
scanf requires an address as an argument. It goes on and stores the input from stdin after converting it based on the supplied format specifier. Here the format specifier is %s so it will try to store the input at the address pointed by address of choice . What you need here is the address from where the choice array will begin,which in this case is choice itself.