Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
i was writing a password checking program. and i encountered a problem.
when i run the program, everything gets printed out as plain text instead of allowing user input.I have also tried to run other programs that worked before but they are all having the same problem. Here is my code and an image of the problem :
#include <stdio.h>
main()
{
int password, pass2;
char name;
printf("\nInput your name: ");
scanf("%c", &name);
printf("\nEnter your password(must be a number): ");
scanf("%d, &password");
printf("\nRe-enter your password(must be a number): ");
scanf("%d, &pass2");
if("password == pass2")
{
printf("welcome %c", name);
}
else{
printf("sorry your passwords did not match!");
}
getch();
}
You have made some mistakes,
scanf("%d, &password");
The correct way of calling a scanf is the following.
scanf ( const char * format, ... );
The correct way of coding in C this line would be.
scanf("%d", &password);
Also have an error in line 15.
if("password == pass2")
The correct way of comparing numbers would be
if(password == pass2)
The char problem.
You are declaring a char, that in C is a single character.
When reading a string, you should declare a array of char, and to read/write it you should use %s.
There are problems with this approach too, words like "firstname lastname" will not work, but I will put this as something to you work with.
It's normal some mistakes when learning, you can check some help in the
http://www.cplusplus.com/
For a more "guided" approach you can also check this
http://c.learncodethehardway.org/book/
Here is the "correct" final code. (it's still need some improves but will run as expected.)
#include <stdio.h>
int main() {
int password, pass2;
char name[20];
printf("\nInput your name: ");
scanf("%s", name);
printf("\nEnter your password(must be a number): ");
scanf("%d", &password);
printf("\nRe-enter your password(must be a number): ");
scanf("%d", &pass2);
if(password == pass2)
{
printf("welcome %s", name);
}
else{
printf("sorry your passwords did not match!");
}
}
Related
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
scanf Getting Skipped [duplicate]
(1 answer)
Closed last year.
I am currently working on a code and I keep having issues with my code. I will add it below, but for some reason, whenever I ask for the input, it skips over the second scanner and goes straight to the third. Does anyone know what the issue may be?
#include <stdio.h>
int main()
{
char a,b,c,d;
printf("Please input the price of your shopping items:");
printf("\n");
printf("Item 1:");
scanf("%c", &a);
printf("Item 2:");
scanf("%c", &b);
printf("\n");
printf("Item 3:");
scanf(" %c", &c);
return 0;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I made a simple little program and it crashes before it gets to the part of the if statement.
#include <stdio.h>
int main()
{
char name[100];
printf("What is your name?");
scanf("%s", name);
printf("Hello, %s\n",name);
printf("You are trapped in a tunnel. Go through the right doors to get out\n");
int choice;
printf("Choose door 1 or door 2");
scanf("%d", choice);
if (choice == 1){
printf("This is the correct door");
}
else if (choice == 2){
printf("This is the wrong door");
}
else{
printf("Press 1 or 2");
}
return 0;
}
My program runs fine with no errors, it just crashes...
scanf("%d", &choice);
This is how it was meant to be. choice is a standalone int, so you need to pass its address to scanf to get correct results. What you do now is passing a value that is stored in choice to scanf, while it expects an address.
The C library function int scanf(const char *format, ...) reads
formatted input from stdin.
So it should be like this:
int choice;
printf("Choose door 1 or door 2");
scanf("%d", &choice); //& means the address-of
if (choice == 1){
printf("This is the correct door");
}
else if (choice == 2){
printf("This is the wrong door");
}
else{
printf("Press 1 or 2");
}
You miss & in scanf statement that why it doesn't working..
It should be
scanf("%d",&choice);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am trying to develop a small program which is available below:
When I run the program, even after the right input, it gives me the output described in else.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char course;
printf("Enter Your Course Name: \n");
scanf(" %s", &course);
if (course == 'TOEFL') {
printf("Yes, you are eligible \n");
} else {
printf("You Can Not Join Us \n");
}
return 0;
}
You mean
scanf(" %c", &course);
But also,
Strings in c MUST be wrapped with double qoutes, the expression 'TOEFL' must be generating a warning about multi character constant, do not ignore it.
Strings in c, are compared one character at a time, so you need to use a function called strcmp() for that.
To read a string, you need an array to store it in, and yes, the "%s" specifier
char cours[100];
scanf("%99s", course);
if (strcmp(course, "TOEFL") == 0) ...
course is a char variable, so it only can contain a single character.
Try changing its declaration to:
char course[10];
See my code comments for explaination //xxxxxx below
#include <stdio.h>
#include <stdlib.h>
int main()
{
char course; //this decl stores only single character not string
printf("Enter Your Course Name: \n");
scanf(" %s", &course); //passing string argument
if (course == 'TOEFL') {//can't compare strings with == use strcmp
printf("Yes, you are eligible \n");
} else {
printf("You Can Not Join Us \n");
}
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Part of my assignment is to get user input; then, if the input is incorrect, prompt them to try again. When I use if statements and while loops in this way, they completely skip over the scanf command, and the program either runs infinitely or terminates immediately, skipping over the second chance for user input. Any idea how to fix this?
#include <stdio.h>
int main(void)
{
int number;
printf("please insert positive number\n");
scanf(" %d", &number);
// if the user inputs a value that is not a positive integer, it becomes false
// i want the if statement to re-prompt them to input the correct value
if (number <= 0)
{
printf("try again\n");
scanf(" %d", &number);
}
return 0;
}
scanf Returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned.
The scanf function reads data from the standard input stream stdin and writes the data into the location given by argument. Each argument must be a pointer to a variable of a type that corresponds to a type specifier in format.
I recommend handle errors . it will happen in Real World code. (write Concise and robust Code).
so i recommend something like this:
#include <stdio.h>
int read_positive_int(int *n){
int retry = 3;
do{
printf("Please enter a positive number:\n");
if (scanf("%d", n) == 1){
if (*n > 0) return 1; //ok
}
} while (--retry);
return 0; //error
}
int main(void)
{
int n;
if (read_positive_int(&n))
printf("n = %d\n", n);
else
printf("error\n");
}
i hope this helps.
the code does what it's written to do, so in your case it's following:
message to user: enter a positive number
scanf: reads user's input
determine, if the number is positive
if yes - the if body will not be executed
and program ends
if not - print a message, read a number and program ends
the thing you are looking for is a loop
you want to do something like this:
while the entered number is not positive, ask the user to enter the number again
that's what loops are used for
int number;
printf("please insert positive number\n");
scanf("%d", &number);
while (number <= 0) {
printf("try again\n");
scanf("%d", &number);
}
return 0;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
while i am running the above title is appearing as error
#include <stdio.h>
int main()
{
int i;
char name[20];
printf("Enter name: ");
scanf("%s",name);
printf("Your name is %s",name);
while(name[i]!="\0")
{
i++;
if(name[i]==" ")
{
strcpy(b[i],name[i]);
printf("copied name: ");
scanf("%s",b[i]);
}
}
}
while i am running this it is showing this error why? warning: comparison between pointer and integer.
"\0" is a string, '\0' is a character. As you comparing a character, you need the latter.
Also, as pointed out by chqrlie, there are many other issues - you need to check your compiler warnings/errors and fix them all. For example,
name[i]==" " is wrong with the same reason.
where is b declared?
where is i initialized??
There are many errors/ warnings in your code.
It should be '\0'. Not "\0";
You have not declared b[];
Initialize i=0;
Instead of strcpy, you can use another method which is shown in below code.
Also, you should not use scanf in last line. You should use printf. scanf is used to take input from user. printf is used to print the results.
Code-
#include <stdio.h>
int main()
{
int i=0,n;
char name[20],b[20];
printf("Enter name: ");
scanf("%s",name);
printf("Your name is %s\n",name);
while(name[i]!='\0')
{
b[i]=name[i]; // to copy from name[] to b[]. Instead of strcpy
i++;
}
printf("copied name: ");
for(n=0;n<=i;n++)
{
printf("%c",b[n]); // to show the copied result.
}
printf("\n");
return 0;
}