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);
Related
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 4 years ago.
Improve this question
//code for mvt. in this ch did not scan by compiler. ch is used as a condition for loop . Its initial value is 'y' , and if it goes to 'n' , loop breaks. In this code i am asking user if he/she want to continue then press y else n . but don't know why gcc do not wait for scan it. It is a simple code but i can not get the mistake.
int main(){
char ch;
temp=tm;
ch='y';
for(i=0;ch=='y';i++){
printf("enter memory size for process %d",i+1);
scanf("%d",&m);
if(m<temp)
{printf("memory allocated\n"); ms[n]=m; n++;
temp-=ms[i];
}
else
{printf("memory not allocated\n"); }
https://stackoverflow.com/users/10404087/rishabh-sharma
printf("do you want to continue");
scanf("%c",&ch);
}
printf("total memory : %d\n",tm);
printf("process \t occupied \n");
for(i=0;i<n;i++)
printf("%7d \t %8d \n",i+1,ms[i]);
printf("total externel fregment : %d \n",temp);
return 0;
}
You have to use flushall() function that clears all buffers associated with input streams, and writes any buffers associated with output streams.
Added:flushall() isn't C, but a vendor specific extension.
OR
Other alternative is to use space before %c
Example
char ch;
scanf(" %c", &ch);
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 4 years ago.
Improve this question
I wrote a program which test the age of person , if the age is above 18 it further asks the name and gender of the person and depending on the gender it prints if output.
But after I enter the age I am getting Segmentation Fault error.
I am using debian and I am a beginner in C language.
#include <stdio.h>
#include <stdlib.h>
int main(){
char name[20];
int age;
char gender;
printf("How old are you ? \n");
scanf(" %d",age);
if(age >= 18){
printf("What your name ? \n");
scanf(" %s",&name);
printf("What's your gender ? (m/f) \n");
scanf(" %c",&gender);
if(gender == 'm'){
printf("Welcome Mr. %s \n",name);
}else{
printf("Welcome Ms/Mrs %s \n",name);
}
}else{
printf("Nothing to see here! \n");
}
return 0;
}
You missed & in the line:
scanf(" %d",age);
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
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!");
}
}
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 8 years ago.
Improve this question
my code here
char temp;
AGAIN:
printf("Enter char: "); scanf("%c", &temp);
if (temp != 'A') goto AGAIN;
why did it go back to AGAIN even though my input is 'A' ?
If I put this block of code to main() function, it works correctly.
But if I do the same thing in the subrountine, it failed.
This is because of the \n character read by the sacnf is next call.
Try this
scanf(" %c", &temp);
^ A space before %c can eat up any number of white-spaces
But for me, its work!. Are you given any other label as AGAIN in your program?
#include<stdio.h>
//./a.out
int main()
{
char temp;
calfunc(temp);
}
char calfunc(char temp)
{
printf("Inside Function");
AGAIN:
printf("Enter char: "); scanf("%c", &temp);
if (temp != 'A')
goto AGAIN;
else
printf("Temp: %c\n",temp);
printf("Final: %c\n",temp);
return temp;
}
Result:
If type 'A', Temp:A Final:A
If type any other even 'a', Enter char: Enter char:. Then cursor places.